最近根據公司需求,寫個郵件發送。這里面的傳入的地址信息的參數都是經過加密的,主要是保證用戶信息的安全。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Net.Mail; using System.Text; using System.Web; namespace CalslNum.Helper { /// <summary> ///發送郵件類 /// </summary> public class MailService { /// <summary> /// 發送郵件程序調用方法 SendMail("[email protected]", "某某人", "[email protected]", "你好", "我測試下郵件", "郵箱登錄名", "郵箱密碼", "smtp.126.com", true,); /// </summary> /// <param name="from">發送人郵件地址</param> /// <param name="fromname">發送人顯示名稱</param> /// <param name="to">發送給誰(郵件地址)</param> /// <param name="subject">標題</param> /// <param name="body">內容</param> /// <param name="username">郵件登錄名</param> /// <param name="password">郵件密碼</param> /// <param name="server">郵件服務器 smtp服務器地址</param> /// <param name= "IsHtml "> 是否是HTML格式的郵件 </param> /// <returns>send ok</returns> public static bool SendMail( string from, string fromname, string to, string subject, string body, string server, string username, string password, bool IsHtml) { //郵件發送類 MailMessage mail = new MailMessage(); try { //是誰發送的郵件 mail.From = new MailAddress(from, fromname); //發送給誰 mail.To.Add(to); //標題 mail.Subject = subject; //內容編碼 mail.BodyEncoding = Encoding.Default; //發送優先級 mail.Priority = MailPriority.High; //郵件內容 mail.Body = body; //是否HTML形式發送 mail.IsBodyHtml = IsHtml; //郵件服務器和端口 SmtpClient smtp = new SmtpClient(server, 25); smtp.UseDefaultCredentials = true ; //指定發送方式 smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //發件人身份驗證,否則163 發不了 smtp.UseDefaultCredentials = true ; //指定登錄名和密碼 smtp.Credentials = new System.Net.NetworkCredential(username, password); //超時時間 smtp.EnableSsl = false ; smtp.Timeout = 10000; smtp.Send(mail); return true ; } catch (Exception) { return false ; } finally { mail.Dispose(); } } //讀取指定URL地址的HTML,用來以后發送網頁用 public static string ScreenScrapeHtml( string url) { //讀取stream并且對于中文頁面防止亂碼 StreamReader reader = new StreamReader(System.Net.WebRequest.Create(url).GetResponse().GetResponseStream(), System.Text.Encoding.UTF8); string str = reader.ReadToEnd(); reader.Close(); return str; } //發送plaintxt public static bool SendText( string from, string fromname, string to, string subject, string body, string server, string username, string password) { return SendMail(from, fromname, to, subject, body, server, username, password, false ); } //發送HTML內容 public static bool SendHtml( string from, string fromname, string to, string subject, string body, string server, string username, string password) { return SendMail(from, fromname, to, subject, body, server, username, password, true ); } //發送制定網頁 public static bool SendWebUrl( string from, string fromname, string to, string subject, string server, string username, string password, string url) { //發送制定網頁 return SendHtml(from, fromname, to, subject, ScreenScrapeHtml(url), server, username, password); } //默認發送格式 public static bool SendEmailDefault( string ToEmail, string f_username, string f_pass, string f_times) { StringBuilder MailContent = new StringBuilder(); MailContent.Append( "親愛的×××會員:<br/>" ); MailContent.Append( " 您好!你于" ); MailContent.Append(DateTime.Now.ToString( "yyyy-MM-dd HH:MM:ss" )); MailContent.Append( "通過<a href='#'>×××</a>管理中心審請找回密碼。<br/>" ); MailContent.Append( " 為了安全起見,請用戶點擊以下鏈接重設個人密碼:<br/><br/>" ); string url = "http://www.×××.×××/SignIn/Rest?u=" + f_username + "&s=" + f_pass + "&t=" + f_times; 114 MailContent.Append( "<a href='" + url + "'>" + url + "</a><br/><br/>" ); 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
2
3
|
<add key= "EmailName" value= "××××@163.com" /> <add key= "EmailPass" value= "××××" /> <add key= "EmailService" value= "smtp.163.com" /> |
//說明: 這里面的"EmailService"得與你自己設置郵箱的smtp/POP3/...服務要相同, 大部分是根據@后面的進行配置。我是用163郵箱配置的。 可以根據自己需要自己配置。
后臺調用的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
public ActionResult SendEmail( string EmailName) { EmailName = Helper.FI_DesTools.DesDecrypt(EmailName); if (!Regex.IsMatch(EmailName, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" )) { return Content( "0" ); } string f_username = "" ; string f_pass = "" ; string f_times = Helper.FI_DesTools.DesEncrypt(DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss" )); List<user> list = (from a in users where a.emailaddress == EmailName select a).ToList(); if (list.Count > 0) { f_username = Helper.FI_DesTools.DesEncrypt(list[0].×××); f_pass = Helper.FI_DesTools.DesEncrypt(list[0].×××); bool flag = Helper.MailService.SendEmailDefault(EmailName, “×××”,“×××”, “×××”); //這里面的參數根據自己需求自己定,最好進行加密 if (flag) { return Content( "true" ); } else { return Content( "false" ); } } else { return Content( "false" ); } } |
發送完郵件效果圖如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_32915337/article/details/62428642