现在的位置: 首页 > 综合 > 正文

C# 发送邮件

2013年10月10日 ⁄ 综合 ⁄ 共 1060字 ⁄ 字号 评论关闭
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mail;
using System.ComponentModel;
using System.Net.Mail;
using System.Net;
using System.Text;

namespace HmltEmail
{
    public class EmailHepler
    {
        public void sendEmail()
        {
            System.Net.Mail.MailMessage mailMsg = new System.Net.Mail.MailMessage();
            mailMsg.From = new MailAddress("发件邮箱地址");
            mailMsg.To.Add("收件邮箱地址1");
            mailMsg.To.Add("收件邮箱地址2");
            mailMsg.Subject = "邮件主题";
            mailMsg.Body = "<p style=\"color:Red\">dewf,<img alt=\"\" src=\"http://192.168.34.345:8000/Areas/__eret__/images/contact_china.gif\">我用ergtr发送邮件<p>";
            mailMsg.BodyEncoding = Encoding.UTF8;
            mailMsg.IsBodyHtml = true;
            mailMsg.Priority = System.Net.Mail.MailPriority.High;

            SmtpClient smtp = new SmtpClient();
            // 提供身份验证的用户名和密码 
            // 网易邮件用户可能为:username password 
            // Gmail 用户可能为:username@gmail.com password 
            smtp.Credentials = new NetworkCredential("发件箱地址", "发件邮箱密码");
            smtp.Port = 25; // Gmail 使用 465 和 587 端口 
            smtp.Host = "smtp.163.com"; // 如 smtp.163.com, smtp.gmail.com 
            smtp.EnableSsl = false; // 如果使用GMail,则需要设置为true 
            try
            {
                smtp.Send(mailMsg);
            }
            catch (SmtpException ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

抱歉!评论已关闭.