標籤:style blog class code java c
很久前寫的一個簡單郵件發送類分享給大家:
1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Web; 5 using System.Web.Security; 6 using System.Web.UI; 7 using System.Web.UI.WebControls; 8 using System.Web.UI.WebControls.WebParts; 9 using System.Web.UI.HtmlControls;10 using System.Net.Mail;11 12 /// <summary>13 /// 發送 E-mail 協助類14 /// </summary>15 public class EmailHelper16 {17 //發送郵件的帳號18 private string from = "";19 //內送郵件的帳號20 private string to = "";21 //smtp伺服器位址22 private string smtp = "";23 //發送郵件帳號的密碼24 private string fromPWD = "";25 private string subject = "";26 private string body = "";27 28 public EmailHelper(string sTo, string sSubject, string sBody)29 {30 //this.from = "[email protected]";31 //this.fromPWD = "Angelasp.com";32 //this.smtp = "mail.126.com";33 34 this.to = sTo;35 this.subject = sSubject;36 this.body = sBody;37 }38 public EmailHelper(string sFrom, string sTo, string sSmtp, string sFromPWD, string sSubject, string sBody)39 {40 //41 // TODO: 在此處添加建構函式邏輯42 //43 this.from = sFrom;44 this.to = sTo;45 this.smtp = sSmtp;46 this.fromPWD = sFromPWD;47 this.subject = sSubject;48 this.body = sBody;49 }50 /// <summary>51 /// 發送郵件52 /// </summary>53 public void SendMail()54 {55 MailAddress mailTOAddr = new MailAddress(this.to);56 MailAddress mailFromAddr = new MailAddress(this.from);57 MailMessage mail = new MailMessage(mailFromAddr, mailTOAddr);58 mail.Subject = this.subject;59 mail.IsBodyHtml = true;60 61 //mail.62 mail.Body = this.body;63 SmtpClient smtpMail = new SmtpClient(this.smtp);64 smtpMail.Credentials = new System.Net.NetworkCredential(this.from, this.fromPWD);65 //smtpMail.EnableSsl = true; //不支援SSL66 smtpMail.Send(mail);67 }68 }