騰訊雲服務器上發送郵件連接超時(無法發送)的相關問題


<configuration>
<!--Web.config 發送郵箱配置-->
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="123@163.com" >
<network host="smtp.163.com" userName="123@163.com" password="123456" />
</smtp>
</mailSettings>
</system.net>
</configuration>
        /// <summary>
        /// 發送郵件
        /// </summary>
        /// <param name="emailAddressList">收件人地址集合</param>
        /// <param name="Subject">郵件標題</param>
        /// <param name="Body">郵件體(可以為html)</param>
        public bool Sendemail(string emailAddressList, string Subject, string Body)
        {
            try
            {
                SmtpSection cfg = NetSectionGroup.GetSectionGroup(WebConfigurationManager.OpenWebConfiguration("~/Web.config")).MailSettings.Smtp;
                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(cfg.Network.Host);
                client.UseDefaultCredentials = true;
                client.Credentials = new System.Net.NetworkCredential(cfg.Network.UserName, cfg.Network.Password);

                client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

                System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
                Message.From = new System.Net.Mail.MailAddress(cfg.From);//這里需要注意,163似乎有規定發信人的郵箱地址必須是163的,而且發信人的郵箱用戶名必須和上面SMTP服務器認證時的用戶名相同
                //因為上面用的用戶名abc作SMTP服務器認證,所以這里發信人的郵箱地址也應該寫為abc@163.com
                if (emailAddressList == null)
                    return false;

                Message.To.Add(emailAddressList);
                //可以將郵件發送給自己 不然163將視為垃圾郵件 無法發送
                Message.To.Add("123@163.com");
                Message.Subject = Subject;
                Message.Body = Body;
                Message.SubjectEncoding = System.Text.Encoding.UTF8;
                Message.BodyEncoding = System.Text.Encoding.UTF8;
                Message.Priority = System.Net.Mail.MailPriority.High;
                Message.IsBodyHtml = true;      //可以為html

                client.Send(Message);
                return true;
            }
            catch (Exception ex)
            {
                string path = Server.MapPath("~") + "\\mail.txt";
                if (!System.IO.File.Exists(path))
                {
                    FileStream fs1 = new FileStream(path, FileMode.Create, FileAccess.Write);//創建寫入文件 
                    StreamWriter sw = new StreamWriter(fs1);
                    sw.WriteLine(DateTime.Now.ToString() + " ;Source:" + ex.Source + " ;HelpLink:" + ex.HelpLink + " ;Message:" + ex.Message);//開始寫入值
                    sw.Close();
                    fs1.Close();
                }
                else
                {
                    StreamWriter sr = System.IO.File.AppendText(path);
                    sr.WriteLine(DateTime.Now.ToString() + " ;Source:" + ex.Source + " ;HelpLink:" + ex.HelpLink + " ;Message:" + ex.Message);//追加寫入值
                    sr.Close();
                }

                return false;
            }
        }

 

騰訊雲會將服務器25端口禁用(騰訊雲默認禁用)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM