使用smtp发送邮件所有的一个类.

代码
  1 #region 编码信息
  2 /*
  3  * 开发人员:秦仕川
  4  * Date: 2010-3-27
  5  * Time: 19:07
  6  * 修改人员:秦仕川
  7  * 修改时间:
  8  */
  9 #endregion
 10 using System;
 11 using System.Net.Mail;//引入命名空间mail
 12 using System.IO;//引入命名空间,用于发送附件
 13 namespace QSMY.SMTP
 14 {
 15     /// <summary>
 16     /// Description of EmailSend.
 17     /// </summary>
 18     public class EmailSend
 19     {
 20         private EmailSend()
 21         {
 22         }
 23         /// <summary>
 24         /// 在调用时使用的构造函数
 25         /// </summary>
 26         /// <param name="serverAddress">你所用邮箱服务器地址</param>
 27         /// <param name="serverSmptPort">该服务器的smpt端口号</param>
 28         /// <param name="senderUserName">你的登陆账号</param>
 29         /// <param name="senderPassWord">你的登陆密码</param>
 30         /// <param name="receiverEmail">接收方的邮箱地址</param>
 31         /// <param name="replyTo">对方回复给你到的邮箱</param>
 32         /// <param name="subject">邮件主题</param>
 33         /// <param name="body">邮件内容</param>
 34         public EmailSend(string serverAddress,
 35                          string serverSmptPort,
 36                          string senderUserName,
 37                          string senderPassWord,
 38                          string receiverEmail,
 39                          string replyTo,
 40                          string subject,
 41                          string body )
 42         {
 43             this.ServerAddress=serverAddress;
 44             this.ServerSmptPort=serverSmptPort;
 45             this.SenderUserName=senderUserName;
 46             this.SenderPassWord=senderPassWord;
 47             this.ReceiverEmail=receiverEmail;
 48             this.ReplyTo=replyTo;
 49             this.Subject=subject;
 50             this.Body=body;
 51             
 52         }
 53         public string Subject{
 54             get;
 55             set;
 56         }
 57         public string Body{
 58             get;
 59             set;
 60         }
 61         public string ReplyTo{
 62             get;
 63             set;
 64         }
 65         public string ReceiverEmail{
 66             get;
 67             set;
 68         }
 69         public string ServerAddress{
 70             get;
 71             set;
 72         }
 73         public string ServerSmptPort{
 74             get;
 75             set;
 76         }
 77         public string SenderUserName{
 78             get;
 79             set;
 80         }
 81         public string SenderPassWord{
 82             get;
 83             set;
 84         }
 85         private FileInfo _attachment;
 86         private bool _hasAttachment;
 87         /// <summary>
 88         /// 附件,只要设置了附件就会使发送时自动发送附件.
 89         /// </summary>
 90         public FileInfo Attachment{
 91             get{
 92                 
 93                 return _attachment;
 94             }
 95             set{
 96                 _hasAttachment=true;
 97                 _attachment=value;
 98             }
 99         }
100         public void Send(FileInfo attachment){
101             
102             //创建 SmtpClient 以发送 Email
103             SmtpClient client = new SmtpClient();
104             MailMessage message = new MailMessage();
105             //设置发信人的EMAIL地址
106             message.From = new MailAddress(this.SenderUserName);
107             //设置收信人的EMAIL地址
108             message.To.Add(this.ReceiverEmail);
109             //设置回复的EMAIL地址
110             message.ReplyTo = new MailAddress(this.ReplyTo);
111             //设置发信主题及内容
112             message.Subject = this.Subject;
113             message.Body =this.Body;
114             message.IsBodyHtml = true;
115             //设置SMTP服务器
116             client.Host =this.ServerAddress;
117             //设置SMTP 端口
118             client.Port = int.Parse(this.ServerSmptPort);
119             client.UseDefaultCredentials = false;
120             //设置SMTP的登录名和密码
121             System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential(SenderUserName, SenderPassWord);
122             client.Credentials = basicAuthenticationInfo;
123             client.EnableSsl = false;
124             //创建附件对象
125             if (_hasAttachment)//如果有附件发送附件
126             {
127                 if (attachment.Exists)
128                 {
129                     System.Net.Mail.Attachment myAttachment = new System.Net.Mail.Attachment(attachment.FullName, System.Net.Mime.MediaTypeNames.Application.Octet);
130                     System.Net.Mime.ContentDisposition disposition = myAttachment.ContentDisposition;
131                     disposition.CreationDate = System.IO.File.GetCreationTime(attachment.FullName);
132                     disposition.ModificationDate = System.IO.File.GetLastWriteTime(attachment.FullName);
133                     disposition.ReadDate = System.IO.File.GetLastAccessTime(attachment.FullName);
134                     message.Attachments.Add(myAttachment);
135                 }
136             }
137             client.Send(message);//发送邮件
138 
139             
140         }
141     }
142 }
143 





少侠,我看你气度不凡天赋异禀,这么帅,来了就给推荐一把吧




我的最近更新
最新发布文章、框架、咨询等,来看看吧
原文地址:https://www.cnblogs.com/humble/p/1698513.html