昨天晚上没事的时候写的一个发邮件的类... 只是能发送,没有什么经验上的可取之处,老鸟慢飞,菜鸟留步.
   | | using System; |
   | | using System.Data; |
   | | using System.Configuration; |
   | | using System.Web; |
   | | using System.Web.Security; |
   | | using System.Web.UI; |
   | | using System.Web.UI.WebControls; |
   | | using System.Web.UI.WebControls.WebParts; |
   | | using System.Web.UI.HtmlControls; |
   | | using System.Net; |
   | | using System.Net.Mail; |
   | | using System.Collections; |
   | | |
   | | namespace SendMail |
   | | { |
   | | public class SendMail |
   | | { |
   | | public static string Title=null; |
   | | public static string Content=null; |
   | | public SendMail() |
   | | { |
   | | } |
   | | public SendMail(string toMail) |
   | | { |
   | | this.toMail = toMail; |
   | | } |
   | | private string toMail=null; |
   | | |
   | | public string ToMail |
   | | { |
   | | get { return toMail; } |
   | | set { toMail = value; } |
   | | } |
   | | |
   | | static SmtpClient smtpClient = new SmtpClient("smtp 服务器"); |
   | | static SendMail() |
   | | { |
   | | smtpClient.Credentials = new System.Net.NetworkCredential("用户名", "密码"); |
   | | } |
   | | public void Send() |
   | | { |
   | | try |
   | | { |
   | | if (SendMail.Title == null) return; |
   | | if (SendMail.Content == null) return; |
   | | if (this.toMail == null) return; |
   | | MailAddress from = new MailAddress("邮件地址", "别名"); |
   | | MailAddress to = new MailAddress(this.toMail); |
   | | MailMessage msg = new MailMessage(from, to); |
   | | msg.Subject = SendMail.Title; |
   | | msg.Body = SendMail.Content; |
   | | msg.BodyEncoding = System.Text.Encoding.Default; |
   | | msg.IsBodyHtml = true; |
   | | smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted); |
   | | smtpClient.Send(msg); |
   | | msg.Dispose(); |
   | | } |
   | | catch { } |
   | | } |
   | | |
   | | void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) |
   | | { |
   | | if (e.Cancelled) |
   | | { |
   | | R = -1; |
   | | } |
   | | else if (e.Error != null) |
   | | { |
   | | R = -2; |
   | | } |
   | | else |
   | | { |
   | | R = 0; |
   | | } |
   | | } |
   | | public int R; |
   | | |
   | | } |
   | | public class Mail |
   | | { |
   | | static Mail() |
   | | { |
   | | |
   | | SendMail.Title = "标题"; |
   | | SendMail.Content = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("~/1.htm")); |
   | | } |
   | | private static ArrayList _mailList=new ArrayList(); |
   | | public static ArrayList mailList |
   | | { |
   | | get |
   | | { |
   | | if (_mailList.Count==0) |
   | | { |
   | | string[] mails = System.IO.File.ReadAllLines(HttpContext.Current.Server.MapPath("~/maillist.txt")); |
   | | _mailList = ArrayList.Adapter(mails); |
   | | } |
   | | return _mailList; |
   | | } |
   | | } |
   | | public static void ClearList() |
   | | { |
   | | _mailList=new ArrayList(); |
   | | } |
   | | public static System.Collections.Generic.List<string> Start(int index, int count) |
   | | { |
   | | System.Collections.Generic.List<string> result = new System.Collections.Generic.List<string>(); |
   | | //从当前开始发送 |
   | | for (int i = 0; i < count; i++) |
   | | { |
   | | //发送 |
   | | if (index + i + 1 > mailList.Count) |
   | | break; |
   | | SendMail sm = new SendMail(mailList[index+i].ToString()); |
   | | result.Add(mailList[index + i].ToString()); |
   | | sm.Send(); |
   | | } |
   | | return result; |
   | | } |
   | | } |
   | | } |
   | | |