a.在軟件開發中,我們經常能夠遇到給用戶或者客戶推送郵件,推送郵件也分為很多方式,比如:推送一句話,推送一個網頁等等。那麼在系統開發中我們一般在什麼情況下會使用郵件發送呢?下面我簡單總結了一下:總結不全,純屬於整理封裝的類。
(1):用戶注冊(推送郵件,提示用戶注冊成功,牢記用戶名密碼)
(2):修改密碼(推送郵件,提示用戶密碼修改成功)
(3):下訂單(推送郵件,提示用戶訂單已下)
(4):物流跟蹤(推送郵件,跟蹤物流信息)
(5):廣告推送(推送廣告,提示用戶近來公司近況或者新的商品,產品)
(6):日志監控系統推送(推送錯誤信息,提供給程序員使用)
................................................
b.上面我們說了郵件推送的使用,那麼既然郵件推送這麼頻繁,就需要整理出來一個公用類來給大家使用,下面我就簡單整理了一下公用類,如果大家需要,請git上下載或者復制即可,不能保證百分百實現你的功能,如果不能實現你的功能,請改進。
c.git下載地址:https://github.com/kencery/Common/tree/master/KenceryCommonMethod/Smtp%E9%82%AE%E4%BB%B6%E5%8F%91%E9%80%81
d.代碼整理如下,請您查看.......
a.C#代碼以及如何調用等都在代碼中已注釋,如不會使用,請仔細查看
1 // 源文件頭信息:
2 // <copyright file="Email.cs,EmailHelp.cs">
3 // Copyright(c)2014-2034 Kencery.All rights reserved.
4 // 個人博客:http://www.cnblogs.com/hanyinglong
5 // 創建人:韓迎龍(kencery)
6 // 創建時間:2015-8-18
7 // </copyright>
8
9 using System;
10 using System.Net;
11 using System.Net.Mail;
12 using System.Text;
13
14 namespace KenceryCommonMethod
15 {
16 /// <summary>
17 /// 功能:實現郵件發送,分裝發送郵件的調用方法
18 /// </summary>
19 /// <auther>
20 /// <name>Kencery</name>
21 /// <date>2015-8-18</date>
22 /// </auther>
23 public class Email
24 {
25 #region --------------------字段--------------------
26
27 private string _serviceType = "SMTP";
28 private string _host;
29
30 #endregion
31
32 #region --------------------屬性--------------------
33
34 /// <summary>
35 /// 發送者郵箱
36 /// </summary>
37 public string From { get; set; }
38
39 /// <summary>
40 /// 接收者郵箱列表
41 /// </summary>
42 public string[] To { get; set; }
43
44 /// <summary>
45 /// 抄送者郵箱列表
46 /// </summary>
47 public string[] Cc { get; set; }
48
49 /// <summary>
50 /// 秘抄者郵箱列表
51 /// </summary>
52 public string[] Bcc { get; set; }
53
54 /// <summary>
55 /// 郵件主題
56 /// </summary>
57 public string Subject { get; set; }
58
59 /// <summary>
60 /// 郵件內容
61 /// </summary>
62 public string Body { get; set; }
63
64 /// <summary>
65 /// 是否是HTML格式
66 /// </summary>
67 public bool IsBodyHtml { get; set; }
68
69 /// <summary>
70 /// 附件列表
71 /// </summary>
72 public string[] Attachments { get; set; }
73
74 /// <summary>
75 /// 郵箱服務類型(Pop3,SMTP,IMAP,MAIL等),默認為SMTP
76 /// </summary>
77 public string ServiceType
78 {
79 get { return _serviceType; }
80 set { _serviceType = value; }
81 }
82
83 /// <summary>
84 /// 郵箱服務器,如果沒有定義郵箱服務器,則根據serviceType和Sender組成郵箱服務器
85 /// </summary>
86 public string Host
87 {
88 get
89 {
90 return string.IsNullOrEmpty(_host)
91 ? (this.ServiceType + "." +
92 Sender.Split("@".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[1])
93 : _host;
94 }
95 set { _host = value; }
96 }
97
98 /// <summary>
99 /// 郵箱賬號(默認為發送者郵箱的賬號)
100 /// </summary>
101 public string UserName { get; set; }
102
103 /// <summary>
104 /// 郵箱密碼(默認為發送者郵箱的密碼),默認格式GB2312
105 /// </summary>
106 public string Password { get; set; }
107
108 /// <summary>
109 /// 郵箱優先級
110 /// </summary>
111 public MailPriority MailPriority { get; set; }
112
113 /// <summary>
114 /// 郵件正文編碼格式
115 /// </summary>
116 public Encoding Encoding { get; set; }
117
118 #endregion
119
120 #region ------------------調用方法------------------
121
122 /// <summary>
123 /// 構造參數,發送郵件,使用方法備注:公開方法中調用
124 /// </summary>
125 public void Send()
126 {
127 var mailMessage = new MailMessage();
128
129 //讀取To 接收者郵箱列表
130 if (this.To != null && this.To.Length > 0)
131 {
132 foreach (string to in this.To)
133 {
134 if (string.IsNullOrEmpty(to)) continue;
135 try
136 {
137 mailMessage.To.Add(new MailAddress(to.Trim()));
138 }
139 catch (Exception ex)
140 {
141 throw new Exception(ex.Message);
142 }
143 }
144 }
145 //讀取Cc 抄送者郵件地址
146 if (this.Cc != null && this.Cc.Length > 0)
147 {
148 foreach (var cc in this.Cc)
149 {
150 if (string.IsNullOrEmpty(cc)) continue;
151 try
152 {
153 mailMessage.CC.Add(new MailAddress(cc.Trim()));
154 }
155 catch (Exception ex)
156 {
157 throw new Exception(ex.Message);
158 }
159 }
160 }
161 //讀取Attachments 郵件附件
162 if (this.Attachments != null && this.Attachments.Length > 0)
163 {
164 foreach (var attachment in this.Attachments)
165 {
166 if (string.IsNullOrEmpty(attachment)) continue;
167 try
168 {
169 mailMessage.Attachments.Add(new Attachment(attachment));
170 }
171 catch (Exception ex)
172 {
173 throw new Exception(ex.Message);
174 }
175 }
176 }
177 //讀取Bcc 秘抄人地址
178 if (this.Bcc != null && this.Bcc.Length > 0)
179 {
180 foreach (var bcc in this.Bcc)
181 {
182 if (string.IsNullOrEmpty(bcc)) continue;
183 try
184 {
185 mailMessage.Bcc.Add(new MailAddress(bcc.Trim()));
186 }
187 catch (Exception ex)
188 {
189 throw new Exception(ex.Message);
190 }
191 }
192 }
193 //讀取From 發送人地址
194 try
195 {
196 mailMessage.From = new MailAddress(this.From);
197 }
198 catch (Exception ex)
199 {
200 throw new Exception(ex.Message);
201 }
202
203 //郵件標題
204 Encoding encoding = Encoding.GetEncoding("GB2312");
205 mailMessage.Subject = string.Format("?={0}?B?{1}?=", encoding.HeaderName,
206 Convert.ToBase64String(encoding.GetBytes(this.Subject), Base64FormattingOptions.None));
207 //郵件正文是否為HTML格式
208 mailMessage.IsBodyHtml = this.IsBodyHtml;
209 //郵件正文
210 mailMessage.Body = this.Body;
211 mailMessage.BodyEncoding = this.Encoding;
212 //郵件優先級
213 mailMessage.Priority = this.MailPriority;
214
215 //發送郵件代碼實現
216 var smtpClient = new SmtpClient
217 {
218 Host = this.Host,
219 Credentials = new NetworkCredential(this.UserName, this.Password)
220 };
221 //認證
222 try
223 {
224 smtpClient.Send(mailMessage);
225 }
226 catch (Exception ex)
227 {
228 throw new Exception(ex.Message);
229 }
230 }
231
232 #endregion
233 }
234
235 /// <summary>
236 ///郵件發送接口調用:bool isTrue=EmailInfo.SendEmail(參數,........); 參數解釋參考方法
237 /// <auther>
238 /// <name>Kencery</name>
239 /// <date>2015-8-18</date>
240 /// </auther>
241 /// </summary>
242 public static class EmailInfo
243 {
244 /// <summary>
245 /// 郵件發送方法,傳遞參數(使用中如出現問題,請調試)
246 /// </summary>
247 /// <param name="from">發送者郵箱名稱(從配置文件中讀取,比如:934532778@qq.com)(必填項)</param>
248 /// <param name="to">接收者郵箱列表,可以傳遞多個,使用string[]表示(從配置文件中讀取)(必填項)</param>
249 /// <param name="cc">抄送者郵箱列表,可以傳遞多個,使用string[]表示(從配置文件中讀取)</param>
250 /// <param name="bcc">秘抄者郵箱列表,可以傳遞多個,使用string[]表示(從配置文件中讀取)</param>
251 /// <param name="subject">郵件主題,構造(必填項)</param>
252 /// <param name="body">郵件內容,構造發送的郵件內容,可以發送網頁(必填項)</param>
253 /// <param name="isBodyHtml">是否是HTML格式,true為是,false為否</param>
254 /// <param name="attachments">郵箱附件,可以傳遞多個,使用string[]表示(從配置文件中讀取),可空</param>
255 /// <param name="host">郵箱服務器(從配置文件中讀取,如:smtp@qq.com)(必填項)</param>
256 /// <param name="password">郵箱密碼(從配置文件中讀取,from郵箱的密碼)(必填項)</param>
257 /// <returns>郵件發送成功,返回true,否則返回false</returns>
258 public static bool SendEmail(string from, string[] to, string[] cc, string[] bcc, string subject, string body,
259 bool isBodyHtml, string[] attachments, string host, string password)
260 {
261 //郵箱發送不滿足,限制這些參數必須傳遞
262 if (from == "" || to.Length <= 0 || subject == "" || body == "" || host == "" || password == "")
263 {
264 return false;
265 }
266
267 var emil = new Email
268 {
269 From = @from,
270 To = to,
271 Cc = cc,
272 Bcc = bcc,
273 Subject = subject,
274 Body = body,
275 IsBodyHtml = isBodyHtml,
276 Attachments = attachments,
277 Host = host,
278 Password = password
279 };
280 try
281 {
282 emil.Send();
283 return true;
284 }
285 catch (Exception ex)
286 {
287 throw new Exception(ex.Message);
288 }
289 }
290 }
291 }