不能發送郵件可能原因:(以下以163郵箱為例,smtp服務器是: smtp.163.com)
1、端口被封,目前阿里雲很多25端口被封不能使用。測試方法:
使用Telnet smtp.163.com 25
如果端口正常可以使用25端口發送,否則只能改用465,由於.net 的新方法:System.Net.Mail.StmpClient不能用465端口發,所以如果使用465改用過時的方法 System.Web.Mail.SmtpMail
2、被發件服務器當成垃圾郵件阻止。 解決方法:抄送一份給自己。
/// <summary> /// 發送郵箱 /// </summary> /// <param name="emails">發送郵箱</param> /// <param name="title">郵件標題</param> /// <param name="cPassword">郵件內容</param> public static void SendEmailBack(List<string> emails, string title, string content) { string Email_smtp = new BSet("500").cCode2;//發郵件smtp服務器名稱 string Email = new BSet("510").cCode2; //發送人郵箱地址 string Email_smtp_J =new BSet("520").cCode2;//發郵件郵箱授權碼 WriteLog("Email_smtp:" + Email_smtp); System.Web.Mail.MailMessage mmsg = new System.Web.Mail.MailMessage(); //郵件主題 mmsg.Subject = title; mmsg.BodyFormat = System.Web.Mail.MailFormat.Html; //郵件正文 mmsg.Body = content; //正文編碼 mmsg.BodyEncoding = Encoding.UTF8; //優先級 mmsg.Priority = System.Web.Mail.MailPriority.High; //發件者郵箱地址 mmsg.From = Email; //收件人收箱地址 string to =""; emails.ForEach(s=>{to+= s+",";}); if (!string.IsNullOrEmpty(to)){ to += Email; } //mmsg.To = "th_msc@qq.com"; mmsg.Cc = to ; mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //用戶名 mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", Email); //密碼 mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Email_smtp_J); //端口 mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465); //是否ssl mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl",true); //Smtp服務器 System.Web.Mail.SmtpMail.SmtpServer = Email_smtp; try { System.Web.Mail.SmtpMail.Send(mmsg); } catch (SmtpException e) { WriteLog("發送郵件出錯:" + e.ToString()); } }