1. 程式人生 > >阿里雲伺服器郵件傳送

阿里雲伺服器郵件傳送

一個郵件傳送的功能,本機除錯無問題,但釋出到阿里雲伺服器後郵件傳送功能失敗。

網上查了下大概是說阿里雲把傳送郵件的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>[email protected]</ConsigneeAddress> <!--抄送郵箱地址,多個郵箱間用'|'分割--> <BccAddress></BccAddress> <!--收件人名稱--> <ConsigneeName>浦泓醫療</ConsigneeName> <!--發件人名稱--> <ConsigneeHand
>微商城</ConsigneeHand> <!--郵件主題--> <ConsigneeTheme>睛彩眼界商城訂單</ConsigneeTheme> <!--發件郵件伺服器的Smtp設定--> <SendSetSmtp>smtp.qq.com</SendSetSmtp> <!--發件人的郵件--> <SendEmail>[email protected]</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; }

    }
}
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