1: // --------------------------------------------------------------------------------------------------------------------
2: //
3: // Copyright © 2014 Wedn.Net. All Rights Reserved.
4: //
5: //
6: // 邮件发送助手类 0.10
7: // Verion:0.10
8: // Description:通过SMTP发送邮件
9: //
10: // --------------------------------------------------------------------------------------------------------------------
11: namespace Micua.Infrastructure.Utility
12: {
13: using System;
14: using System.Linq;
15: using System.Net.Mail;
16: using System.Text;
17:
18: ///
19: /// 邮件发送助手类
20: ///
21: ///
22: /// 2013-11-18 18:56 Created By iceStone
23: ///
24: public static class MailHelper
25: {
26: private readonly static string SmtpServer = "smtp.wedn.net";
27: private readonly static int SmtpServerPort = 25;
28: private readonly static bool SmtpEnableSsl = false;
29: private readonly static string SmtpUsername = "server@wedn.net";
30: private readonly static string SmtpDisplayName = "Wedn.Net";
31: private readonly static string SmtpPassword = "2014@itcast";
32:
33: ///
34: /// 发送邮件到指定收件人
35: ///
36: ///
37: /// 2013-11-18 18:55 Created By iceStone
38: ///
39: /// 收件人地址
40: /// 主题
41: /// 正文内容(支持HTML)
42: /// 抄送地址列表
43: ///是否发送成功
44: public static bool Send(string to, string subject, string mailBody, params string[] copyTos)
45: {
46: return Send(new[] { to }, subject, mailBody, copyTos, new string[] { }, MailPriority.Normal);
47: }
48:
49: ///
50: /// 发送邮件到指定收件人
51: ///
52: ///
53: /// 2013-11-18 18:55 Created By iceStone
54: ///
55: /// 收件人地址列表
56: /// 主题
57: /// 正文内容(支持HTML)
58: /// 抄送地址列表
59: /// 密件抄送地址列表
60: /// 此邮件的优先级
61: /// 附件列表
62: ///是否发送成功
63: ///attachments
64: public static bool Send(string[] tos, string subject, string mailBody, string[] ccs, string[] bccs, MailPriority priority, params Attachment[] attachments)
65: {
66: if (attachments == null) throw new ArgumentNullException("attachments");
67: if (tos.Length == 0) return false;
68: //创建Email实体
69: var message = new MailMessage();
70: message.From = new MailAddress(SmtpUsername, SmtpDisplayName);
71: message.Subject = subject;
72: message.Body = mailBody;
73: message.BodyEncoding = Encoding.UTF8;
74: message.IsBodyHtml = true;
75: message.Priority = priority;
76: //插入附件
77: foreach (var attachment in attachments)
78: {
79: message.Attachments.Add(attachment);
80: }
81: //插入收件人地址,抄送地址和密件抄送地址
82: foreach (var to in tos.Where(c => !string.IsNullOrEmpty(c)))
83: {
84: message.To.Add(new MailAddress(to));
85: }
86: foreach (var cc in ccs.Where(c => !string.IsNullOrEmpty(c)))
87: {
88: message.CC.Add(new MailAddress(cc));
89: }
90: foreach (var bcc in bccs.Where(c => !string.IsNullOrEmpty(c)))
91: {
92: message.CC.Add(new MailAddress(bcc));
93: }
94: //创建SMTP客户端
95: var client = new SmtpClient
96: {
97: Host = SmtpServer,
98: Credentials = new System.Net.NetworkCredential(SmtpUsername, SmtpPassword),
99: DeliveryMethod = SmtpDeliveryMethod.Network,
100: EnableSsl = SmtpEnableSsl,
101: Port = SmtpServerPort
102: };
103: //client.SendCompleted += Client_SendCompleted;
104: //try
105: //{
106: //发送邮件
107: client.Send(message);
108: //client.SendAsync(message,DateTime.Now.ToString());
109:
110: //client.Dispose();
111: //message.Dispose();
112: return true;
113: //}
114: //catch (Exception)
115: //{
116: // throw;
117: //}
118: }
119: }
120: }