C#发送邮件代码

  1. using System;  
  2. using System.Net;  
  3. using System.Net.Mail;  
  4. using System.Net.Mime;  
  5. namespace Fish  
  6. {  
  7.     /**/ 
  8.     ///    
  9.     /// 发送邮件的类   
  10.     ///    
  11.     public class SendMail  
  12.     {  
  13.         private MailMessage mailMessage = new MailMessage();  
  14.         private SmtpClient smtpClient;  
  15.         private string password;//发件人密码   
  16.         /**/ 
  17.         ///    
  18.         /// 设置收件人、发件人信息与邮件内容、标题   
  19.         ///    
  20.         /// 收件人地址   
  21.         /// 发件人地址   
  22.         /// 邮件正文   
  23.         /// 邮件的主题   
  24.         /// 发件人密码   
  25.         public void Mail(string To, string From, string Body, string Title, string Password)  
  26.         {  
  27.             mailMessage.To.Add(To);  
  28.             mailMessage.From = new System.Net.Mail.MailAddress(From);  
  29.             mailMessage.Subject = Title;  
  30.             mailMessage.Body = Body;  
  31.             mailMessage.IsBodyHtml = true;  
  32.             mailMessage.BodyEncoding = System.Text.Encoding.UTF8;  
  33.             mailMessage.Priority = System.Net.Mail.MailPriority.Normal;  
  34.             this.password = Password;  
  35.         }  
  36.         /**/ 
  37.         ///    
  38.         /// 添加附件   
  39.         ///    
  40.         public void Attachments(string Path)  
  41.         {  
  42.             string[] path = Path.Split(',');  
  43.             Attachment data;  
  44.             ContentDisposition disposition;  
  45.             for (int i = 0; i < path.Length; i++)  
  46.             {  
  47.                 data = new Attachment(path[i], MediaTypeNames.Application.Octet);//实例化 附件   
  48.                 disposition = data.ContentDisposition;  
  49.                 disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//获取 附件的创建日期   
  50.                 disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);// 获取附件的修改日期   
  51.                 disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//获取附 件的读取日期   
  52.                 mailMessage.Attachments.Add(data);//添加到附件中 
  53.             }  
  54.         }  
  55.         /**/ 
  56.         ///    
  57.         /// 异步发送邮件   
  58.         ///    
  59.         ///    
  60.         public void SendAsync(SendCompletedEventHandler CompletedMethod)  
  61.         {  
  62.             if (mailMessage != null)  
  63.             {  
  64.                 smtpClient = new SmtpClient();  
  65.                 smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据   
  66.                 smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;  
  67.                 smtpClient.Host = "smtp." + mailMessage.From.Host;  
  68.                 smtpClient.SendCompleted += new SendCompletedEventHandler(CompletedMethod);//注册异步发送邮件完成时的事件   
  69.                 smtpClient.SendAsync(mailMessage, mailMessage.Body);  
  70.             }  
  71.         }  
  72.         /**/ 
  73.         ///    
  74.         /// 发送邮件   
  75.         ///    
  76.         public void Send()  
  77.         {  
  78.             if (mailMessage != null)  
  79.             {  
  80.                 smtpClient = new SmtpClient();  
  81.                 smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据   
  82.                 smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;  
  83.                 smtpClient.Host = "smtp." + mailMessage.From.Host;  
  84.                 smtpClient.Send(mailMessage);  
  85.             }  
  86.         }  
  87.         ///  
  88.         /// 根据索引删除附件 
  89.         ///  
  90.         ///  
  91.         public void DeleteAttachments(int Id)  
  92.         {  
  93.             mailMessage.Attachments.RemoveAt(Id);  
  94.         }  
  95.     }  
  96. }  

 

创新互联建站是专业的大悟网站建设公司,大悟接单;提供网站设计、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行大悟网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

 


文章标题:C#发送邮件代码
文章分享:http://myzitong.com/article/gijjhi.html