1. 程式人生 > >C#實現發送郵件的三種方法

C#實現發送郵件的三種方法

thumbnail catch plugins () listbox 幫助 哈希 .text sbo

本文實例講述了C#實現發送郵件的三種方法。分享給大家供大家參考。具體方法分析如下:


一、問題:

最近公司由於一個R&I;項目的需要,用戶要求在購買產品或出貨等一些環節,需要發送郵件提醒或者說每周一讓系統自動采集數據發送一封E-mail,因此我也就找來相關資料,寫了一個Demo分享給大家,大家共同學習學習。


二、實現代碼:

通過.Net FrameWork 2.0下提供的“System.Net.Mail”可以輕松的實現,本文列舉了3種途徑來發送:

1.通過Localhost;

2.通過普通SMTP;

3.通過SSL的SMTP;

下面一個一個來說:


代碼如下:

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#代碼如下


代碼如下:

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


代碼如下:

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來發送郵件,成功率極高,幾乎都可以發到,推薦使用,以上的幾種方法,我想已經夠我們做開發的用了。

希望本文所述對大家的C#程序設計有所幫助。

除聲明外,跑步客文章均為原創,轉載請以鏈接形式標明本文地址
C#實現發送郵件的三種方法

本文地址: http://www.paobuke.com/develop/c-develop/pbk23309.html






相關內容

技術分享圖片C#編寫COM組件的方法分析技術分享圖片WPF?¢D?á?ììoíí¨????°′?¥?ùê?′ú??·??í技術分享圖片C#實現簡單獲取掃碼槍信息代碼技術分享圖片C#使用Protocol Buffer(ProtoBuf)進行Unity中的Socket通信
技術分享圖片基於C#實現網絡爬蟲 C#抓取網頁Html源碼技術分享圖片C#計算字符串哈希值(MD5、SHA)的方法小結技術分享圖片C#實現Zip壓縮目錄中所有文件的方法技術分享圖片C#保存listbox中數據到文本文件的方法

C#實現發送郵件的三種方法