一个邮件发送的功能,本机调试无问题,但发布到阿里云服务器后邮件发送功能失败。
网上查了下大概是说阿里云把发送邮件的25端口禁用掉了
那么解决方式一就是向阿里云申请开放25端口,但具体如何申请,并未深入操作。
解决方式二:使用邮件服务商的加密端口。
但是当使用465端口时,先后试验过smtp.mxhichina.com(阿里企业邮箱)、smtp.163.com(163邮箱)、smtp.qq.com(qq邮箱)三种发送方式,均失败!
再尝试考虑SSL加密SMTP通过587端口进行发件,发送成功。
以下为配置及源码
<?xml version="1.0" encoding="utf-8"?> <xml><!--收件人邮箱地址--><ConsigneeAddress>pro@163.com</ConsigneeAddress><!--抄送邮箱地址,多个邮箱间用'|'分割--><BccAddress></BccAddress><!--收件人名称--><ConsigneeName>浦泓医疗</ConsigneeName><!--发件人名称--><ConsigneeHand>微商城</ConsigneeHand><!--邮件主题--><ConsigneeTheme>睛彩眼界商城订单</ConsigneeTheme><!--发件邮件服务器的Smtp设置--><SendSetSmtp>smtp.qq.com</SendSetSmtp><!--发件人的邮件--><SendEmail>124@qq.com</SendEmail><!--发件人的邮件密码--><SendPwd>boblunxyluwdjjbh</SendPwd><!--发件端口号--><port>587</port><!--邮件内容--><SendContent>您有新的订单消息</SendContent><!--后台管理地址--><serverAddress>http://xxx/admin/login</serverAddress> </xml>
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Mail; using System.Text; using System.Web; using System.Xml;namespace MallServer.Utility {public class emailhelper{public static bool MailSend(emailpara para){try{EmailParameterSet EPSModel = new EmailParameterSet();string filepath = System.Web.HttpContext.Current.Server.MapPath("\\Files\\email\\email.xml");XmlDocument xml = common.xmlHelper.getXML(filepath);string BccAddress = xml.SelectSingleNode("xml").SelectSingleNode("BccAddress").InnerText;//邮件抄送地址string portvalue = xml.SelectSingleNode("xml").SelectSingleNode("port").InnerText; //发送邮件的端口int port = 587;int.TryParse(portvalue, out port);string serverAddress= xml.SelectSingleNode("xml").SelectSingleNode("serverAddress").InnerText;//提示跳转的管理地址 EPSModel.ConsigneeAddress = xml.SelectSingleNode("xml").SelectSingleNode("ConsigneeAddress").InnerText;EPSModel.ConsigneeName = xml.SelectSingleNode("xml").SelectSingleNode("ConsigneeName").InnerText;// EPSModel.ConsigneeHand = xml.SelectSingleNode("xml").SelectSingleNode("ConsigneeHand").InnerText;//发件人标题EPSModel.ConsigneeTheme = xml.SelectSingleNode("xml").SelectSingleNode("ConsigneeTheme").InnerText;//收件人的主题EPSModel.SendSetSmtp = xml.SelectSingleNode("xml").SelectSingleNode("SendSetSmtp").InnerText;//发件邮件服务器的Smtp设置EPSModel.SendEmail = xml.SelectSingleNode("xml").SelectSingleNode("SendEmail").InnerText;//发件人的邮件EPSModel.SendPwd = xml.SelectSingleNode("xml").SelectSingleNode("SendPwd").InnerText;EPSModel.SendContent = xml.SelectSingleNode("xml").SelectSingleNode("SendContent").InnerText;if (para.ConsigneeTheme != "") {EPSModel.ConsigneeTheme = para.ConsigneeTheme;}if (para.SendContent != "") {EPSModel.SendContent = para.SendContent+"\r\n查看详细请登陆 "+serverAddress; }//确定smtp服务器端的地址,实列化一个客户端smtp System.Net.Mail.SmtpClient sendSmtpClient = new System.Net.Mail.SmtpClient(EPSModel.SendSetSmtp);//发件人的邮件服务器地址//构造一个发件的人的地址System.Net.Mail.MailAddress sendMailAddress = new MailAddress(EPSModel.SendEmail, EPSModel.ConsigneeHand, Encoding.UTF8);//发件人的邮件地址和收件人的标题、编码//构造一个收件的人的地址System.Net.Mail.MailAddress consigneeMailAddress = new MailAddress(EPSModel.ConsigneeAddress, EPSModel.ConsigneeName, Encoding.UTF8);//收件人的邮件地址和收件人的名称 和编码//构造一个Email对象System.Net.Mail.MailMessage mailMessage = new MailMessage(sendMailAddress, consigneeMailAddress);//发件地址和收件地址if (BccAddress != ""){string[] addressArr = BccAddress.Split('|');for (int i = 0; i < addressArr.Length; i++){mailMessage.Bcc.Add(new MailAddress(addressArr[i]));//添加抄送 }}mailMessage.Subject = EPSModel.ConsigneeTheme;//邮件的主题mailMessage.BodyEncoding = Encoding.UTF8;//编码mailMessage.SubjectEncoding = Encoding.UTF8;//编码mailMessage.Body = EPSModel.SendContent;//发件内容mailMessage.IsBodyHtml = false;//获取或者设置指定邮件正文是否为html//设置邮件信息 (指定如何处理待发的电子邮件)sendSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定如何发邮件 是以网络来发sendSmtpClient.EnableSsl = true;//服务器支持安全接连,安全则为truesendSmtpClient.Port = port;sendSmtpClient.UseDefaultCredentials = true;//是否随着请求一起发//用户登录信息NetworkCredential myCredential = new NetworkCredential(EPSModel.SendEmail, EPSModel.SendPwd);sendSmtpClient.Credentials = myCredential;//登录 sendSmtpClient.Send(mailMessage);//发邮件return true;}catch (Exception ex){//common.CommonMethod.WriteTxt("ex.message:"+ex.Message);//common.CommonMethod.WriteTxt("ex.Source:" + ex.Source);//common.CommonMethod.WriteTxt("ex.StackTrace:" + ex.StackTrace);return false;}}} }
using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace MallServer.Utility {public class emailpara{public string ConsigneeTheme { get; set; }public string SendContent { get; set; }} }
using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace MallServer.Utility {public class EmailParameterSet{/// <summary>/// 收件人的邮件地址 /// </summary>public string ConsigneeAddress { get; set; }/// <summary>/// 收件人的名称/// </summary>public string ConsigneeName { get; set; }/// <summary>/// 收件人标题/// </summary>public string ConsigneeHand { get; set; }/// <summary>/// 收件人的主题/// </summary>public string ConsigneeTheme { get; set; }/// <summary>/// 发件邮件服务器的Smtp设置/// </summary>public string SendSetSmtp { get; set; }/// <summary>/// 发件人的邮件/// </summary>public string SendEmail { get; set; }/// <summary>/// 发件人的邮件密码/// </summary>public string SendPwd { get; set; }/// <summary>/// 发件内容/// </summary>public string SendContent { get; set; }} }
说明:
实例中使用的是qq邮箱,但邮箱的密匙非qq的密码,而是邮箱的独立密码,可以进入qq邮箱,然后在设置-》账户里面设置
并且要保证邮箱的POP3/SMTP服务开启,同样是进入qq邮箱,然后在设置-》账户里面设置
引用:
https://www.cnblogs.com/axinno1/p/8303130.html