asp.net C#发送邮件类

时间:2021-11-23 12:37:59

很久前写的一个简单邮件发送类分享给大家:

 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.Mail; /// <summary>
/// 发送 E-mail 帮助类
/// </summary>
public class EmailHelper
{
//发送邮件的帐号
private string from = "";
//接收邮件的帐号
private string to = "";
//smtp服务器地址
private string smtp = "";
//发送邮件帐号的密码
private string fromPWD = "";
private string subject = "";
private string body = ""; public EmailHelper(string sTo, string sSubject, string sBody)
{
//this.from = "Angel_asp@126.com";
//this.fromPWD = "Angelasp.com";
//this.smtp = "mail.126.com"; this.to = sTo;
this.subject = sSubject;
this.body = sBody;
}
public EmailHelper(string sFrom, string sTo, string sSmtp, string sFromPWD, string sSubject, string sBody)
{
//
// TODO: 在此处添加构造函数逻辑
//
this.from = sFrom;
this.to = sTo;
this.smtp = sSmtp;
this.fromPWD = sFromPWD;
this.subject = sSubject;
this.body = sBody;
}
/// <summary>
/// 发送邮件
/// </summary>
public void SendMail()
{
MailAddress mailTOAddr = new MailAddress(this.to);
MailAddress mailFromAddr = new MailAddress(this.from);
MailMessage mail = new MailMessage(mailFromAddr, mailTOAddr);
mail.Subject = this.subject;
mail.IsBodyHtml = true; //mail.
mail.Body = this.body;
SmtpClient smtpMail = new SmtpClient(this.smtp);
smtpMail.Credentials = new System.Net.NetworkCredential(this.from, this.fromPWD);
//smtpMail.EnableSsl = true; //不支持SSL
smtpMail.Send(mail);
}
}