原文链接
/// <summary> /// 发送邮件 发送激活码 /// </summary> /// <param name="address">接受者地址</param> /// <param name="displayName">接收者姓名</param> /// <param name="signString">激活码</param> public static void RegionSendEmail(string address, string displayName, string signString, string userName)//发激活码邮件部分 { try { Encoding encoding = Encoding.GetEncoding("utf-8"); Configuration config = WebConfigurationManager.OpenWebConfiguration("~/web.config"); MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings"); MailAddress from = new MailAddress(settings.Smtp.From, settings.Smtp.Network.UserName, encoding); MailAddress to = new MailAddress(address, displayName, encoding); MailMessage message = new MailMessage(from, to); message.SubjectEncoding = encoding; message.IsBodyHtml = true; message.Priority = MailPriority.High; message.Subject = "XXXXX邮箱验证激活"; message.BodyEncoding = encoding; StringBuilder sb = new StringBuilder(); sb.Append("您好:"); sb.Append(userName); sb.Append("<br/>请点击下面的连接来验证激活"); sb.Append(string.Format("<br/><a href='http://baidu.com/Index/CheckingEmailBack.aspx?SignString={0}&userName={1}'>", signString, userName)); sb.Append(string.Format("http://baidu.com/Index/CheckingEmailBack.aspx?SignString={0}&userName={1}", signString, userName)); sb.Append("</a>"); //邮件主体 message.Body = sb.ToString(); //服务器 SmtpClient client = new SmtpClient(settings.Smtp.Network.Host); //的用户名和密码 client.Credentials = new NetworkCredential(settings.Smtp.From, settings.Smtp.Network.Password); client.Send(message); } catch (Exception ex) { throw new Exception(ex.Message); } }
我测试后无法成功, 这里特作两点说明
//要使用Configuration类,不仅要using System.Configuration; 还需要在引用中找到添加引用System.Configuration.dll
//要使用WebConfigurationManager,不仅要System.Web.Configuration; 还需要在引用中找到添加引用System.Web.dll