码迷,mamicode.com
首页 > 其他好文 > 详细

推荐使用SMTP服务发送邮件

时间:2018-08-16 13:44:32      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:get   图片   sed   方便   gif   class   summary   for   代码   

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

userName是你的邮箱地址,password是你的授权码

配置文件如下

技术分享图片
<configuration>
     <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="XXXXX@163.com" >
        <network host="smtp.163.com" userName="XXXXX@163.com" password="XXXX" />
      </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服务发送邮件

标签:get   图片   sed   方便   gif   class   summary   for   代码   

原文地址:https://www.cnblogs.com/zhangxianshen/p/9486458.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!