C#發送郵件(使用SSL,587端口)


static readonly string smtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"];
        static readonly string userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
        static readonly string pwd = System.Configuration.ConfigurationManager.AppSettings["Pwd"];
        static readonly int smtpPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpPort"]);
        static readonly string authorName = System.Configuration.ConfigurationManager.AppSettings["AuthorName"];
        protected void Page_Load(object sender, EventArgs e)
        {
            MailMessage msg = new System.Net.Mail.MailMessage();
            msg.To.Add("1833793557@qq.com");

            msg.From = new MailAddress(userName, authorName, System.Text.Encoding.UTF8);
            /* 上面3個參數分別是發件人地址(可以隨便寫),發件人姓名,編碼*/
            msg.Subject = "這是測試郵件";//郵件標題    
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//郵件標題編碼    
            msg.Body = "郵件內容";//郵件內容    
            msg.BodyEncoding = System.Text.Encoding.UTF8;//郵件內容編碼    
            msg.IsBodyHtml = false;//是否是HTML郵件    
            msg.Priority = MailPriority.High;//郵件優先級    

            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential(userName, pwd);
            
            client.Port = 587; 
            client.Host = smtpServer;
            client.EnableSsl = true; //經過ssl加密    
            object userState = msg;

            //異步發送
            client.SendAsync(msg, userState);            
            //同步發送
            //client.Send(msg);
        }

 


免責聲明!

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



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