C#发送邮件

 

  
  
  
  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Net;  
  5. using System.Net.Mail;  
  6. using System.Net.Mime;  
  7. namespace BoyanCms.Inc  
  8. {  
  9.     public class mail  
  10.     {  
  11.         private  MailMessage message;  
  12.         private  SmtpClient smtp;  
  13.         private  string password;  
  14.         private string smtpserver;  
  15.         /// <summary>  
  16.         ///   
  17.         /// </summary>  
  18.         /// <param name="ToMailAddr">收件人地址</param>  
  19.         /// <param name="FromMailAddr">发件人地址</param>  
  20.         /// <param name="MailBody">邮件内容</param>  
  21.         /// <param name="MailSubject">邮件主题</param>  
  22.         /// <param name="password">发送密码</param>  
  23.         public mail(string ToMailAddr,string FromMailAddr,string MailBody,string MailSubject,string SmtpSrv,string SmtpPassword)  
  24.         {  
  25.             message = new MailMessage();  
  26.             message.To.Add(ToMailAddr);  
  27.             message.From = new System.Net.Mail.MailAddress(FromMailAddr);  
  28.             message.Subject = MailSubject;  
  29.             message.Body = MailBody;  
  30.             message.IsBodyHtml = true;  
  31.             message.BodyEncoding = System.Text.Encoding.UTF8;  
  32.             message.Priority = System.Net.Mail.MailPriority.Normal;//设置邮件发送优先级普通  
  33.             smtpserver = SmtpSrv;  
  34.             password = SmtpPassword;  
  35.         }  
  36.         /// <summary>  
  37.         /// 添加附件  
  38.         /// </summary>  
  39.         /// <param name="Path"></param>  
  40.         public  void AddAtts(string Path)  
  41.         {  
  42.             string[] PathArr = Path.Split(',');  
  43.             Attachment Data;  
  44.             ContentDisposition disposition = null;  
  45.             for (int i = 0; i < PathArr.Length; i++)  
  46.             {  
  47.                 Data = new Attachment(PathArr[i], MediaTypeNames.Application.Octet);//实例化附件  
  48.                 disposition.CreationDate = System.IO.File.GetCreationTime(PathArr[i]);//获取附件的创建日期   
  49.                 disposition.ReadDate = System.IO.File.GetLastAccessTime(PathArr[i]);///获取附 件的读取日期    
  50.                 message.Attachments.Add(Data);//添加到附件中  
  51.             }  
  52.         }  
  53.         /// <summary>  
  54.         /// 异步发送邮件  
  55.         /// </summary>  
  56.         /// <param name="CompletedMethod"></param>  
  57.         public  void SendAsync(SendCompletedEventHandler CompletedMethod)  
  58.         {  
  59.             if (message != null)  
  60.             {  
  61.                 smtp = new SmtpClient();  
  62.                 smtp.Credentials = new System.Net.NetworkCredential(message.From.Address, password);//验证发件人身份  
  63.                 smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;//电子邮件通过网络发送到smtp服务器  
  64.                 if (smtpserver != "")  
  65.                 {  
  66.  
  67.                     smtp.Host = smtpserver;  
  68.                 }  
  69.                 else 
  70.                 {  
  71.                     smtp.Host = "smtp." + message.From.Host;  
  72.                 }  
  73.                 smtp.SendCompleted += new SendCompletedEventHandler(CompletedMethod);  
  74.                 smtp.SendAsync(message, message.Body);  
  75.             }  
  76.         }  
  77.         /// <summary>  
  78.         /// 发送邮件  
  79.         /// </summary>  
  80.         public  void Send()  
  81.         {  
  82.             if (message != null)  
  83.             {  
  84.                 smtp = new SmtpClient();  
  85.                 smtp.Credentials = new System.Net.NetworkCredential(message.From.Address, password);  
  86.                 smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;  
  87.                 if (smtpserver != "")  
  88.                 {  
  89.                     smtp.Host = smtpserver;  
  90.  
  91.                 }  
  92.                 else 
  93.                 {  
  94.                     smtp.Host = "smtp." + message.From.Host;  
  95.                 }  
  96.                 smtp.Send(message);  
  97.             }  
  98.         }  
  99.           
  100.     }  
  101. }  

 

本文出自 “爱尔华-技术成就未来” 博客,转载请与作者联系!

你可能感兴趣的:(邮件,职场,休闲)