1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Data; 
  5. using System.Drawing; 
  6. using System.Text; 
  7. using System.Windows.Forms; 
  8. using System.Net; 
  9. using System.Net.Sockets; 
  10. namespace SendMail 
  11.     public partial class Form1 : Form 
  12.     { 
  13.         public Form1() 
  14.         { 
  15.             InitializeComponent(); 
  16.         } 
  17.         private void button1_Click(object sender, EventArgs e) 
  18.         { 
  19.             for (int i = 0; i < 20; i++) 
  20.             { 
  21.                 string[] recs = rec.Text.Split(';'); 
  22.                 bool isyes = SendMail(server.Text, 25, this.txt_account.Text, recs, this.subject.Text, this.content.Text, truethis.txt_pwd.Text); 
  23.             } 
  24.             //if (isyes) 
  25.             //{ 
  26.             //    MessageBox.Show("邮件发送成功"); 
  27.             //} 
  28.         } 
  29.         NetworkStream ns; 
  30.         /// <summary> 
  31.         ///  
  32.         /// </summary> 
  33.         /// <param name="smtpServer">smtp服务器</param> 
  34.         /// <param name="point">发送端口</param> 
  35.         /// <param name="_from">发件人</param> 
  36.         /// <param name="_tos">收件人</param> 
  37.         /// <param name="_subject">主题</param> 
  38.         /// <param name="_content">内容</param> 
  39.         /// <param name="isESMTP">是否需要身份验证</param>        
  40.         /// <param name="userPWD">发件人密码</param>        
  41.         /// <returns></returns> 
  42.         public bool SendMail(string smtpServer, int point, string _from, string[] _tos, string _subject, string _content, bool isESMTP, string userPWD) 
  43.         { 
  44.             try 
  45.             { 
  46.                 TcpClient smtpClient = new TcpClient(); 
  47.                 IPAddress ip = Dns.GetHostAddresses(smtpServer)[0]; 
  48.                 smtpClient.Connect(ip, point);//连接服务器 
  49.                 ns = smtpClient.GetStream(); 
  50.                 if (SMTPReceive() != 220) 
  51.                     return false
  52.                 Send("HELO\r\n"); 
  53.                 if (SMTPReceive() != 250) 
  54.                     return false
  55.                 //身份验证 
  56.                 if (isESMTP) 
  57.                 { 
  58.                     Send("AUTH LOGIN\r\n"); 
  59.                     if (SMTPReceive() != 334) 
  60.                         return false
  61.                     string uname = _from.Substring(0, _from.IndexOf('@')); 
  62.                     Send(ConvertToBase64(uname) + "\r\n"); 
  63.                     if (SMTPReceive() != 334) 
  64.                         return false
  65.                     Send(ConvertToBase64(userPWD) + "\r\n"); 
  66.                     if (SMTPReceive() != 235) 
  67.                         return false
  68.                 } 
  69.                 Send("Mail From:<" + _from + ">\r\n");//发件人 
  70.                 if (SMTPReceive() != 250) 
  71.                     return false
  72.                 if (_tos.Length < 0) 
  73.                     return false
  74.                 foreach (string to in _tos) 
  75.                 { 
  76.                     Send("RCPT TO:<" + to + ">\r\n"); 
  77.                     if (SMTPReceive() != 250) 
  78.                         return false
  79.                 } 
  80.                 Send("DATA \r\n"); 
  81.                 if (SMTPReceive() != 354) 
  82.                     return false
  83.                 StringBuilder content = new StringBuilder(); 
  84.                 content.Append("From:" + _from + "\r\n"); 
  85.                 if (_tos.Length <= 0) 
  86.                     return false
  87.                 foreach (string to in _tos) 
  88.                 { 
  89.                     content.AppendFormat("To:{0}\r\n", to); 
  90.                 } 
  91.                 //邮件标题 
  92.                 content.AppendFormat("Subject:{0}\r\n", _subject); 
  93.                 //邮件编码格式 
  94.                 content.AppendFormat("Charset=\"{0}\"\r\n""gb2312"); 
  95.                 content.Append("\r\n"); 
  96.                 content.Append(_content);//追加发送的内容 
  97.                 content.Append("\r\n.\r\n"); 
  98.                 Send(content.ToString()); 
  99.                 if (SMTPReceive() != 250) 
  100.                     return false
  101.                 else 
  102.                     return true
  103.             } 
  104.             catch (Exception exc) 
  105.             { 
  106.                 return false
  107.             } 
  108.         } 
  109.         /// <summary> 
  110.         /// 将string型转换为Base64 
  111.         /// </summary> 
  112.         /// <param name="str"></param> 
  113.         /// <returns></returns> 
  114.         string ConvertToBase64(string str) 
  115.         { 
  116.             byte[] data = Encoding.UTF8.GetBytes(str); 
  117.             return Convert.ToBase64String(data); 
  118.         } 
  119.         /// <summary> 
  120.         /// 发送消息至服务器  
  121.         /// </summary> 
  122.         /// <param name="msg"></param> 
  123.         public void Send(string msg) 
  124.         { 
  125.             byte[] data = Encoding.UTF8.GetBytes(msg); 
  126.             ns.Write(data, 0, data.Length); 
  127.             ns.Flush(); 
  128.         } 
  129.         public int SMTPReceive() 
  130.         { 
  131.             try 
  132.             { 
  133.                 byte[] data = new byte[1024]; 
  134.                 int size = ns.Read(data, 0, 1024); 
  135.                 string result = Encoding.UTF8.GetString(data, 0, size); 
  136.                 int index = result.IndexOf(' '); 
  137.                 if (index == -1) 
  138.                     index = result.Length; 
  139.                 result = result.Substring(0, index); 
  140.                 return int.Parse(result); 
  141.             } 
  142.             catch 
  143.             { 
  144.                 return 0; 
  145.             } 
  146.         } 
  147.     }