一個郵件發送的功能,本機調試無問題,但發佈到阿里雲服務器後郵件發送功能失敗。html
網上查了下大概是說阿里雲把發送郵件的25端口禁用掉了安全
那麼解決方式一就是向阿里雲申請開放25端口,但具體如何申請,並未深刻操做。服務器
解決方式二:使用郵件服務商的加密端口。網絡
可是當使用465端口時,前後試驗過smtp.mxhichina.com(阿里企業郵箱)、smtp.163.com(163郵箱)、smtp.qq.com(qq郵箱)三種發送方式,均失敗!ide
再嘗試考慮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;//服務器支持安全接連,安全則爲true sendSmtpClient.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郵箱,而後在設置-》帳戶裏面設置spa
而且要保證郵箱的POP3/SMTP服務開啓,一樣是進入qq郵箱,而後在設置-》帳戶裏面設置3d
引用: