最近开发一个项目,需要给客户发送报告邮件,在开发中遇到本地调试发送邮件一切正常,但当部署到服务器上的时候,一直返回“发送失败”,在此记录,以免以后采坑
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邮箱账号进行发送邮件,结果真的是发送成功了。