ASP.NET发送邮件
公用方法
/// <summary>
/// 发激活码邮件
/// </summary>
/// <param name="emailInfo">邮件</param>
public void sendEmail(FilmSys.Model.EmailInfo emailInfo)
{
try
{
//编码暂硬性规定为GB2312
Encoding encoding = Encoding.GetEncoding(936);
MailMessage Message = new MailMessage(
new MailAddress(emailInfo.SendEmailAddress, emailInfo.SendEmailName, encoding),//第一个是发信人的地址,第二个参数是显示的发信人 new MailAddress("hfxliyunfang@163.com", "西安集天电子科技服务公司", encoding)
new MailAddress(emailInfo.ReceiveEmailAddress));//收信人邮箱
Message.SubjectEncoding = encoding;
Message.Subject = emailInfo.Subject;//"西安集天电子科技服务公司欢迎您的加入";//标题
Message.BodyEncoding = encoding;
Message.IsBodyHtml = true;//邮箱主体识别html语言
Message.Body = emailInfo.Content;//"请进行邮箱验证来完成您注册的最后一步,点击下面的链接激活您的帐号:<br><a target='_blank' rel='nofollow' style='color: #0041D3; text-decoration: underline' href='http://www.XXX.com/regedit/regeditOK.aspx?code=" + numCode + "&uid=" + cu_name + "'>http://www.XXX.com.cn/regedit/regeditOK.aspx?code=" + numCode + "&uid=" + cu_name + "</a>"; //邮件主体
SmtpClient smtpClient = new SmtpClient(emailInfo.SmtpClient);//信箱服务器 "smtp.163.com"
smtpClient.Credentials = new NetworkCredential(emailInfo.SendEmailAddress, emailInfo.SendEmailPwd);//信箱的用户名和密码
smtpClient.Timeout = 999999;
smtpClient.Send(Message);
}
catch (Exception)
{
//throw new Exception(ex.Message);
}
}
实体类
public class EmailInfo
{
private string sendEmailAddress;
public string SendEmailAddress
{
get { return sendEmailAddress; }
set { sendEmailAddress = value; }
}
private string sendEmailName;
public string SendEmailName
{
get { return sendEmailName; }
set { sendEmailName = value; }
}
private string sendEmailPwd;
public string SendEmailPwd
{
get { return sendEmailPwd; }
set { sendEmailPwd = value; }
}
private string receiveEmailAddress;
public string ReceiveEmailAddress
{
get { return receiveEmailAddress; }
set { receiveEmailAddress = value; }
}
private string subject;
public string Subject
{
get { return subject; }
set { subject = value; }
}
private string content;
public string Content
{
get { return content; }
set { content = value; }
}
private string smtpClient;
public string SmtpClient
{
get { return smtpClient; }
set { smtpClient = value; }
}
private string numCode;
public string NumCode
{
get { return numCode; }
set { numCode = value; }
}
private string userid;
public string Userid
{
get { return userid; }
set { userid = value; }
}
}
调用方法 加上try catch()
string userEmail = dt.Rows[i]["userEmail"].ToString();
//发送邮件
FilmSys.Model.EmailInfo emailInfo = new FilmSys.Model.EmailInfo();
emailInfo.SendEmailAddress = "newmoviess@163.com";//发件人地址
emailInfo.SendEmailName = "云轩阁电影网";//发件人名称
emailInfo.SendEmailPwd = "mv123456";//发件人密码
emailInfo.ReceiveEmailAddress = userEmail;//收件人地址
emailInfo.SmtpClient = "smtp.163.com";//邮箱服务器smtp.163.com smtp.qq.com
emailInfo.Subject = this.EmailTitle.Text;//邮件标题
emailInfo.NumCode = "";//自动生成的验证号
emailInfo.Userid = "";//用户id
emailInfo.Content = this.CKfilmContent.Text;//邮件内容 可以使用html
sendEmail(emailInfo);