C#一个完整的电子邮件操作类

  1     /// <summary>
2 /// 电子邮件操作类。
3 /// </summary>
4 public class Mail
5 {
6 /// <summary>
7 /// 初始化一个电子邮件操作类的空实例。
8 /// </summary>
9 public Mail()
10 {
11 }
12 /// <summary>
13 /// 初始化一个电子邮件操作类的实例。
14 /// </summary>
15 /// <param name="from">发件人的电子邮件地址。</param>
16 /// <param name="fromName">发件人的姓名。</param>
17 /// <param name="recipient">收件人的电子邮件地址。</param>
18 /// <param name="recipientName">收件人的姓名。</param>
19 /// <param name="subject">电子邮件的主题。</param>
20 /// <param name="body">电子邮件的内容。</param>
21 /// <param name="host">电子邮件的服务器地址。</param>
22 /// <param name="port">电子邮件的主机端口号。</param>
23 /// <param name="username">登录电子邮件服务器的用户名。</param>
24 /// <param name="password">登录电子邮件服务器的用户密码。</param>
25 /// <param name="isBodyHtml">邮件的正文是否是HTML格式。</param>
26 /// <param name="file">邮件附件的文件路径。</param>
27 public Mail(string from, string fromName, string recipient, string recipientName, string subject, string body, string host, int port, string username, string password, bool isBodyHtml, string filepath)
28 {
29 this._from = from;
30 this._fromName = fromName;
31 this._recipient = recipient;
32 this._recipientName = recipientName;
33 this._subject = subject;
34 this._body = body;
35 this._serverHost = host;
36 this._serverPort = port;
37 this._username = username;
38 this._password = password;
39 this._isBodyHtml = isBodyHtml;
40 this._attachment.Add(filepath);
41 }
42 /// <summary>
43 /// 初始化一个电子邮件操作类的实例。
44 /// </summary>
45 /// <param name="from">发件人的电子邮件地址。</param>
46 /// <param name="fromName">发件人的姓名。</param>
47 /// <param name="recipient">收件人的电子邮件地址。</param>
48 /// <param name="recipientName">收件人的姓名。</param>
49 /// <param name="subject">电子邮件的主题。</param>
50 /// <param name="body">电子邮件的内容。</param>
51 /// <param name="host">电子邮件的服务器地址。</param>
52 /// <param name="port">电子邮件的主机端口号。</param>
53 /// <param name="username">登录电子邮件服务器的用户名。</param>
54 /// <param name="password">登录电子邮件服务器的用户密码。</param>
55 /// <param name="isBodyHtml">邮件的正文是否是HTML格式。</param>
56 public Mail(string from, string fromName, string recipient, string recipientName, string subject, string body, string host, int port, string username, string password, bool isBodyHtml)
57 : this(from, fromName, recipient, recipientName, subject, body, host, 25, username, password, isBodyHtml, null)
58 {
59 }
60
61 /// <summary>
62 /// 初始化一个电子邮件操作类的实例。
63 /// </summary>
64 /// <param name="from">发件人的电子邮件地址。</param>
65 /// <param name="fromName">发件人的姓名。</param>
66 /// <param name="recipient">收件人的电子邮件地址。</param>
67 /// <param name="recipientName">收件人的姓名。</param>
68 /// <param name="subject">电子邮件的主题。</param>
69 /// <param name="body">电子邮件的内容。</param>
70 /// <param name="host">电子邮件的服务器地址。</param>
71 /// <param name="port">电子邮件的主机端口号。</param>
72 /// <param name="username">登录电子邮件服务器的用户名。</param>
73 /// <param name="password">登录电子邮件服务器的用户密码。</param>
74 /// <param name="isBodyHtml">邮件的正文是否是HTML格式。</param>
75 public Mail(string from, string fromName, string recipient, string recipientName, string subject, string body, string host, string username, string password)
76 : this(from, fromName, recipient, recipientName, subject, body, host, 25, username, password, false, null)
77 {
78 }
79 /// <summary>
80 /// 初始化一个电子邮件操作类的实例(邮件的正文非HTML格式,端口默认25)。
81 /// </summary>
82 /// <param name="from">发件人的电子邮件地址。</param>
83 /// <param name="recipient">收件人的电子邮件地址。</param>
84 /// <param name="subject">电子邮件的主题。</param>
85 /// <param name="body">电子邮件的内容。</param>
86 /// <param name="host">电子邮件的服务器地址。</param>
87 /// <param name="username">登录电子邮件服务器的用户名。</param>
88 /// <param name="password">登录电子邮件服务器的用户密码。</param>
89 /// <param name="isBodyHtml">邮件的正文是否是HTML格式。</param>
90 public Mail(string from, string recipient, string subject, string body, string host, string username, string password)
91 : this(from, null, recipient, null, subject, body, host, 25, username, password, false, null)
92 {
93 }
94 /// <summary>
95 /// 初始化一个电子邮件操作类的实例(邮件的正文非HTML格式,端口默认25)。
96 /// </summary>
97 /// <param name="from">发件人的电子邮件地址。</param>
98 /// <param name="recipient">收件人的电子邮件地址。</param>
99 /// <param name="subject">电子邮件的主题。</param>
100 /// <param name="body">电子邮件的内容。</param>
101 /// <param name="port">电子邮件的主机端口号。</param>
102 /// <param name="host">电子邮件的服务器地址。</param>
103 /// <param name="username">登录电子邮件服务器的用户名。</param>
104 /// <param name="password">登录电子邮件服务器的用户密码。</param>
105 public Mail(string from, string recipient, string subject, string body, string host, int port, string username, string password)
106 : this(from, null, recipient, null, subject, body, host, port, username, password, false, null)
107 {
108 }
109
110 private string _subject;
111 private string _body;
112 private string _from;
113 private string _fromName;
114 private string _recipientName;
115 private string _serverHost;
116 private int _serverPort;
117 private string _username;
118 private string _password;
119 private bool _isBodyHtml;
120 private string _recipient;
121 private List<string> _attachment = new List<string> { };
122
123 /// <summary>
124 /// 获取或设置邮件的主题。
125 /// </summary>
126 public string Subject
127 {
128 get { return this._subject; }
129 set { this._subject = value; }
130 }
131
132 /// <summary>
133 /// 获取或设置邮件的正文内容。
134 /// </summary>
135 public string Body
136 {
137 get { return this._body; }
138 set { this._body = value; }
139 }
140
141 /// <summary>
142 /// 获取或设置发件人的邮件地址。
143 /// </summary>
144 public string From
145 {
146 get { return this._from; }
147 set { this._from = value; }
148 }
149
150 /// <summary>
151 /// 获取或设置发件人的姓名。
152 /// </summary>
153 public string FromName
154 {
155 get { return this._fromName; }
156 set { this._fromName = value; }
157 }
158
159 /// <summary>
160 /// 获取或设置收件人的姓名。
161 /// </summary>
162 public string RecipientName
163 {
164 get { return this._recipientName; }
165 set { this._recipientName = value; }
166 }
167
168 /// <summary>
169 /// 获取或设置收件人的邮件地址。
170 /// </summary>
171 public string Recipient
172 {
173 get { return this._recipient; }
174 set { this._recipient = value; }
175 }
176
177 /// <summary>
178 /// 获取或设置邮件服务器主机地址。
179 /// </summary>
180 public string ServerHost
181 {
182 get { return this._serverHost; }
183 set { this._serverHost = value; }
184 }
185
186 /// <summary>
187 /// 获取或设置邮件服务器的端口号。
188 /// </summary>
189 public int ServerPort
190 {
191 set { this._serverPort = value; }
192 get { return this._serverPort; }
193 }
194
195
196 /// <summary>
197 /// 获取或设置SMTP认证时使用的用户名。
198 /// </summary>
199 public string Username
200 {
201 get { return this._username; }
202 set
203 {
204 if (value.Trim() != "")
205 {
206 this._username = value.Trim();
207 }
208 else
209 {
210 this._username = "";
211 }
212 }
213 }
214
215 /// <summary>
216 /// 获取或设置SMTP认证时使用的密码。
217 /// </summary>
218 public string Password
219 {
220 set { this._password = value; }
221 get { return this._password; }
222 }
223
224 /// <summary>
225 /// 获取或设置指示邮件正文是否为 Html 格式的值。
226 /// </summary>
227 public bool IsBodyHtml
228 {
229 get { return this._isBodyHtml; }
230 set { this._isBodyHtml = value; }
231 }
232
233 /// <summary>
234 /// 获取电子邮件附件。
235 /// </summary>
236 public List<string> Attachment
237 {
238 get { return this._attachment; }
239 set { this._attachment = value; }
240 }
241
242 ///// <summary>
243 ///// 添加一个收件人的邮箱地址。
244 ///// </summary>
245 ///// <param name="mailList">联系人列表。</param>
246 ///// <returns></returns>
247 //public bool AddRecipient(params string[] mailList)
248 //{
249 // this.Recipient = mailList[0].Trim();
250 // return true;
251 //}
252
253 /// <summary>
254 /// 添加电子邮件附件。
255 /// </summary>
256 /// <param name="fileList">文件列表。</param>
257 /// <returns>是否添加成功。</returns>
258 public bool AddAttachment(params string[] fileList)
259 {
260 if (fileList.Length > 0)
261 {
262 foreach (string file in fileList)
263 {
264 if (file != null)
265 {
266 this._attachment.Add(file);
267 }
268 else
269 {
270 return false;
271 }
272 }
273 return true;
274 }
275 else
276 {
277 return false;
278 }
279 }
280 /// <summary>
281 /// 发送电子邮件。
282 /// </summary>
283 /// <returns>是否发送成功。</returns>
284 public bool Send()
285 {
286 //初始化 MailMessage 对象。
287 MailMessage mail = new MailMessage();
288 Encoding encoding = Encoding.GetEncoding("utf-8");
289 mail.From = new MailAddress(this.From, this.FromName, encoding);
290 mail.To.Add(new MailAddress(this.Recipient, this.RecipientName));
291 mail.Subject = this.Subject;
292 mail.IsBodyHtml = this.IsBodyHtml;
293 mail.Body = this.Body;
294 mail.Priority = MailPriority.Normal;
295 mail.BodyEncoding = encoding;
296 if (this.Attachment.Count > 0)
297 {
298 foreach (string file in this.Attachment)
299 {
300 mail.Attachments.Add(new Attachment(file));
301 }
302 }
303 //初始化 SmtpClient 对象。
304 SmtpClient smtp = new SmtpClient();
305 smtp.Host = this.ServerHost;
306 smtp.Port = this.ServerPort;
307 smtp.Credentials = new NetworkCredential(this.Username, this.Password);
308 if (smtp.Port != 25)
309 {
310 smtp.EnableSsl = true;
311 }
312 try
313 {
314 smtp.Send(mail);
315 }
316 catch (SmtpException ex)
317 {
318 string message = ex.Message;
319 return false;
320 }
321 return true;
322 }
323 }


 

你可能感兴趣的:(C#一个完整的电子邮件操作类)