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

來源:互聯網
上載者:User

標籤:des   alt   subject   strong   問題:   儲存   密碼   amp   sendmail   

本文執行個體講述了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#實現發送郵件的三種方法

聯繫我們

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