asp.net C#發送郵件類

來源:互聯網
上載者:User

標籤: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     }

 

聯繫我們

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