.net 开发SMTP服务器邮件发送

前些日子开发程序,我感觉也有参考价值的,是关于。net C#发送SMTP邮件的类。

我做了一个自定义的类,它继承于MailMessage类,具体如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Net.Mime;
using System.IO;

namespace NetMailDll
{
    /// <summary>
    /// .net 开发SMTP服务器邮件发送
    /// </summary>
    public class NetMail : MailMessage
    {
        #region  定义私有成员和属性

        /// <summary>
        /// 发送邮件对象
        /// </summary>
        SmtpClient clientSMTP = null;

        /// <summary>
        /// 用于 SMTP 事务的主机的名称或 IP 地址
        /// </summary>
        private string SmtpServer = "";

        /// <summary>
        /// 用于 SMTP 事务的主机的名称或 IP 地址
        /// </summary>
        public string SMTPhost
        {
            get
            {
                return SmtpServer;
            }
            set
            {
                SmtpServer = value;
            }
        }


        #endregion

        #region  定义构造函数
        /// <summary>
        /// 创建发送邮件的类
        /// </summary>
        /// <param >用于 SMTP 事务的主机的名称或 IP 地址</param>
        public NetMail(string SMTPhost)
        {
            SmtpServer = SMTPhost;
            clientSMTP  = new SmtpClient(SmtpServer);
        }
        /// <summary>
        /// 创建发送邮件的类
        /// </summary>
        public NetMail()
        {
        }
        #endregion

        /// <summary>
        /// 添加收件人的邮件地址
        /// </summary>
        /// <param >收件人的邮件地址</param>
        /// <param >收件人的显示名称</param>
        public void AddSendMailAddressTo(string address, string displayName)
        {
            To.Add(new MailAddress(address, displayName));
        }
        /// <summary>
        /// 添加收件人的邮件地址
        /// </summary>
        /// <param >收件人的邮件地址</param>
        public void AddSendMailAddressTo(string address)
        {
            To.Add(new MailAddress(address));
        }
        /// <summary>
        /// 发件人的邮件地址
        /// </summary>
        /// <param >发件人的邮件地址</param>
        /// <param >显示名称</param>
        public void MailAddressFrom(string address, string displayName)
        {
            From = new MailAddress(address, displayName);
        }
        /// <summary>
        /// 发件人的邮件地址
        /// </summary>
        /// <param >发件人的邮件地址</param>
        public void MailAddressFrom(string address)
        {
            From = new MailAddress(address);
        }
        /// <summary>
        /// 执行发送邮件
        /// </summary>
        /// <param >邮件标题</param>
        /// <param >邮件正文</param>
        /// <param >发送邮件的文件(包括路径和文件扩展名),没有附件则只null</param>
        public void RunSendMail(string mailSubject, string mailBody, string attachmentFileName)
        {
            if (attachmentFileName !=""&&attachmentFileName !=null &&!System.IO.File.Exists(attachmentFileName))
            {
                throw new Exception("文件:" + attachmentFileName+" 不存在!");
            }
            if (SmtpServer == "" || SmtpServer == null)
                throw new Exception("没有设置SMTP邮件服务器的名称或IP地址!");
            if (From == null)
                throw new Exception("没有设置发送人的邮件地址!");
            if (To.Count ==0)
                throw new Exception("没有设置收件人的邮件地址列表!");
            try
            {
                Subject = mailSubject;
                Body = mailBody;
                Attachment Attachmentdata = null;
                if (attachmentFileName != "" && attachmentFileName != null)
                {
                    // 创建邮件附加对象,二进制文件
                    Attachmentdata = new Attachment(attachmentFileName, MediaTypeNames.Application.Octet);
                    // Add time stamp information for the file.
                    ContentDisposition disposition = Attachmentdata.ContentDisposition;
                    disposition.CreationDate = System.IO.File.GetCreationTime(attachmentFileName);
                    disposition.ModificationDate = System.IO.File.GetLastWriteTime(attachmentFileName);
                    disposition.ReadDate = System.IO.File.GetLastAccessTime(attachmentFileName);
                    // 向邮件添加附件文件

                    Attachments.Add(Attachmentdata);
                }

                if (clientSMTP == null)
                    clientSMTP = new SmtpClient(SmtpServer);

                //Add credentials if the SMTP server requires them.

                //外部邮箱可以加登陆验证
                //NetworkCredential myCred = new NetworkCredential("lisontu","******");
                //client.Credentials = myCred;
                //client.Port = 25;

                clientSMTP.Credentials = CredentialCache.DefaultNetworkCredentials;

 

                clientSMTP.Send(this);
                //释放邮件附件对象
                if (Attachmentdata != null)
                    Attachmentdata.Dispose();
            }
            catch (Exception ex )
            {
                throw new Exception(ex.Message);
            }
        }
    }
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值