C# 邮件发送

Mail类

using System.Net.Mail;
using System.Net.Mime;

namespace Common
{
    public class MFMail
    {
        private MailMessage mailMessage;
        private SmtpClient smtpClient;
        private string password; 

        /// <summary>  
        /// 初始化 
        /// </summary>  
        /// <param name="To">收件人地址[di zhi]</param>  
        /// <param name="From">发件人地址[di zhi]</param>  
        /// <param name="Body">邮件[you jian]正文[zheng wen]</param>  
        /// <param name="Title">邮件[you jian]的主题</param>  
        /// <param name="Password">发件人密码[mi ma]</param>
        public  MFMail(string To, string From, string Body, string Title, string Password)
        {
            mailMessage = new MailMessage();
            mailMessage.To.Add(To);
            mailMessage.From = new System.Net.Mail.MailAddress(From);
            mailMessage.Subject = Title;
            mailMessage.Body = Body;
            mailMessage.IsBodyHtml = true;
            mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
            mailMessage.Priority = System.Net.Mail.MailPriority.Normal;
            this.password = Password;
        }
     
        /// <summary>  
        /// 添加附件  
        /// </summary>
        public void Attachments(string Path)
        {
            string[] path = Path.Split(',');
            Attachment data;
            ContentDisposition disposition;
            for (int i = 0; i < path.Length; i++)
            {
                data = new Attachment(path[i], MediaTypeNames.Application.Octet); //实例化[shi li hua]附件  
                disposition = data.ContentDisposition;
                disposition.CreationDate = System.IO.File.GetCreationTime(path[i]); //获取附件的创建日期[chuang jian ri qi]  
                disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]); //获取附件的修改[xiu gai]日期  
                disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]); //获取附件的读取[du qu]日期  
                mailMessage.Attachments.Add(data); //添加到附件中  
            }
        }

        /// <summary>  
        /// 异步发送邮件
        /// </summary>  
        /// <param name="CompletedMethod"></param>
        public void SendAsync(SendCompletedEventHandler CompletedMethod)
        {
            if (mailMessage != null)
            {
                smtpClient = new SmtpClient();
                smtpClient.EnableSsl = true;//启用SSL
                smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);
                //设置发件人身份的票据  
                smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtpClient.Host = "smtp." + mailMessage.From.Host;
                smtpClient.SendCompleted += new SendCompletedEventHandler(CompletedMethod);
                 //注册异步发送邮件完成时的事件 
                smtpClient.SendAsync(mailMessage, mailMessage.Body);
            }
        }
        
        /// <summary>  
        /// 发送邮件
        /// </summary>
        public void Send()
        {
            if (mailMessage != null)
            {
                smtpClient = new SmtpClient();
                smtpClient.EnableSsl = true;//启用SSL
                smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);
                //设置发件人身份的票据  
                smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtpClient.Host = "smtp." + mailMessage.From.Host;
                smtpClient.Send(mailMessage);
            }
        }
    }
}
View Code

测试邮箱163邮箱 QQ邮箱个人版

经验证QQ邮箱认证密码必须使用授权码而非QQ密码 或者 QQ邮箱独立密码 以上代码测试通过

posted @ 2016-03-07 09:25  Merray  Views(111)  Comments(0Edit  收藏  举报