C# smtp邮件发送

第一种方式发送邮件,不读取配置文件发送邮件,(本地测试可以,但是服务器上不行)

/// <summary>
/// 发送邮件
/// </summary>
/// <param name="emailAddressList">收件人地址集合</param>
/// <param name="Subject">邮件标题</param>
/// <param name="Body">邮件体(可以为html)</param>

public void email(List<string> emailAddressList, string Subject, string Body)

{
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
            client.Host = "smtp.163.com";//使用163的SMTP服务器发送邮件
            client.UseDefaultCredentials = true;
            client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            client.Credentials = new System.Net.NetworkCredential("15221591234@163.com", "1321510477");//163的SMTP服务器需要用163邮箱的用户名和smtp授权码作认证,如果没有需要去163申请个,
            //这里假定你已经拥有了一个163邮箱的账户,用户名为abc,密码为*******
            System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
            Message.From = new System.Net.Mail.MailAddress("15221591234@163.com");//这里需要注意,163似乎有规定发信人的邮箱地址必须是163的,而且发信人的邮箱用户名必须和上面SMTP服务器认证时的用户名相同
            //因为上面用的用户名abc作SMTP服务器认证,所以这里发信人的邮箱地址也应该写为abc@163.com
            if (emailAddressList == null || emailAddressList.Count <= 0)
                return;
            foreach (string email in emailAddressList)
            {
                Message.To.Add(email);//将邮件发送给Gmail
            }
            //Message.To.Add("123456@gmail.com");//将邮件发送给Gmail
            //Message.To.Add("123456@qq.com");//将邮件发送给QQ邮箱
            Message.Subject = Subject;
            Message.Body = Body;
            Message.SubjectEncoding = System.Text.Encoding.UTF8;
            Message.BodyEncoding = System.Text.Encoding.UTF8;
            Message.Priority = System.Net.Mail.MailPriority.High;
            Message.IsBodyHtml = true;  //可以为html
            try
            {
                client.Send(Message);
            }
            catch(Exception ex)
            {
                string path = Server.MapPath("~") + "\\mail.txt";
                if (!System.IO.File.Exists(path))
                {
                    FileStream fs1 = new FileStream(path, FileMode.Create, FileAccess.Write);//创建写入文件 
                    StreamWriter sw = new StreamWriter(fs1);
                    sw.WriteLine(DateTime.Now.ToString() + " ;Source:" + ex.Source + " ;HelpLink:" + ex.HelpLink + " ;Message:" + ex.Message);//开始写入值
                    sw.Close();
                    fs1.Close();
                }
                else
                {
                    StreamWriter sr = System.IO.File.AppendText(path);
                    sr.WriteLine(DateTime.Now.ToString() + " ;Source:" + ex.Source + " ;HelpLink:" + ex.HelpLink + " ;Message:" + ex.Message);//追加写入值
                    sr.Close();
                }
            }
}

第二种方式,通配置web.config发送邮件,(服务器上也能成功,推荐这种方式,方便以后修改smtp服务)

配置文件如下

<configuration>
     <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="15221591234@163.com" >
        <network host="smtp.163.com" userName="15221591234@163.com" password="1321510477" />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

代码如下

/// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="emailAddressList">收件人地址集合</param>
        /// <param name="Subject">邮件标题</param>
        /// <param name="Body">邮件体(可以为html)</param>
        public void email(List<string> emailAddressList, string Subject, string Body)
        {
            try
            {
                SmtpSection cfg = NetSectionGroup.GetSectionGroup(WebConfigurationManager.OpenWebConfiguration("~/web.config")).MailSettings.Smtp;
                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(cfg.Network.Host);
                client.UseDefaultCredentials = true;
                client.Credentials = new System.Net.NetworkCredential(cfg.Network.UserName, cfg.Network.Password);

                client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

                System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
                Message.From = new System.Net.Mail.MailAddress(cfg.From);//这里需要注意,163似乎有规定发信人的邮箱地址必须是163的,而且发信人的邮箱用户名必须和上面SMTP服务器认证时的用户名相同
                //因为上面用的用户名abc作SMTP服务器认证,所以这里发信人的邮箱地址也应该写为abc@163.com
                if (emailAddressList == null || emailAddressList.Count <= 0)
                    return;
                foreach (string email in emailAddressList)
                {
                    Message.To.Add(email);//将邮件发送给Gmail
                }
                //Message.To.Add("123456@gmail.com");//将邮件发送给Gmail
                //Message.To.Add("123456@qq.com");//将邮件发送给QQ邮箱
                Message.Subject = Subject;
                Message.Body = Body;
                Message.SubjectEncoding = System.Text.Encoding.UTF8;
                Message.BodyEncoding = System.Text.Encoding.UTF8;
                Message.Priority = System.Net.Mail.MailPriority.High;
                Message.IsBodyHtml = true;      //可以为html

                client.Send(Message);
            }
            catch (Exception ex)
            {
                string path = Server.MapPath("~") + "\\mail.txt";
                if (!System.IO.File.Exists(path))
                {
                    FileStream fs1 = new FileStream(path, FileMode.Create, FileAccess.Write);//创建写入文件 
                    StreamWriter sw = new StreamWriter(fs1);
                    sw.WriteLine(DateTime.Now.ToString() + " ;Source:" + ex.Source + " ;HelpLink:" + ex.HelpLink + " ;Message:" + ex.Message);//开始写入值
                    sw.Close();
                    fs1.Close();
                }
                else
                {
                    StreamWriter sr = System.IO.File.AppendText(path);
                    sr.WriteLine(DateTime.Now.ToString() + " ;Source:" + ex.Source + " ;HelpLink:" + ex.HelpLink + " ;Message:" + ex.Message);//追加写入值
                    sr.Close();
                }
            }
        }

smtp服务可以去163申请一个邮箱,然后开通smtp服务(其中15221591234@163.com是你邮箱地址,1321510477是你授权码)

http://jingyan.baidu.com/article/4e5b3e19266fee91901e2489.html

 

posted @ 2017-01-13 17:45  漫天行  阅读(4011)  评论(1编辑  收藏  举报