C#,WinForm發送郵件

來源:互聯網
上載者:User

C#  WinForm下實現的發送郵件

案例下載

 

using System;<br />using System.Collections.Generic;<br />using System.ComponentModel;<br />using System.Data;<br />using System.Drawing;<br />using System.Text;<br />using System.Windows.Forms;</p><p>using System.Net;<br />using System.Net.Sockets;</p><p>namespace SendMail<br />{<br /> public partial class Form1 : Form<br /> {<br /> public Form1()<br /> {<br /> InitializeComponent();<br /> }</p><p> private void button1_Click(object sender, EventArgs e)<br /> {<br /> for (int i = 0; i < 20; i++)<br /> {</p><p> string [] recs = rec.Text.Split(';');<br /> bool isyes = SendMail(server.Text, 25, this.txt_account.Text, recs, this.subject.Text, this.content.Text, true, this.txt_pwd.Text);</p><p> }<br /> //if (isyes)<br /> //{<br /> // MessageBox.Show("郵件發送成功");<br /> //}<br /> }<br /> NetworkStream ns;<br /> /// <summary><br /> ///<br /> /// </summary><br /> /// <param name="smtpServer">smtp伺服器</param><br /> /// <param name="point">傳送埠</param><br /> /// <param name="_from">寄件者</param><br /> /// <param name="_tos">收件者</param><br /> /// <param name="_subject">主題</param><br /> /// <param name="_content">內容</param><br /> /// <param name="isESMTP">是否需要身分識別驗證</param><br /> /// <param name="userPWD">寄件者密碼</param><br /> /// <returns></returns><br /> public bool SendMail(string smtpServer, int point, string _from, string[] _tos, string _subject, string _content, bool isESMTP, string userPWD)<br /> {<br /> try<br /> {<br /> TcpClient smtpClient = new TcpClient();<br /> IPAddress ip = Dns.GetHostAddresses(smtpServer)[0];<br /> smtpClient.Connect(ip, point);//串連伺服器<br /> ns = smtpClient.GetStream();<br /> if (SMTPReceive() != 220)<br /> return false;<br /> Send("HELO/r/n");<br /> if (SMTPReceive() != 250)<br /> return false;<br /> //身分識別驗證<br /> if (isESMTP)<br /> {<br /> Send("AUTH LOGIN/r/n");<br /> if (SMTPReceive() != 334)<br /> return false;<br /> string uname=_from.Substring(0,_from.IndexOf('@'));<br /> Send(ConvertToBase64(uname) + "/r/n");<br /> if (SMTPReceive() != 334)<br /> return false;<br /> Send(ConvertToBase64(userPWD) + "/r/n");<br /> if (SMTPReceive() != 235)<br /> return false;<br /> }</p><p> Send("Mail From:<" + _from + ">/r/n");//寄件者<br /> if (SMTPReceive() != 250)<br /> return false;<br /> if (_tos.Length < 0)<br /> return false;<br /> foreach (string to in _tos)<br /> {<br /> Send("RCPT TO:<" + to + ">/r/n");<br /> if (SMTPReceive() != 250)<br /> return false;<br /> }<br /> Send("DATA /r/n");<br /> if (SMTPReceive() != 354)<br /> return false;<br /> StringBuilder content = new StringBuilder();<br /> content.Append("From:" + _from + "/r/n");<br /> if (_tos.Length <= 0)<br /> return false;<br /> foreach (string to in _tos)<br /> {<br /> content.AppendFormat("To:{0}/r/n", to);<br /> }<br /> //郵件標題<br /> content.AppendFormat("Subject:{0}/r/n", _subject);<br /> //郵件編碼格式<br /> content.AppendFormat("Charset=/"{0}/"/r/n", "gb2312");</p><p> content.Append("/r/n");<br /> content.Append(_content);//追加發送的內容<br /> content.Append("/r/n./r/n");<br /> Send(content.ToString());<br /> if (SMTPReceive() != 250)<br /> return false;<br /> else<br /> return true;<br /> }<br /> catch (Exception exc)<br /> {<br /> return false;<br /> }</p><p> }</p><p> /// <summary><br /> /// 將string型轉換為Base64<br /> /// </summary><br /> /// <param name="str"></param><br /> /// <returns></returns><br /> string ConvertToBase64(string str)<br /> {<br /> byte[] data = Encoding.UTF8.GetBytes(str);<br /> return Convert.ToBase64String(data);<br /> }</p><p> /// <summary><br /> /// 發送訊息至伺服器<br /> /// </summary><br /> /// <param name="msg"></param><br /> public void Send(string msg)<br /> {<br /> byte[] data = Encoding.UTF8.GetBytes(msg);<br /> ns.Write(data, 0, data.Length);<br /> ns.Flush();<br /> }</p><p> public int SMTPReceive()<br /> {<br /> try<br /> {<br /> byte[] data = new byte[1024];<br /> int size = ns.Read(data, 0, 1024);<br /> string result = Encoding.UTF8.GetString(data, 0, size);<br /> int index = result.IndexOf(' ');<br /> if (index == -1)<br /> index = result.Length;<br /> result = result.Substring(0, index);<br /> return int.Parse(result);<br /> }<br /> catch<br /> {<br /> return 0;<br /> }<br /> }<br /> }<br />}

聯繫我們

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