c#与com


  1 using System; 
  2 using System.Net; 
  3 using System.Net.Mail; 
  4 using System.Text; 
  5 using System.ComponentModel; 
  6 
  7 namespace Tmiqpl.Net 
  8 
  9     /// <summary> 
 10     /// 简单邮件传输协议 
 11     /// author: Tmiqpl 
 12     /// </summary> 
 13     public class SMTP : IDisposable 
 14     { 
 15         #region 预定义 
 16         private string server = null
 17         private string username = null
 18         private string password = null
 19         private int port = 25
 20         private bool network = true
 21         private bool ishtml = true
 22         private string subject = null
 23         private string body = null
 24         private string from = null
 25         private string to = null
 26         private string errortext = null
 27         private string[] attachement = null
 28         private Encoding bodyencoding = Encoding.Default; 
 29         private Encoding subjectencoding = Encoding.Default; 
 30         private Component conponent = new Component(); 
 31         private bool disposed = false
 32         private string[] cc = null
 33         private string[] bcc = null
 34         #endregion 
 35         /// <summary> 
 36         /// 初始化邮件传输协议 
 37         /// </summary> 
 38         /// <param name="servername">邮件服务器地址</param> 
 39         public SMTP(string servername) 
 40             : this(servername, 25
 41         { 
 42         } 
 43         /// <summary> 
 44         /// 初始化邮件传输协议 
 45         /// </summary> 
 46         /// <param name="servername">邮件服务器地址</param> 
 47         /// <param name="prot">端口</param> 
 48         public SMTP(string servername, int prot) 
 49             : this(servername, prot, nullnull
 50         { 
 51         } 
 52         /// <summary> 
 53         /// 初始化邮件传输协议 
 54         /// </summary> 
 55         /// <param name="servername">邮件服务器地址</param> 
 56         /// <param name="prot">端口</param> 
 57         /// <param name="username">用户名</param> 
 58         /// <param name="password">密码</param> 
 59         public SMTP(string servername, int prot, string username, string password) 
 60         { 
 61             this.ServerName = servername; 
 62             this.port = prot; 
 63             this.UserName = username; 
 64             this.PassWord = password; 
 65         } 
 66         ~SMTP() 
 67         { 
 68             this.Dispose(false); 
 69         } 
 70 
 71         protected void Dispose(bool disposed) 
 72         { 
 73             if (!this.disposed && disposed) 
 74             { 
 75                 conponent.Dispose(); 
 76             } 
 77             this.disposed = true
 78         } 
 79         /// <summary> 
 80         /// 发送 
 81         /// </summary> 
 82         public bool Send() 
 83         { 
 84             SmtpClient _client; 
 85             if (this.ServerName == null
 86             { 
 87                 this.errortext = "(SMTP) 服务器未定义"
 88                 return false
 89             } 
 90             _client = new SmtpClient(this.server, this.port); 
 91             _client.UseDefaultCredentials = false
 92             if ((this.username == null|| (this.password == null)) 
 93             { 
 94                 _client.Credentials = CredentialCache.DefaultNetworkCredentials; 
 95             } 
 96             else 
 97             { 
 98                 _client.Credentials = new NetworkCredential(this.username, this.password); 
 99             } 
100             if (this.network) 
101             { 
102                 _client.DeliveryMethod = SmtpDeliveryMethod.Network; 
103             } 
104             else 
105             { 
106                 _client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis; 
107             } 
108             if (this.from == null
109             { 
110                 this.errortext = "发件人地址未定义!"
111                 return false
112             } 
113             if (this.to == null
114             { 
115                 this.errortext = "收件人人地址未定义!"
116                 return false
117             } 
118             MailAddress from = new MailAddress(this.from); 
119             MailAddress to = new MailAddress(this.to); 
120             MailMessage msg = new MailMessage(from, to); 
121             if (this.subject == null
122             { 
123                 this.errortext = "邮件主题未定义!"
124                 return false
125             } 
126             msg.Subject = this.subject; 
127             msg.Body = this.body; 
128             if (this.cc != null
129             { 
130                 if (this.cc.Length > 0
131                 { 
132                     foreach (string cc in this.cc) 
133                     { 
134                         msg.CC.Add(cc); 
135                     } 
136                 } 
137             } 
138             if (this.bcc != null
139             { 
140                 if (this.bcc.Length > 0
141                 { 
142                     foreach (string bcc in this.bcc) 
143                     { 
144                         msg.Bcc.Add(bcc); 
145                     } 
146                 } 
147             } 
148             if (this.attachement != null
149             { 
150                 if (this.attachement.Length > 0
151                 { 
152                     foreach (string att in this.attachement) 
153                     { 
154                         msg.Attachments.Add(new Attachment(att)); 
155                     } 
156                 } 
157             } 
158             msg.BodyEncoding = this.bodyencoding; 
159             msg.SubjectEncoding = this.subjectencoding; 
160             msg.IsBodyHtml = this.ishtml; 
161             try 
162             { 
163                 _client.Send(msg); 
164                 return true
165             } 
166             catch (Exception e) 
167             { 
168                 this.errortext = e.Message; 
169                 return false
170             } 
171             finally 
172             { 
173                 msg.Dispose(); 
174             } 
175         } 
176         #region 属性 
177         /// <summary> 
178         /// 获取或设置 SMTP 服务器地址 
179         /// </summary> 
180         public string ServerName 
181         { 
182             get { return this.server; } 
183             set { this.server = value; } 
184         } 
185         /// <summary> 
186         /// 获取或设置登陆 SMTP 服务器的用户名 
187         /// </summary> 
188         public string UserName 
189         { 
190             get { return this.username; } 
191             set { this.username = value; } 
192         } 
193         /// <summary> 
194         /// 获取或设置登陆 SMTP 服务器的密码 
195         /// </summary> 
196         public string PassWord 
197         { 
198             get { return this.password; } 
199             set { this.password = value; } 
200         } 
201         /// <summary> 
202         /// 获取或设置 SMTP 端口 
203         /// </summary> 
204         public int Port 
205         { 
206             get { return this.port; } 
207             set { this.port = value; } 
208         } 
209         /// <summary> 
210         /// 获取或设置是否通过网络 SMTP 服务器发送 
211         /// </summary> 
212         public bool IsNetwork 
213         { 
214             get { return this.network; } 
215             set { this.network = value; } 
216         } 
217         /// <summary> 
218         /// 获取或设置邮件内容是否以 HTML 格式发送 
219         /// </summary> 
220         public bool IsHtml 
221         { 
222             get { return this.ishtml; } 
223             set { this.ishtml = value; } 
224         } 
225         /// <summary> 
226         /// 获取或设置邮件内容 
227         /// </summary> 
228         public string Body 
229         { 
230             get { return this.body; } 
231             set { this.body = value; } 
232         } 
233         /// <summary> 
234         /// 获取或设置邮件主题行 
235         /// </summary> 
236         public string Subject 
237         { 
238             get { return this.subject; } 
239             set { this.subject = value; } 
240         } 
241         /// <summary> 
242         /// 获取或设置邮件发送人 
243         /// </summary> 
244         public string From 
245         { 
246             get { return this.from; } 
247             set { this.from = value; } 
248         } 
249         /// <summary> 
250         /// 获取或设置邮件接收人 
251         /// </summary> 
252         public string To 
253         { 
254             get { return this.to; } 
255             set { this.to = value; } 
256         } 
257         /// <summary> 
258         /// 获取邮件发送失败后的信息描述 
259         /// </summary> 
260         public string ErrorText 
261         { 
262             get { return this.errortext; } 
263         } 
264         /// <summary> 
265         /// 获取或设置邮件附件 
266         /// </summary> 
267         public string[] Attachement 
268         { 
269             get { return this.attachement; } 
270             set { this.attachement = value; } 
271         } 
272         /// <summary> 
273         /// 获取或设置邮件抄送地址 
274         /// </summary> 
275         public string[] Cc 
276         { 
277             get { return this.cc; } 
278             set { this.cc = value; } 
279         } 
280         /// <summary> 
281         /// 获取或设置邮件密送地址 
282         /// </summary> 
283         public string[] Bcc 
284         { 
285             get { return this.bcc; } 
286             set { this.bcc = value; } 
287         } 
288         /// <summary> 
289         /// 获取或设置邮件内容编码格式 
290         /// </summary> 
291         public Encoding BodyEncoding 
292         { 
293             get { return this.bodyencoding; } 
294             set { this.bodyencoding = value; } 
295         } 
296         /// <summary> 
297         /// 获取或设置邮件主题编码格式 
298         /// </summary> 
299         public Encoding SubjectEncoding 
300         { 
301             get { return this.subjectencoding; } 
302             set { this.subjectencoding = value; } 
303         } 
304         #endregion 
305         /// <summary> 
306         /// 释放该对象非托管资源 
307         /// </summary> 
308         public virtual void Dispose() 
309         { 
310             this.Dispose(true); 
311             GC.SuppressFinalize(this); 
312         } 
313     } 
314 

你可能感兴趣的:(com)