原文:
C# Email邮件发送,功能是密码找回或者重置功能。
1 using System;
2 using System.Collections.Generic;
3 using System.Configuration;
4 using System.IO;
5 using System.Linq;
6 using System.Net.Mail;
7 using System.Text;
8 using System.Web;
9
10 namespace CalslNum.Helper
11 {
12 ///
13 ///发送邮件类
14 ///
15 public class MailService
16 {
17 ///
18 /// 发送邮件程序调用方法 SendMail("abc@126.com", "某某人", "cba@126.com", "你好", "我测试下邮件", "邮箱登录名", "邮箱密码", "smtp.126.com", true,);
19 ///
20 /// 发送人邮件地址
21 /// 发送人显示名称
22 /// 发送给谁(邮件地址)
23 /// 标题
24 /// 内容
25 /// 邮件登录名
26 /// 邮件密码
27 /// 邮件服务器 smtp服务器地址
28 /// 是否是HTML格式的邮件
29 /// send ok
30 public static bool SendMail(string from, string fromname, string to, string subject, string body, string server, string username, string password, bool IsHtml)
31 {
32 //邮件发送类
33 MailMessage mail = new MailMessage();
34 try
35 {
36 //是谁发送的邮件
37 mail.From = new MailAddress(from, fromname);
38 //发送给谁
39 mail.To.Add(to);
40 //标题
41 mail.Subject = subject;
42 //内容编码
43 mail.BodyEncoding = Encoding.Default;
44 //发送优先级
45 mail.Priority = MailPriority.High;
46 //邮件内容
47 mail.Body = body;
48 //是否HTML形式发送
49 mail.IsBodyHtml = IsHtml;
50 //邮件服务器和端口
51 SmtpClient smtp = new SmtpClient(server, 25);
52 smtp.UseDefaultCredentials = true;
53 //指定发送方式
54 smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
55 //发件人身份验证,否则163 发不了
56 smtp.UseDefaultCredentials = true;
57 //指定登录名和密码
58 smtp.Credentials = new System.Net.NetworkCredential(username, password);
59 //超时时间
60 smtp.EnableSsl = false;
61 smtp.Timeout = 10000;
62 smtp.Send(mail);
63 return true;
64 }
65 catch (Exception)
66 {
67 return false;
68 }
69 finally
70 {
71 mail.Dispose();
72 }
73 }
74
75 //读取指定URL地址的HTML,用来以后发送网页用
76 public static string ScreenScrapeHtml(string url)
77 {
78 //读取stream并且对于中文页面防止乱码
79 StreamReader reader = new StreamReader(System.Net.WebRequest.Create(url).GetResponse().GetResponseStream(), System.Text.Encoding.UTF8);
80 string str = reader.ReadToEnd();
81 reader.Close();
82 return str;
83 }
84
85 //发送plaintxt
86 public static bool SendText(string from, string fromname, string to, string subject, string body, string server, string username, string password)
87 {
88 return SendMail(from, fromname, to, subject, body, server, username, password, false);
89 }
90
91 //发送HTML内容
92 public static bool SendHtml(string from, string fromname, string to, string subject, string body, string server, string username, string password)
93 {
94 return SendMail(from, fromname, to, subject, body, server, username, password, true);
95 }
96
97 //发送制定网页
98 public static bool SendWebUrl(string from, string fromname, string to, string subject, string server, string username, string password, string url)
99 {
100 //发送制定网页
101 return SendHtml(from, fromname, to, subject, ScreenScrapeHtml(url), server, username, password);
102
103 }
104 //默认发送格式
105 public static bool SendEmailDefault(string ToEmail,string f_username,string f_pass,string f_times)
106 {
107 StringBuilder MailCOntent= new StringBuilder();
108 MailContent.Append("亲爱的×××会员:
");
109 MailContent.Append(" 您好!你于");
110 MailContent.Append(DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss"));
111 MailContent.Append("通过×××管理中心审请找回密码。
");
112 MailContent.Append(" 为了安全起见,请用户点击以下链接重设个人密码:
");
113 string url = "http://www.×××.×××/SignIn/Rest?u=" + f_username + "&s=" + f_pass + "&t=" + f_times; 114 MailContent.Append("" + url + "
"); 115 MailContent.Append(" (如果无法点击该URL链接地址,请将它复制并粘帖到浏览器的地址输入框,然后单击回车即可。)"); 116 return SendHtml(ConfigurationManager.AppSettings["EmailName"].ToString(), "会员管理中心", ToEmail, "×××找回密码", MailContent.ToString(), ConfigurationManager.AppSettings["EmailService"].ToString(), ConfigurationManager.AppSettings["EmailName"].ToString(), ConfigurationManager.AppSettings["EmailPass"].ToString()); //这是从webconfig中自己配置的。 117 } 118 } 119 }
webconfig配置信息
1 <add key="EmailName" value="××××@163.com"/>
2 <add key="EmailPass" value="××××"/>
3 <add key="EmailService" value="smtp.163.com"/>
4
5 //说明: 这里面的"EmailService"得与你自己设置邮箱的smtp/POP3/...服务要相同, 大部分是根据@后面的进行配置。我是用163邮箱配置的。 可以根据自己需要自己配置。
后台调用的方法
1 public ActionResult SendEmail(string EmailName)
2 {
3 EmailName = Helper.FI_DesTools.DesDecrypt(EmailName);
4 if (!Regex.IsMatch(EmailName, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
5 {
6 return Content("0");
7 }
8 string f_username = "";
9 string f_pass = "";
10 string f_times = Helper.FI_DesTools.DesEncrypt(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
11 List list = (from a in users where a.emailaddress == EmailName select a).ToList();
12 if (list.Count > 0)
13 {
14 f_username = Helper.FI_DesTools.DesEncrypt(list[0].×××);
15 f_pass = Helper.FI_DesTools.DesEncrypt(list[0].×××);
16
17 bool flag = Helper.MailService.SendEmailDefault(EmailName, “×××”,“×××”, “×××”); //这里面的参数根据自己需求自己定,最好进行加密
18 if (flag)
19 {
20 return Content("true");
21 }
22 else
23 {
24 return Content("false");
25 }
26 }
27 else {
28 return Content("false");
29 }
30 }