C#實現簡單的郵件發送功能

來源:互聯網
上載者:User

標籤:style   blog   color   io   html   div   

     唉,最近要做一個項目,裡面需要實現郵件發送功能。在網路上也找一些看,自己也隨便寫下。也當做是給自己複習下吧,如有不對之處還請大家指出。謝謝!

     首先我是建立一個實體物件Model (EmailParameterSet) :

 1  public  class EmailParameterSet 2     { 3         /// <summary> 4         /// 收件者的郵件地址  5         /// </summary> 6         public string ConsigneeAddress { get; set; } 7  8         /// <summary> 9         /// 收件者的名稱10         /// </summary>11         public string ConsigneeName { get; set; }12 13         /// <summary>14         /// 收件者標題15         /// </summary>16         public string ConsigneeHand { get; set; }17 18         /// <summary>19         /// 收件者的主題20         /// </summary>21         public string ConsigneeTheme { get; set; }22 23         /// <summary>24         /// 發件郵件伺服器的Smtp設定25         /// </summary>26         public string SendSetSmtp { get; set; }27 28         /// <summary>29         /// 寄件者的郵件30         /// </summary>31         public string SendEmail { get; set; }32 33         /// <summary>34         /// 寄件者的郵件密碼35         /// </summary>36         public string SendPwd { get; set; }37         /// <summary>38         /// 發件內容39         /// </summary>40         public string SendContent { get; set; }41     }

方法也簡單  如果發送成功就返回一個bool值 

 

 1  public bool MailSend(EmailParameterSet EPSModel )  2         { 3             try 4             { 5                 //確定smtp伺服器端的地址,實列化一個用戶端smtp  6                 System.Net.Mail.SmtpClient sendSmtpClient = new System.Net.Mail.SmtpClient(EPSModel.SendSetSmtp);//寄件者的郵件伺服器地址 7                 //構造一個發件的人的地址 8                 System.Net.Mail.MailAddress sendMailAddress = new MailAddress(EPSModel.SendEmail, EPSModel.ConsigneeHand, Encoding.UTF8);//寄件者的郵件地址和收件者的標題、編碼 9 10                 //構造一個收件的人的地址11                 System.Net.Mail.MailAddress consigneeMailAddress = new MailAddress(EPSModel.ConsigneeAddress, EPSModel.ConsigneeName, Encoding.UTF8);//收件者的郵件地址和收件者的名稱 和編碼12 13                 //構造一個Email對象14                 System.Net.Mail.MailMessage mailMessage = new MailMessage(sendMailAddress, consigneeMailAddress);//發件地址和收件地址15                 mailMessage.Subject = EPSModel.ConsigneeTheme;//郵件的主題16                 mailMessage.BodyEncoding = Encoding.UTF8;//編碼17                 mailMessage.SubjectEncoding = Encoding.UTF8;//編碼18                 mailMessage.Body = EPSModel.SendContent;//發件內容19                 mailMessage.IsBodyHtml = false;//擷取或者設定指定郵件內文是否為html20 21                 //設定郵件資訊 (指定如何處理待發的電子郵件)22                 sendSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定如何發郵件 是以網路來發23                 sendSmtpClient.EnableSsl = false;//伺服器支援安全接連,安全則為true24 25                 sendSmtpClient.UseDefaultCredentials = false;//是否隨著請求一起發26 27                 //使用者登入資訊28                 NetworkCredential myCredential = new NetworkCredential(EPSModel.SendEmail, EPSModel.SendPwd);29                 sendSmtpClient.Credentials = myCredential;//登入30                 sendSmtpClient.Send(mailMessage);//發郵件31                 return true;//發送成功32             }33             catch (Exception)34             {35                 return false;//發送失敗36             }37         }

然後就調用就可以。

 1   private void button3_Click(object sender, EventArgs e) 2         { 3             EmailParameterSet model = new EmailParameterSet(); 4             model.SendEmail = "[email protected]"; 5             model.SendPwd = "xxx";//密碼 6             model.SendSetSmtp = "smtp.163.com";//發送的SMTP服務地址 ,每個郵箱的是不一樣的。。根據寄件者的郵箱來定 7             model.ConsigneeAddress = "[email protected]"; 8             model.ConsigneeTheme = "主題"; 9             model.ConsigneeHand = "標題";10             model.ConsigneeName = "李可能";11             model.SendContent = "htpp://www.baidu.com";12             if (MailSend(model)== true)13             {14                 MessageBox.Show("郵件發送成功!");15             }16             else17             {18                 MessageBox.Show("郵件發送失敗!");19             }20         }

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.