易本地工作室-Ebend Software&Network Studio - 群发,采集,管理类软件定制开发服务【易本地工作室】
本工作室提供信息采集,管理,群发类软件定制服务,6年软件定制经验!

C# 邮件发送类

C#程序中经常要发送邮件,尤其是编写一些监控程序的时候,发送邮件的好处不言而喻,再用个神马139邮箱,就可以通知到用户手机了,当然也可以用来邮件群发,这里封装了一个邮件发送类,亲,要不试试?或者你自己再优化优化也可以,因为为了做到通用性,SendMail函数给我做的实在是太累了,那么多参数要传递,我想你看的也累吧!


using System;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
namespace 邮件发送
{
    class ClassMail
    {
        private MailMessage mailMessage;
        private SmtpClient smtpClient;
        private string SmtpServer;//SMTP服务器
        private int SmtpPort;//SMTP端口
        private string password;//发件人密码 
        private bool bSSL;//启用SSL
        /**/
        /// <summary> 
        /// 处审核后类的实例 
        /// </summary> 
        /// <param name="Smtp">SMTP服务器</param> 
        /// <param name="Port">SMTP端口</param> 
        /// <param name="bEnableSSL">启用SSL</param> 
        /// <param name="To">收件人地址</param> 
        /// <param name="From">发件人地址</param> 
        /// <param name="Body">邮件正文</param> 
        /// <param name="Title">邮件的主题</param> 
        /// <param name="Password">发件人密码</param> 
        public void SendMail(string Smtp,int Port,bool bEnableSSL,string To, string From, string Body, string Title, string Password)
        {
            if (To ==null)
            {
                return;
            }
            SmtpServer = Smtp;
            SmtpPort = Port;
            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;
            this.bSSL = bEnableSSL;
        }
        /**/
        /// <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);//实例化附件 
                disposition = data.ContentDisposition; 
                disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//获取附件的创建日期 
                disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);//获取附件的修改日期 
                disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//获取附件的读取日期 
                mailMessage.Attachments.Add(data);//添加到附件中 
            } 
        }
        /**/
        /// <summary> 
        /// 异步发送邮件 
        /// </summary> 
        /// <param name="CompletedMethod"></param> 
        public void SendAsync(SendCompletedEventHandler CompletedMethod)
        {
            if (mailMessage != null)
            {
                smtpClient = new SmtpClient();
                smtpClient.Credentials = new System.Net.NetworkCredential
(mailMessage.From.Address, password);//设置发件人身份的票据 
                smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtpClient.Host = SmtpServer;
                smtpClient.Port = SmtpPort;
                smtpClient.EnableSsl = bSSL;
                smtpClient.SendCompleted += new SendCompletedEventHandler
(CompletedMethod);//注册异步发送邮件完成时的事件 
                smtpClient.SendAsync(mailMessage, mailMessage.Body);
            }
        }
        /**/
        /// <summary> 
        /// 发送邮件 
        /// </summary> 
        public void Send()
        {
            if (mailMessage != null)
            {
                smtpClient = new SmtpClient(SmtpServer,SmtpPort);
                smtpClient.EnableSsl = bSSL;
                smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//设置发件人身份的票据 
                smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtpClient.Send(mailMessage);
            }
        }
    }
}


标签:C#SendMail发送邮件
分类:源码教程| 发布:adobo| 查看: | 发表时间:2012/11/18
原创文章如转载,请注明:转载自易本地工作室-Ebend Software&Network Studio http://www.ebend.net/
本文链接:http://www.ebend.net/post/C-Sharp_ClassSendMail.html

已经有 ( 0 ) 位网友发表了评论,你也评一评吧!