项目中经常会涉及发送通知或者提醒邮件的功能,共享一个发送邮件的工具类。
先上代码:
Code
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 using System.Net;
6 using System.Net.Mail;
7
8 namespace Gren.FrameWork.Mail
9 {
10 /// <summary>
11 /// 邮件发送工具类
12 /// </summary>
13 public class MailUtility
14 {
15 private string _host;
16
17 /// <summary>
18 /// SMTP主机
19 /// </summary>
20 public string Host
21 {
22 get { return _host; }
23 set { _host = value; }
24 }
25
26 private int _port;
27
28 /// <summary>
29 /// SMTP主机端口
30 /// </summary>
31 public int Port
32 {
33 get { return _port; }
34 set { _port = value; }
35 }
36
37 private string _from;
38
39 /// <summary>
40 /// 发件人
41 /// </summary>
42 public string From
43 {
44 get { return _from; }
45 set { _from = value; }
46 }
47
48 private string _to;
49
50 /// <summary>
51 /// 收件人
52 /// </summary>
53 public string To
54 {
55 get { return _to; }
56 set { _to = value; }
57 }
58
59 private string _subject;
60
61 /// <summary>
62 /// 邮件主题
63 /// </summary>
64 public string Subject
65 {
66 get { return _subject; }
67 set { _subject = value; }
68 }
69
70 private string _body;
71
72 /// <summary>
73 /// 邮件内容
74 /// </summary>
75 public string Body
76 {
77 get { return _body; }
78 set { _body = value; }
79 }
80
81 private string _cc;
82
83 /// <summary>
84 /// 抄送
85 /// </summary>
86 public string Cc
87 {
88 get { return _cc; }
89 set { _cc = value; }
90 }
91
92 private string _bcc;
93
94 /// <summary>
95 /// 密送
96 /// </summary>
97 public string Bcc
98 {
99 get { return _bcc; }
100 set { _bcc = value; }
101 }
102
103 private string _smtpUsername;
104
105 /// <summary>
106 /// 访问SMTP服务器的用户名
107 /// </summary>
108 public string SmtpUsername
109 {
110 get { return _smtpUsername; }
111 set { _smtpUsername = value; }
112 }
113
114 private string _smtpPassword;
115
116 /// <summary>
117 /// 访问SMTP服务器的用户密码
118 /// </summary>
119 public string SmtpPassword
120 {
121 get { return _smtpPassword; }
122 set { _smtpPassword = value; }
123 }
124
125 /// <summary>
126 /// 地址格式:address,displayName
127 /// </summary>
128 public MailAddress ParseAddress(string addressString)
129 {
130 string[] addressArray = addressString.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
131
132 if (addressArray.Length > 1)
133 {
134 return new MailAddress(addressArray[0], addressArray[1]);
135 }
136 else
137 {
138 return new MailAddress(addressArray[0]);
139 }
140 }
141
142 /// <summary>
143 /// 多个地址格式:address1;address2
144 /// </summary>
145 public void ParseAddressCollection(MailAddressCollection addressCollection, string addressCollectionString)
146 {
147 if (!string.IsNullOrEmpty(addressCollectionString))
148 {
149 MailAddress mailAddress;
150
151 string[] addressCollectionArray = addressCollectionString.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
152
153 foreach (string addressString in addressCollectionArray)
154 {
155 mailAddress = ParseAddress(addressString);
156 addressCollection.Add(mailAddress);
157 }
158 }
159 }
160
161 /// <summary>
162 /// 发送邮件
163 /// </summary>
164 public void Send(bool isHTML)
165 {
166 // Create mail message
167 MailMessage message = new MailMessage();
168
169 message.From = ParseAddress(this._from);
170 ParseAddressCollection(message.To, this._to);
171 ParseAddressCollection(message.CC, this._cc);
172 ParseAddressCollection(message.Bcc, this._bcc);
173
174 message.Subject = this._subject;
175 message.Body = this._body;
176
177 message.BodyEncoding = Encoding.GetEncoding(936);
178 message.IsBodyHtml = isHTML;
179
180 // Send email
181 SmtpClient client = new SmtpClient(this._host, this._port);
182
183 if (!String.IsNullOrEmpty(this._smtpUsername) && !String.IsNullOrEmpty(this._smtpPassword))
184 {
185 client.Credentials = new NetworkCredential(this._smtpUsername, this._smtpPassword);
186 }
187
188 client.EnableSsl = false;
189 client.Send(message);
190
191 }
192
193 /// <summary>
194 /// 发送邮件,默认邮件格式为文本
195 /// </summary>
196 public void Send()
197 {
198 Send(false);
199 }
200 }
201 }
202
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 using System.Net;
6 using System.Net.Mail;
7
8 namespace Gren.FrameWork.Mail
9 {
10 /// <summary>
11 /// 邮件发送工具类
12 /// </summary>
13 public class MailUtility
14 {
15 private string _host;
16
17 /// <summary>
18 /// SMTP主机
19 /// </summary>
20 public string Host
21 {
22 get { return _host; }
23 set { _host = value; }
24 }
25
26 private int _port;
27
28 /// <summary>
29 /// SMTP主机端口
30 /// </summary>
31 public int Port
32 {
33 get { return _port; }
34 set { _port = value; }
35 }
36
37 private string _from;
38
39 /// <summary>
40 /// 发件人
41 /// </summary>
42 public string From
43 {
44 get { return _from; }
45 set { _from = value; }
46 }
47
48 private string _to;
49
50 /// <summary>
51 /// 收件人
52 /// </summary>
53 public string To
54 {
55 get { return _to; }
56 set { _to = value; }
57 }
58
59 private string _subject;
60
61 /// <summary>
62 /// 邮件主题
63 /// </summary>
64 public string Subject
65 {
66 get { return _subject; }
67 set { _subject = value; }
68 }
69
70 private string _body;
71
72 /// <summary>
73 /// 邮件内容
74 /// </summary>
75 public string Body
76 {
77 get { return _body; }
78 set { _body = value; }
79 }
80
81 private string _cc;
82
83 /// <summary>
84 /// 抄送
85 /// </summary>
86 public string Cc
87 {
88 get { return _cc; }
89 set { _cc = value; }
90 }
91
92 private string _bcc;
93
94 /// <summary>
95 /// 密送
96 /// </summary>
97 public string Bcc
98 {
99 get { return _bcc; }
100 set { _bcc = value; }
101 }
102
103 private string _smtpUsername;
104
105 /// <summary>
106 /// 访问SMTP服务器的用户名
107 /// </summary>
108 public string SmtpUsername
109 {
110 get { return _smtpUsername; }
111 set { _smtpUsername = value; }
112 }
113
114 private string _smtpPassword;
115
116 /// <summary>
117 /// 访问SMTP服务器的用户密码
118 /// </summary>
119 public string SmtpPassword
120 {
121 get { return _smtpPassword; }
122 set { _smtpPassword = value; }
123 }
124
125 /// <summary>
126 /// 地址格式:address,displayName
127 /// </summary>
128 public MailAddress ParseAddress(string addressString)
129 {
130 string[] addressArray = addressString.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
131
132 if (addressArray.Length > 1)
133 {
134 return new MailAddress(addressArray[0], addressArray[1]);
135 }
136 else
137 {
138 return new MailAddress(addressArray[0]);
139 }
140 }
141
142 /// <summary>
143 /// 多个地址格式:address1;address2
144 /// </summary>
145 public void ParseAddressCollection(MailAddressCollection addressCollection, string addressCollectionString)
146 {
147 if (!string.IsNullOrEmpty(addressCollectionString))
148 {
149 MailAddress mailAddress;
150
151 string[] addressCollectionArray = addressCollectionString.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
152
153 foreach (string addressString in addressCollectionArray)
154 {
155 mailAddress = ParseAddress(addressString);
156 addressCollection.Add(mailAddress);
157 }
158 }
159 }
160
161 /// <summary>
162 /// 发送邮件
163 /// </summary>
164 public void Send(bool isHTML)
165 {
166 // Create mail message
167 MailMessage message = new MailMessage();
168
169 message.From = ParseAddress(this._from);
170 ParseAddressCollection(message.To, this._to);
171 ParseAddressCollection(message.CC, this._cc);
172 ParseAddressCollection(message.Bcc, this._bcc);
173
174 message.Subject = this._subject;
175 message.Body = this._body;
176
177 message.BodyEncoding = Encoding.GetEncoding(936);
178 message.IsBodyHtml = isHTML;
179
180 // Send email
181 SmtpClient client = new SmtpClient(this._host, this._port);
182
183 if (!String.IsNullOrEmpty(this._smtpUsername) && !String.IsNullOrEmpty(this._smtpPassword))
184 {
185 client.Credentials = new NetworkCredential(this._smtpUsername, this._smtpPassword);
186 }
187
188 client.EnableSsl = false;
189 client.Send(message);
190
191 }
192
193 /// <summary>
194 /// 发送邮件,默认邮件格式为文本
195 /// </summary>
196 public void Send()
197 {
198 Send(false);
199 }
200 }
201 }
202
需要注意的是发件人、收件人、抄送、密送的格式。
邮件地址格式统一为 "somedody@domain.com,displayname",多个地址用分号";"隔开。
发件人只能一个地址,其他可以多个地址。
这个工具类没有实现带附件的功能,不过好在带附件的情况不多,作为一个轻量级的小工具吧。呵呵~