推荐使用SMTP服务发送邮件

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

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

配置文件如下

复制代码
<configuration>
     <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="[email protected]" >
        <network host="smtp.163.com" userName="[email protected]" 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服务器认证,所以这里发信人的邮箱地址也应该写为[email protected]
                if (emailAddressList == null || emailAddressList.Count <= 0)
                    return;
                foreach (string email in emailAddressList)
                {
                    Message.To.Add(email);//将邮件发送给Gmail
                }
                //Message.To.Add("[email protected]");//将邮件发送给Gmail
                //Message.To.Add("[email protected]");//将邮件发送给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();
                }
            }
        }

猜你喜欢

转载自www.cnblogs.com/zhangxianshen/p/9486458.html