c# 用465端口發送郵件,以解決阿里雲服務器25端口發送不了的情況

/// <summary>
        /// 郵件服務器地址
        /// </summary>
        private static string MailServer = "smtp.qq.com";
        /// <summary>
        /// 用戶名
        /// </summary>
        private static string MailUserName = "[email protected]";
        /// <summary>
        /// 密碼
        /// </summary>
        private static string MailPassword = "sdfdsfdfsdfsdf";
        /// <summary>
        /// 名稱
        /// </summary>
        private static string MailName = "測試";
public static void SendThreadBy465(string to, string title, string body)
        {
            new Thread(new ThreadStart(delegate ()
            {
                try
                {
                    System.Web.Mail.MailMessage mmsg = new System.Web.Mail.MailMessage();
                    //驗證  
                    mmsg.Subject = title.Trim(); ;// "zhuti1";//郵件主題

                    mmsg.BodyFormat = System.Web.Mail.MailFormat.Html;
                    mmsg.Body = body;// "wqerwerwerwer";//郵件正文
                    mmsg.BodyEncoding = Encoding.UTF8;//正文編碼
                    mmsg.Priority = System.Web.Mail.MailPriority.High;//優先級

                    mmsg.From = MailUserName;//發件者郵箱地址
                    mmsg.To = to;//收件人收箱地址
                    mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                    //登陸名  
                    mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", MailUserName);
                    //登陸密碼  
                    mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", MailPassword);
                    mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//端口 
                    mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
                    System.Web.Mail.SmtpMail.SmtpServer = MailServer;
                    System.Web.Mail.SmtpMail.Send(mmsg);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }

            })).Start();
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章