C#基础-邮件发送

2018-06-17 21:29:10来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using System.Net.Mail;
  7 using System.Net.Mime;
  8 using System.Net;
  9 using System.IO;  
 10 
 11 namespace TestForm
 12 {
 13     class Email
 14     {
 15         string smtpHost = string.Empty;
 16         string Sendmailaddress = string.Empty;
 17         string Sendpassword = string.Empty;
 18         string SendDisplayname = string.Empty;
 19         string Recevivemailaddress = string.Empty;
 20         string ReceviveDisplayname = string.Empty;
 21 
 22         /// <summary>
 23         /// 
 24         /// </summary>
 25         /// <param name="host">主机号</param>
 26         /// <param name="sendmailaddress">发送人邮箱地址</param>
 27         /// <param name="sendpassword">发送邮箱密码</param>
 28         /// <param name="sendDisplayname">发送人显示名称</param>
 29         /// <param name="recevivemailaddress">接收人邮箱地址</param>
 30         /// <param name="receviveDisplayname">接收人显示名称</param>
 31         public Email(string host, string sendmailaddress, string sendpassword, string sendDisplayname, string recevivemailaddress, string receviveDisplayname) 
 32         {
 33             smtpHost = host;
 34             Sendmailaddress = sendmailaddress;
 35             Sendpassword = sendpassword;
 36             SendDisplayname = sendDisplayname;
 37             Recevivemailaddress = recevivemailaddress;
 38             ReceviveDisplayname = receviveDisplayname;
 39         }
 40 
 41         /// <summary>
 42         /// 发送邮件功能
 43         /// </summary>
 44         /// <param name="mailsubject">邮件标题</param>
 45         /// <param name="mailbody">邮件主要内容</param>
 46         /// <param name="isadddocument">是否添加附件</param>
 47         /// <param name="documentpath">添加附件的文件路径列表</param>
 48         /// <returns></returns>
 49         public bool Sendmail(string mailsubject, string mailbody, bool isadddocument, List<string> documentpath)
 50         {
 51             bool sendstatus = false;
 52             try 
 53             {
 54                 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(smtpHost);  //确定smtp服务器地址。实例化一个Smtp客户端
 55                 MailAddress from = new MailAddress(Sendmailaddress, SendDisplayname, Encoding.UTF8);//构造一个发件人地址对象
 56                 MailAddress to = new MailAddress(Recevivemailaddress, ReceviveDisplayname, Encoding.UTF8);//构造一个收件人地址对象                
 57                 MailMessage message = new MailMessage(from, to);//构造一个Email的Message对象
 58 
 59                 message.Subject = mailsubject;
 60                 message.SubjectEncoding = Encoding.UTF8;
 61                 message.Body = mailbody;
 62                 message.BodyEncoding = Encoding.UTF8;
 63 
 64                 //设置邮件的信息
 65                 client.DeliveryMethod = SmtpDeliveryMethod.Network;
 66                 message.BodyEncoding = System.Text.Encoding.UTF8;
 67                 message.IsBodyHtml = false;
 68 
 69                 //如果服务器支持安全连接,则将安全连接设为true。
 70                 //如果是gmail则一定要将其设为true
 71                 if (smtpHost == "smpt.gmail.com")
 72                     client.EnableSsl = true;
 73                 else
 74                     client.EnableSsl = false;
 75 
 76                 if (isadddocument == true)
 77                 {
 78                     AddDocument(message, documentpath);
 79                 }
 82                 client.UseDefaultCredentials = false;
 83                 //用户登陆信息
 84                 NetworkCredential myCredentials = new NetworkCredential(Sendmailaddress, Sendpassword);
 85                 client.Credentials = myCredentials;
 86                 //发送邮件
 87                 client.Send(message);
 88                 sendstatus = true; 
 89             }
 90             catch { }
 91             return sendstatus;
 92         }
 93 
 94         /// <summary>
 95         /// 添加附件功能
 96         /// </summary>
 97         /// <param name="message">Mailmessage对象</param>
 98         /// <param name="Documentpath">附件路径列表</param>
 99         private void AddDocument(MailMessage message, List<string> Documentpath)
100         {
101             foreach(string filepath in Documentpath)
102             {
103                 try
104                 {
105                     if (File.Exists(filepath)) //判断文件是否存在
106                     {
107                         Attachment attach = new Attachment(filepath);    //构造一个附件对象
108                         ContentDisposition disposition = attach.ContentDisposition;   //得到文件的信息
109                         disposition.CreationDate = System.IO.File.GetCreationTime(filepath);
110                         disposition.ModificationDate = System.IO.File.GetLastWriteTime(filepath);
111                         disposition.ReadDate = System.IO.File.GetLastAccessTime(filepath);
112                         message.Attachments.Add(attach);   //向邮件添加附件
113                     }
114                 }
115                 catch { }      
116             }             
117         }
118     }
119 }

做网站的时候用到,看到一篇博文:http://www.cnblogs.com/youring2/archive/2008/11/29/1343911.html

深感有用,特小修并写成类,希望能帮人帮己~

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Newtonsoft.Json 用法

下一篇:c#获取当前应用程序所在路径