您的位置:首页 > 编程语言 > PHP开发

SmtpClient发送邮件(用户找回密码)

2011-01-06 12:18 507 查看
/// <summary>

/// 利用.Net自带类(SmtpClient) 发送邮件

/// </summary>

/// <param name="stmpserver">邮件服务器</param>

/// <param name="username">用户名(邮箱名称)</param>

/// <param name="pwd">密码</param>

/// <param name="strfrom">发件人</param>

/// <param name="strto">收件人</param>

/// <param name="subject">主题</param>

/// <param name="body">内容</param>

/// <param name="Mulitaddress">发送多人 收件人的邮箱地址以逗号隔开</param>

/// <param name="attachmentName">发送的附件名称 没有附件则为null or ""</param>

public static void SendEmailToUser(string stmpserver, string Mulitaddress, string attachmentName, string username,string pwd,string strfrom,string strto,string subject,string body)

{

SmtpClient smtp = new SmtpClient();

//发送邮件的方式

smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

//指定邮件服务器

smtp.Host = stmpserver;

smtp.Port = 587;//Gmail QQ stmp ssl加密使用的端口

smtp.EnableSsl = true;//经过ssl加密

//验证发件人的身份 用户名(邮件地址和密码)

smtp.Credentials = new System.Net.NetworkCredential(username, pwd);

//初始化信息(来自 接收人)

MailMessage _mailmessage = new MailMessage(strfrom, strto);

//发送多个人 接收人邮件地址以,隔开

if (!string.IsNullOrEmpty(Mulitaddress))

{

//添加多个收件人 群发

_mailmessage.Bcc.Add(Mulitaddress);

}

//如果发送失败,SMTP 服务器将发送 失败邮件通知

_mailmessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

//优先级

_mailmessage.Priority = MailPriority.High;

//发送主题

_mailmessage.Subject = subject;

//有附件则添加附件

if (!string.IsNullOrEmpty(attachmentName))

{

System.Net.Mail.Attachment attch = new System.Net.Mail.Attachment(attachmentName);

_mailmessage.Attachments.Add(attch);

}

//邮件主题编码

_mailmessage.SubjectEncoding = System.Text.Encoding.UTF8;

//指定发送的格式 (Html)

_mailmessage.IsBodyHtml = true;

//指定发送邮件的编码

_mailmessage.BodyEncoding = System.Text.Encoding.UTF8;

//指定邮件内容

_mailmessage.Body = body;

//发送邮件

try

{

smtp.Send(_mailmessage);

}

catch(Exception ex) {

throw ex;

}

}

//邮件服务器

string smtpserver = DBUtility.GetConfigVariable.GetConfigValue("smtpserver");

//发件人的邮箱名称

string emailuserName = DBUtility.GetConfigVariable.GetConfigValue("emailuserName");

//发件人的邮箱密码

string emailuserpwd = DBUtility.GetConfigVariable.GetConfigValue("emailuserpwd");

//邮箱地址

string emailfrom = DBUtility.GetConfigVariable.GetConfigValue("emailfrom");

//收件人的邮箱地址

string toUser =model.Email;

//附件

//string attachpath = Server.MapPath("pic/20100119020119.gif");

//邮件内容

string bodys = "亲爱的IT数码商城会员:" + model.Username + ":<br />您好!恭喜你己经找回密码";

bodys+="你的密码是<b style="color;red" mce_style="color;red">"+model.Password+"</b>请妥善保管好你的密码!<br/>";

bodys+="你可以通过http://www.ITWeb.cn/UserLogin.aspx/登录你的帐户!尽情享受IT数码城带给你的的乐趣吧!<br/>";

bodys += "你也可以通过:http://www.ITWeb.cn/UpdateUserPwd.aspx/ 修改的你的密码!<br/>";

bodys += "如果有任何疑问请联系管理员:QQ:664618843 Tel:13456946640<br/>";

bodys += "请勿回复此邮件 谢谢合作!";

//发送邮件

DBUtility.SendEmail.SendEmailToUser(smtpserver, null, null, emailuserName, emailuserpwd, "luokuan2010@gmail.com", toUser, "IT数码城用户找回密码", bodys);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: