.NET MVC之郵件發送

來源:互聯網
上載者:User

標籤:連接埠   div   pass   htm   本地   har   項目   技術   mic   

在以前的項目中,設計到發送郵件的WEB站,當時在網上找了一段代碼研究了一下:

    private static void SendEmail(string clientHost, string emailAddress, string receiveAddress,          string userName, string password, string subject, string body)        {            MailMessage mail = new MailMessage();            mail.From = new MailAddress(emailAddress);            mail.To.Add(new MailAddress(receiveAddress));            mail.Subject = subject;            mail.Body = body;            mail.IsBodyHtml = true;            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;            SmtpClient client = new SmtpClient();            client.Host = clientHost;            client.Credentials = new NetworkCredential(userName, password);            client.DeliveryMethod = SmtpDeliveryMethod.Network;            try            {                client.SendAsync(mail,null);            }            catch (Exception)            {            }        }

  在本地測試沒問題,但是部署到window server2012上就無法發送了,然後在網上百度了一堆,配置郵件伺服器什麼的都不盡人意,後來發現一種已棄用的代碼反而正常了:

  public static void SendEmail(string from, string subject, string body, List<string> mailAddress, string host, int port, string password)        {            System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();            try            {                mail.To = string.Join(",", mailAddress);                mail.From = from;                mail.Subject = subject;                mail.BodyFormat = System.Web.Mail.MailFormat.Html;                mail.Body = body;                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //身分識別驗證                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mail.From); //郵箱登入帳號,這裡跟前面的發送帳號一樣就行                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password); //這個密碼要注意:如果是一般帳號,要用授權碼;企業帳號用登入密碼                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//連接埠                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//SSL加密                System.Web.Mail.SmtpMail.SmtpServer = "smtp.qq.com";    //企業帳號用smtp.exmail.qq.com                System.Web.Mail.SmtpMail.Send(mail);                //郵件發送成功            }            catch (Exception ex)            {                //失敗,錯誤資訊:ex.Message;            }        }

  

後來發現可能是因為伺服器架設在內網中,特定連接埠沒開或者一些限制,不過由於工作要求,暫時只能退步走了,以後一定要改正過來。

 

.NET MVC之郵件發送

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.