.NET2.0下发送电子邮件

.net1.1中实现发送邮件比较麻烦,好在到了2.0新增了System.Net.Mail名称空间,发个邮件什么的轻松了一些.

下面是使用C#2.0写的邮件发送代码(哦.对..没有接收邮件的代码).因为太久不写点代码了,所以抓这个来练一下手.注意:这与生产环境下的代码相差甚远,没有做适当的异常处理也没有重构(主要是懒).由于水平实在有限,代码中有不当之处请拍拍砖.

 

 

  1 /******************************************** 
  2      * author:     dylinux
  3      * Date:       2008-5-4  
  4      ********************************************/
  5     public class SmtpMail
  6     {
  7         
  8         private MailMessage _message=null;
  9         private SmtpClient _client=null;
 10         
 11         
 12         public SmtpMail(SmtpClient client)
 13         {
 14             _client=client;
 15         }
 16         
 17         public SmtpMail(string server,string userName,string password)
 18         {
 19             _client=new SmtpClient(server);
 20             _client.Credentials=new NetworkCredential(userName,password);
 21             _client.DeliveryMethod=SmtpDeliveryMethod.Network;
 22         }
 23         
 24         
 25         /*
 26             发送一个简单消息,缺省使用UTF8编码,消息优先级为Normal
 27         */
 28         public void SendSimpleMessage(string from ,string to,string subject,string body)
 29         {
 30             _message=new MailMessage(from,to,subject,body);
 31             _message.SubjectEncoding=Encoding.UTF8;
 32             _message.BodyEncoding=Encoding.UTF8;
 33             _message.Priority = MailPriority.Normal;
 34             
 35             try
 36             {
 37                 _client.Send(_message);
 38             }
 39             catch(Exception ex)
 40             {
 41                 Console.WriteLine(ex.Message);
 42             }
 43                 
 44             
 45             _message.Dispose();
 46             
 47         }
 48         
 49         
 50         /*
 51             发送一个带有附件的简单消息,缺省使用UTF8编码,消息优先级为Normal
 52         */
 53         public void SendSimpleMessageWithAttachment(string from ,string to,string subject,
 54                                                             string body,string[] attachments)
 55         {
 56             _message=new MailMessage(from,to,subject,body);
 57             _message.SubjectEncoding=Encoding.UTF8;
 58             _message.BodyEncoding=Encoding.UTF8;
 59             _message.Priority = MailPriority.Normal;
 60             if(attachments.Length!=0)
 61             
 62             {
 63                 foreach(string attachment in attachments)
 64                 {
 65                     _message.Attachments.Add(new Attachment(attachment));
 66                 }
 67             }
 68             
 69             try
 70             {
 71                 _client.Send(_message);
 72             }
 73             catch(Exception ex)
 74             {
 75                 Console.WriteLine(ex.Message);
 76             }
 77                 
 78             
 79             _message.Dispose();
 80             
 81         }
 82         
 83         /*
 84             发送带有多个收件人的简单消息,缺省使用UTF8编码,消息优先级为Normal
 85         */
 86         public void SendSimpleMessages(string from ,string[] tos,string subject,string body)
 87         {
 88             _message=new MailMessage();
 89             
 90             MailAddress fromAddr=new MailAddress(from);
 91             
 92             if(tos.Length!=0)
 93             {
 94                 foreach(string to in tos)
 95                 {
 96                     
 97                     _message.To.Add(to);
 98                 }
 99             }
100             
101             
102             _message.From=fromAddr;
103             _message.Subject=subject;
104             _message.Body=body;
105             
106             _message.SubjectEncoding=Encoding.UTF8;
107             _message.BodyEncoding=Encoding.UTF8;
108             _message.Priority = MailPriority.Normal;
109             
110             try
111             {
112                 _client.Send(_message);
113             }
114             catch(Exception ex)
115             {
116                 Console.WriteLine(ex.Message);
117             }
118                 
119             
120             _message.Dispose();
121             
122         }
123         
124         /*
125             使用异步方式发送一个简单消息,缺省使用UTF8编码,消息优先级为Normal
126         */
127         public void AsyncSendSimpleMessage(string from ,string to,string subject,string body)
128         {
129             _message=new MailMessage(from,to,subject,body);
130             _message.SubjectEncoding=Encoding.UTF8;
131             _message.BodyEncoding=Encoding.UTF8;
132             _message.Priority = MailPriority.Normal;
133             
134             try
135             {
136                 string userState="Message1";
137                 
138                 _client.SendCompleted+=new SendCompletedEventHandler(SendCompletedCallback);
139                 _client.SendAsync(_message,userState);
140             }
141             catch(Exception ex)
142             {
143                 _client.SendAsyncCancel();
144                 Console.WriteLine(ex.Message);
145             }
146                 
147             
148         }
149         
150         private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
151         {
152           
153             String token = (string) e.UserState;
154            
155             if (e.Cancelled)
156             {
157                  Console.WriteLine("[{0}] Canceld", token);
158             }
159             if (e.Error != null)
160             {
161                  Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
162             } else
163             {
164                 Console.WriteLine("Message Sent!");
165             }
166            
167         }
168 
169     }

 

posted @ 2008-05-10 17:41  落木萧萧  阅读(179)  评论(0)    收藏  举报