首先,在web.config的appSettings配置一下。
<appSettings> <add key="FromMail" value="xxx@163.com"/> <add key="MailDomain" value="smtp.163.com"/> <add key="MailServerUser" value="xxx"/> <add key="MailServerPWD" value="xxx"/> </appSetting>
下面是类的方法,类名:Email
///发送找回密码Email ///发送注册成功Email ///发送Email方法 using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Globalization; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using System.Net.Mail; namespace Tools { public class Email { public static bool SendMemberJobEmail(Model.Msg member) { StringBuilder sb = new StringBuilder(); sb.Append("<p>找回密码</p>"); sb.Append("<p>用户名:"+member.mname+"</p>"); sb.Append("<p>密码:"+member.content+"</p>"); string fromMail = ConfigurationManager.AppSettings["FromMail"].ToString(); string toMail = member.email;//ConfigurationManager.AppSettings["ToMail"].ToString(); string server = ConfigurationManager.AppSettings["MailDomain"].ToString(); string userName = ConfigurationManager.AppSettings["MailServerUser"].ToString(); string password = ConfigurationManager.AppSettings["MailServerPWD"].ToString(); string subject = "(科技网站 " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ")"; bool result = SendMail(fromMail, toMail, subject, sb.ToString(), server, userName, password, true); return result; } /// <summary> /// 发送邮件 /// </summary> /// <param name= "server "> smtp地址 </param> /// <param name= "username "> 用户名 </param> /// <param name= "password "> 密码 </param> /// <param name= "from "> 发信人地址 </param> /// <param name= "to "> 收信人地址 </param> /// <param name= "subject "> 邮件标题 </param> /// <param name= "body "> 邮件正文 </param> /// <param name= "IsHtml "> 是否是HTML格式的邮件 </param> public static bool SendMail(string from, string to, string subject, string body, string server, string username, string password, bool IsHtml) { //设置SMTP 验证 SmtpClient mailClient = new SmtpClient(server); if(server == "smtp.gmail.com") { mailClient.Port = 587; mailClient.EnableSsl = true; } //表示当前登陆用户的默认凭据进行身份验证,并且包含用户名密码 mailClient.UseDefaultCredentials = true; //mailClient.Host = "smtp.gmail.com"; mailClient.Credentials = new System.Net.NetworkCredential(username, password); //指定如何发送电子邮件。 //Network 电子邮件通过网络发送到 SMTP 服务器。 //PickupDirectoryFromIis 将电子邮件复制到挑选目录,然后通过本地 Internet 信息服务 (IIS) 传送。 //SpecifiedPickupDirectory 将电子邮件复制到 SmtpClient.PickupDirectoryLocation 属性指定的目录,然后由外部应 用程序传送。 mailClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; //创建邮件对象 MailMessage mailMessage = new MailMessage(from, to, subject, body); //定义邮件正文,主题的编码方式 mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312"); mailMessage.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312"); //获取或者设置一个值,该值表示电子邮件正文是否为HTML mailMessage.IsBodyHtml = IsHtml; //指定邮件的优先级 mailMessage.Priority = MailPriority.Normal; /* // //添加附件 //System.Web.Mail.MailAttachment mailAttachment=new System.Web.Mail.MailAttachment(@ "f:/att.txt "); if (strFileName != " " && strFileName != null) { Attachment data = new Attachment(strFileName); mailMessage.Attachments.Add(data); } */ //发件人身份验证,否则163 发不了 mailClient.Credentials = new System.Net.NetworkCredential(username, password); //发送 //try //{ mailClient.Send(mailMessage); return true; //} //catch(Exception ex) //{ // return false; //} } } }