C#发送邮件
1 using System.Net.Mail;
2 using System.Net;
3 using System.Data;
4 using System.Xml;
5 /// <summary>
6 /// EmailConfig Info
7 /// </summary>
8 public partial class EmailConfig
9 {
10 #region
11 private string _UserName;
12 private string _PSD;
13 private string _host;
14 private int _pop3Port = 0;
15 private int _smtpPort = 0;
16 private bool _enableSsl = false;
17 private bool _needCredentials = true;
18
19 private string _accpetAdd;
20 private string _accpetSubject;
21 private string _accpetBody;
22
23 public EmailConfig()
24 {
25 string filePath = System.Configuration.ConfigurationManager.AppSettings["APPEmailXML"].ToString();
26 Dictionary<string, string> dicts = new Dictionary<string, string>();
27
28 XmlDocument xmlDoc = new XmlDocument();
29 xmlDoc.Load(filePath);
30 XmlNodeList list = xmlDoc.DocumentElement.SelectNodes("/Email/Settings");
31 foreach (XmlNode mainMenu in list)
32 {
33 XmlNodeList mainMenuList = mainMenu.SelectNodes("menu");
34 foreach (XmlNode menu in mainMenuList)
35 {
36 dicts.Add(menu.Attributes["key"].Value, menu.Attributes["value"].Value);
37 }
38 }
39 _UserName = dicts["EUserName"];
40 _PSD = dicts["EPassword"];
41 _host = dicts["EHost"];
42 //_pop3Port = Convert.ToInt32(dicts["EPOP3Port"]);
43 _smtpPort = Convert.ToInt32(dicts["ESMTPPort"]);
44 _enableSsl = Convert.ToBoolean(dicts["EEnableSsl"]);
45 _needCredentials = Convert.ToBoolean(dicts["ENeedCredentials"]);
46 ACCEPTADD = dicts["ACCEPTADD"];
47 ACCEPTSubject = dicts["ACCEPTSubject"];
48 ACCEPTBody = dicts["ACCEPTBody"];
49 }
50
51
52 /// <summary>
53 /// 用户名
54 /// </summary>
55 public string UserName
56 {
57 set { this._UserName = value; }
58 get { return _UserName; }
59 }
60 /// <summary>
61 /// 密码
62 /// </summary>
63 public string Password
64 {
65 set { this._PSD = value; }
66 get { return _PSD; }
67 }
68 /// <summary>
69 /// 主机
70 /// </summary>
71 public string Host
72 {
73 set { this._host = value; }
74 get { return _host; }
75 }
76 /// <summary>
77 /// 接受邮件端口
78 /// </summary>
79 public int POP3Port
80 {
81 set { this._pop3Port = value; }
82 get { return _pop3Port; }
83 }
84 /// <summary>
85 /// 发送邮件端口
86 /// </summary>
87 public int SMTPPort
88 {
89 set { this._smtpPort = value; }
90 get { return _smtpPort; }
91 }
92 /// <summary>
93 /// 是否使用安全套接字层 (SSL) 加密连接
94 /// </summary>
95 public bool EnableSsl
96 {
97 set { this._enableSsl = value; }
98 get { return _enableSsl; }
99 }
100 /// <summary>
101 /// 是否需要登录验证
102 /// </summary>
103 public bool NeedCredentials
104 {
105 set { this._needCredentials = value; }
106 get { return _needCredentials; }
107 }
108
109 /// <summary>
110 /// 接受邮件地址,多个邮件以“,”分割
111 /// </summary>
112 public string ACCEPTADD
113 {
114 set { this._accpetAdd = value; }
115 get
116 {
117 //将输入的逗号替换为英文逗号
118 _accpetAdd = System.Text.RegularExpressions.Regex.Replace(_accpetAdd, "[,,]", ",");
119 return _accpetAdd;
120 }
121 }
122
123 /// <summary>
124 /// 接受邮件标题
125 /// </summary>
126 public string ACCEPTSubject
127 {
128 set { this._accpetSubject = value; }
129 get { return _accpetSubject; }
130 }
131 /// <summary>
132 /// 接受邮件内容
133 /// </summary>
134 public string ACCEPTBody
135 {
136 set { this._accpetBody = value; }
137 get { return _accpetBody; }
138 }
139 #endregion
140 }
141 public partial class EmailHelper
142 {
143 private EmailConfig _config = new EmailConfig();
144
145 /// <summary>
146 /// 发送指定的邮件内容
147 /// </summary>
148 /// <param name="Sender">邮件地址</param>
149 public void SendMail()
150 {
151 List<string> lists = new List<string>();
152 if (_config.ACCEPTADD.Length > 0 && _config.ACCEPTADD.Contains(","))
153 {
154 lists.AddRange(_config.ACCEPTADD.Split(','));
155 }
156 else
157 {
158 lists.Add(_config.ACCEPTADD);
159 }
160 MailAddress from = new MailAddress(_config.UserName, "", Encoding.Default);
161
162 foreach (string toAdd in lists)
163 {
164 MailAddress to = new MailAddress(toAdd, "", Encoding.Default);
165
166 MailMessage m = new MailMessage(from, to);
167
168 m.Subject = _config.ACCEPTSubject;
169 m.Body = _config.ACCEPTBody;
170 m.HeadersEncoding = Encoding.Default;
171 m.Priority = MailPriority.High;
172 m.Sender = from;
173
174 SendMail(m, null);
175 }
176 }
177 private void SendMail(MailMessage m)
178 {
179 SendMail(m, null);
180 }
181
182 private void SendMail(MailMessage m, SendCompletedEventHandler completed)
183 {
184 try
185 {
186 SmtpClient client = new SmtpClient();
187 client.Host = _config.Host;
188 client.Port = _config.SMTPPort;
189
190 if (_config != null)
191 {
192 client.EnableSsl = _config.EnableSsl;
193
194 //服务器需要登陆
195 if (_config.NeedCredentials)
196 {
197 client.Credentials = new NetworkCredential(_config.UserName, _config.Password);
198 }
199 }
200
201 m.BodyEncoding = Encoding.UTF8;
202
203 client.Send(m);
204 }
205 catch (Exception ex)
206 {
207 throw ex;
208 }
209 }
210 }