使用SMTP(簡單郵件傳輸協議)發送郵件一般都是使用默認的25端口,而阿里雲服務器為了安全將25端口封禁了,會出現在本機測試發送郵件功能正常,但是部署到服務器上卻發送失敗的情況。
解決辦法是向阿里雲申請解封25端口,或者更換端口,建議使用587端口(有的說465可用但經過測試不可用)
using System.Configuration; using System.Collections.Specialized; using System.IO; using System.Net.Mail;
public string BulidFile(string tempPath, NameValueCollection values) { string prefix = "[$"; string postfix = "]"; StreamReader reader = null; string template = string.Empty; try { reader = new StreamReader(tempPath); template = reader.ReadToEnd(); reader.Close(); if (values != null) { foreach (string key in values.AllKeys) { template = template.Replace(string.Format("{0}{1}{2}", prefix, key, postfix), values[key]); } } } catch { } finally { if (reader != null) reader.Close(); } return template; }
Email文件:
Hello,
[$NAME]
[$URL]
配置文件:
<appSettings> <!-- 發送地址--> <add key="sendFrom" value="111111111@qq.com" /> <!-- 發送人的名字--> <add key="sendUserName" value="發送人姓名" /> <!-- 郵件標題--> <add key="sendTitle" value="EmailTitle" /> <!-- 郵箱用戶名--> <add key="userName" value="111111111@qq.com" /> <!-- 郵箱密碼注意使用授權碼--> <add key="passWord" value="bccdbrpveswhbghj" /> <!-- 郵箱服務器地址--> <add key="smtpServer" value="smtp.qq.com" /> </appSettings>
1 public void SendEmail(string recName) 2 { 3 string templetpath = Server.MapPath("../../Resource/EmailTemplate/Email.txt"); 4 NameValueCollection myCol = new NameValueCollection(); 5 string Url =""; 6 string Name = ""; 7 myCol.Add("NAME", Name); 8 myCol.Add("URL", Url); 9 string mailBody = BulidFile(templetpath, myCol); 10 string sendFrom = ConfigurationManager.AppSettings["sendFrom"]; //生成一個發送地址 11 string sendUserName = ConfigurationManager.AppSettings["sendUserName"];//發送人的名字 12 string sendTitle = ConfigurationManager.AppSettings["sendTitle"];//發送郵件標題 13 string username = ConfigurationManager.AppSettings["userName"];//發送郵箱用戶名 14 string passwd = ConfigurationManager.AppSettings["passWord"];//發送郵箱密碼 15 string smtpServer = ConfigurationManager.AppSettings["smtpServer"];//發送郵箱密碼 16 MailMessage msg = new MailMessage(); 17 msg.To.Add(recName); 18 /* 19 * msg.To.Add("b@b.com"); 20 * msg.To.Add("b@b.com"); 21 * msg.To.Add("b@b.com");可以發送給多人 22 */ 23 /* 24 * msg.CC.Add("c@c.com"); 25 * msg.CC.Add("c@c.com");可以抄送給多人 26 */ 27 /* 上面3個參數分別是發件人地址(可以隨便寫),發件人姓名,編碼*/ 28 msg.From = new MailAddress(sendFrom); 29 msg.Subject = sendTitle;//郵件標題 30 msg.SubjectEncoding = System.Text.Encoding.UTF8;//郵件標題編碼 31 msg.Body = mailBody;//郵件內容 32 msg.BodyEncoding = System.Text.Encoding.UTF8;//郵件內容編碼 33 msg.Priority = System.Net.Mail.MailPriority.High;//郵件優先級 34 msg.IsBodyHtml = false;//是否是HTML郵件 35 SmtpClient client = new SmtpClient(); 36 //注意一下三行代碼 37 client.EnableSsl = true; 38 client.UseDefaultCredentials = false; 39 client.DeliveryMethod = SmtpDeliveryMethod.Network; 40 //指定發件人的郵件地址和密碼以驗證發件人身份默認端口為25 41 client.Port = 587; 42 client.Credentials = new System.Net.NetworkCredential(sendFrom, passwd); 43 client.Host = smtpServer; 44 try 45 { 46 client.Send(msg); --異步可使用SendAsync 48 } 49 catch (System.Net.Mail.SmtpException ex) 50 { 51 52 } 53 }