阿里云服务器邮件发送
生活随笔
收集整理的這篇文章主要介紹了
阿里云服务器邮件发送
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個郵件發送的功能,本機調試無問題,但發布到阿里云服務器后郵件發送功能失敗。
網上查了下大概是說阿里云把發送郵件的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; }} } emailpara?
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; }} } EmailParameterSet?
?
說明:
實例中使用的是qq郵箱,但郵箱的密匙非qq的密碼,而是郵箱的獨立密碼,可以進入qq郵箱,然后在設置-》賬戶里面設置
并且要保證郵箱的POP3/SMTP服務開啟,同樣是進入qq郵箱,然后在設置-》賬戶里面設置
?
引用:
https://www.cnblogs.com/axinno1/p/8303130.html
?
轉載于:https://www.cnblogs.com/eye-like/p/10103783.html
總結
以上是生活随笔為你收集整理的阿里云服务器邮件发送的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python变量名可以包含的字符有问号吗
- 下一篇: ci mysql空闲连接回收_数据库连接