<configuration>
<!--Web.config 发送邮箱配置-->
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="123@163.com" >
<network host="smtp.163.com" userName="123@163.com" password="123456" />
</smtp>
</mailSettings>
</system.net>
</configuration>
/// <summary> /// 发送邮件 /// </summary> /// <param name="emailAddressList">收件人地址集合</param> /// <param name="Subject">邮件标题</param> /// <param name="Body">邮件体(可以为html)</param> public bool Sendemail(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) return false; Message.To.Add(emailAddressList); //可以将邮件发送给自己 不然163将视为垃圾邮件 无法发送 Message.To.Add("123@163.com"); 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); return true; } 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(); } return false; } }
腾讯云会将服务器25端口禁用(腾讯云默认禁用)