C#發送郵件三種方法(Localhost,SMTP,SSL-SMTP)

來源:互聯網
上載者:User

標籤:blog   http   io   ar   os   使用   sp   資料   on   

原文:C#發送郵件三種方法(Localhost,SMTP,SSL-SMTP)

最近公司由於一個R&I項目的需要,使用者要求在購買產品或出貨等一些環節,需要發送寄件提醒或者說每周一讓系統自動採集資料發送一封E-mail,因此我也就找來相關資料,寫了一個Demo分享給大家,大家共同學習學習。
通過.Net FrameWork 2.0下提供的“System.Net.Mail”可以輕鬆的實現,本文列舉了3種途徑來發送:
1.通過Localhost;
2.通過普通SMTP;
3.通過SSL的SMTP;
下面一個一個來說:
[html]
  
   public void SendMailLocalhost()  
{  
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();  
msg.To.Add("[email protected]");  
msg.To.Add("[email protected]");  
/* msg.To.Add("[email protected]");  
* msg.To.Add("[email protected]");  
* msg.To.Add("[email protected]");可以發送給多人  
*/  
msg.CC.Add([email protected]);  
/*  
* msg.CC.Add("[email protected]");  
* msg.CC.Add("[email protected]");可以抄送給多人  
*/  
msg.From = new MailAddress("[email protected]", "AlphaWu", 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.Host = "localhost";  
object userState = msg;  
try  
{  
client.SendAsync(msg, userState);  
//簡單一點兒可以client.Send(msg);  
MessageBox.Show("發送成功");  
}  
catch (System.Net.Mail.SmtpException ex)  
{  
MessageBox.Show(ex.Message, "發送郵件出錯");  
}  

    public void SendMailLocalhost()  
{  
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();  
msg.To.Add("[email protected]");  
msg.To.Add("[email protected]");  
/* msg.To.Add("[email protected]");  
* msg.To.Add("[email protected]");  
* msg.To.Add("[email protected]");可以發送給多人  
*/  
msg.CC.Add([email protected]);  
/*  
* msg.CC.Add("[email protected]");  
* msg.CC.Add("[email protected]");可以抄送給多人  
*/  
msg.From = new MailAddress([email protected], "dulei", 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.Host = "localhost";  
object userState = msg;  
try  
{  
client.SendAsync(msg, userState);  
//簡單一點兒可以client.Send(msg);  
MessageBox.Show("發送成功");  
}  
catch (System.Net.Mail.SmtpException ex)  
{  
MessageBox.Show(ex.Message, "發送郵件出錯");  
}  

 
2.通過普通SMTP C#代碼如下
[html]
public void SendMailUseZj()    
{    
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();    
msg.To.Add([email protected]);    
msg.To.Add([email protected]);    
/*   
* msg.To.Add("[email protected]");   
* msg.To.Add("[email protected]");   
* msg.To.Add("[email protected]");可以發送給多人   
*/    
msg.CC.Add("[email protected]");    
/*   
* msg.CC.Add("[email protected]");   
* msg.CC.Add("[email protected]");可以抄送給多人   
*/    
msg.From = new MailAddress("[email protected]", "dulei", 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("[email protected]", "userpass");    
//在71info.com註冊的郵箱和密碼    
client.Host = "smtp.71info.com";    
object userState = msg;    
try    
{    
client.SendAsync(msg, userState);    
//簡單一點兒可以client.Send(msg);    
MessageBox.Show("發送成功");    
}    
catch (System.Net.Mail.SmtpException ex)    
{    
MessageBox.Show(ex.Message, "發送郵件出錯");    
}    
}  

 
3.通過SSL的SMTP
[html]
public void SendMailUseGmail()    
{    
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();    
msg.To.Add([email protected]);    
msg.To.Add([email protected]);    
/*   
 msg.To.Add("[email protected]");   
* msg.To.Add("[email protected]");   
* msg.To.Add("[email protected]");可以發送給多人   
*/    
msg.CC.Add([email protected]);    
/*   
* msg.CC.Add("[email protected]");   
* msg.CC.Add("[email protected]");可以抄送給多人   
*/    
msg.From = new MailAddress("boys90.com", "dulei", 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("[email protected]", "password");    
//上述寫你的GMail郵箱和密碼    
client.Port = 587;//Gmail使用的連接埠    
client.Host = "smtp.gmail.com";    
client.EnableSsl = true;//經過ssl加密    
object userState = msg;    
try    
{    
client.SendAsync(msg, userState);    
//簡單一點兒可以client.Send(msg);    
MessageBox.Show("發送成功");    
}    
catch (System.Net.Mail.SmtpException ex)    
{    
MessageBox.Show(ex.Message, "發送郵件出錯");    
}    
}  
 
通過Gmail來發送郵件,成功率極高,幾乎都可以發到,推薦使用,以上的幾種方法,我想已經夠我們做開發的用了。至於我做的Demo我整理好了就分享給大家。
我的獨立部落格90男孩分享網 歡迎大家光臨,我們一起努力學習更多的知識,分享網快樂分享,快樂生活!!

 

C#發送郵件三種方法(Localhost,SMTP,SSL-SMTP)

聯繫我們

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