最近開發一個項目,需要給客戶發送報告郵件,在開發中遇到本地調試發送郵件一切正常,但當部署到伺服器上的時候,一直返回“發送失敗”,在此記錄,以免以後採坑 webapi 端代碼: /// <summary> /// 發送郵件 /// </summary> /// <param name="fromMai ...
最近開發一個項目,需要給客戶發送報告郵件,在開發中遇到本地調試發送郵件一切正常,但當部署到伺服器上的時候,一直返回“發送失敗”,在此記錄,以免以後採坑
webapi 端代碼:
/// <summary> /// 發送郵件 /// </summary> /// <param name="fromMail">發件人郵箱</param> /// <param name="toMail">收件人郵箱</param> /// <param name="authorizationCode">發件人授權碼</param> /// <param name="replyTo">收件人回覆郵件預設接收地址(不設置也可以)</param> /// <param name="ccMail">郵件抄送人</param> /// <param name="filePath">附件地址</param> /// <param name="sendPeopleCall">收件人稱呼</param> /// <param name="collectSampleDate">收樣時間</param> /// <returns></returns> public MsgModel SendMails(string fromMail,List<string> toMail,string authorizationCode,string replyTo, List<string> ccMail,List<string> filePath,string sendPeopleCall,string collectSampleDate) { try { //實例化發送郵件類 MailMessage mailMessage = new MailMessage(); //郵件的優先順序 分為 low normal high 通常用Normal即可 mailMessage.Priority = MailPriority.Normal; //發送人郵件地址 mailMessage.From = new MailAddress(fromMail); //收件人郵件地址 //考慮群發郵件 foreach(var r in toMail) { mailMessage.To.Add(new MailAddress(r)); } if (replyTo == "" || replyTo == null) { replyTo = fromMail; } //對方回覆郵件時預設的接收地址(可以不設置) //mailMessage.ReplyTo = new MailAddress(replyTo); if (ccMail != null) { //郵件抄送 支持 群發 抄送 foreach (var r in ccMail) { mailMessage.CC.Add(r); } } //如果郵件標題包含中文,這裡要設置編碼方式 否則會出現亂碼 mailMessage.SubjectEncoding = Encoding.GetEncoding(936); //郵件正文是否是Html 格式 mailMessage.IsBodyHtml = true; //郵件標題 mailMessage.Subject = $@" REPORT-{collectSampleDate}"; //郵件內容 mailMessage.Body = $@" Dear {sendPeopleCall}, "; mailMessage.Attachments.Clear(); if (filePath != null&&filePath.Count>0) { foreach (var r in filePath) { var urls = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + r;//獲取項目的絕對 //將附件添加到郵件 mailMessage.Attachments.Add(new Attachment(urls)); //獲取設置此電子郵件的發送通知 mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess; } } //實例化 SmtpClient類 SmtpClient client = new SmtpClient(); client.Host = GetClientHost(fromMail); //使用安全加密連接 client.EnableSsl = true; client.Port = 587;//預設是25 此處設置成587 //不和請求一塊發送 client.UseDefaultCredentials = false; //client.DeliveryMethod = SmtpDeliveryMethod.Network; //驗證發送人身份(發件人的郵箱,郵箱里生成的授權碼) client.Credentials = new NetworkCredential(fromMail,authorizationCode); //如果發送失敗,SMTP 伺服器將發送 失敗郵件告知fromEmail mailMessage.DeliveryNotificationOptions= DeliveryNotificationOptions.OnFailure; client.Send(mailMessage); return new MsgModel { Status = true, Msg = "發送郵件成功" , Data = "" }; } catch(Exception ex) { return new MsgModel { Status = false, Msg = "發送郵件失敗!錯誤:" + ex.Message, Data="" }; } }
/// <summary> /// 根據發送郵箱 識別發件伺服器地址 /// </summary> /// <param name="fromEmail"></param> /// <returns></returns> public string GetClientHost(string fromEmail) { var host = ""; if (fromEmail.Length != 0) { var addressStr = fromEmail.Trim().Split(new char[] { '@','.'}); switch (addressStr[1]) { case "163": host = "smtp.163.com"; break; case "126": host = "smtp.126.com"; break; case "qq": host = "smtp.qq.com"; break; case "gmail": host = "smtp.gmail.com"; break; case "hotmail": host = "smtp.live.com";//outlook郵箱 //client.Port = 587; break; case "foxmail": host = "smtp.foxmail.com"; break; case "sina": host = "smtp.sina.com.cn"; break; default: host = "smtp.exmail.qq.com";//qq企業郵箱 break; } } return host; }
發送郵箱,以及授權碼都放在配置文件webconfig 中,這裡不展示。
另外:項目是部署到阿裡伺服器的,此項目郵件發送埠是587,因此在阿裡伺服器需要設置允許埠587進出。
在此基礎上本地調試可正常發送,部署到伺服器上就發送失敗,經過查閱相關博客,不斷的嘗試其他博客的解決方法,其中一個博客中說:本地調試發送了郵件成功後,tx那邊認為你當前發送郵件的地址為常用的郵件發送地址,再部署到伺服器上的時候,tx就認為你是發送廣告,就被禁止當前地址發送郵箱,需要解除禁止。禁止一時半會不知道咋去解禁,於是嘗試著替換一個qq郵箱賬號進行發送郵件,結果真的是發送成功了。