C#,一个邮件发送的类

以下为邮件发送使用SMTP协议,在使用过程中曾经遇到邮件不能发送的问题,如果你也遇到了,可以检查一下windows的防火墙,windows自带的防火墙如有启用,可加入例外端口:25。如有安装金山病毒防火墙,也需要将25端口的过滤禁用。
///邮件实体类:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;

namespace QSystem.ToolKit
{
    /// <summary>
    /// 类名称:Mail
    /// 说  明:邮件实体
    /// 创建日期:08/17/2006 13:52:00
    /// </summary>
    [Serializable]
    public class Mail
    {

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public Mail() { }

        public Mail(string smtpServer, string userName, string userPassword, string from, string[] to, string subject, string body)
            : this(smtpServer, userName, userPassword, from, to, subject, body, null) { }

        public Mail(string smtpServer, string userName, string userPassword, string from, string[] to, string subject, string body, string[] attachments)
            : this(smtpServer, userName, userPassword, from, to, subject, body, attachments, false) { }

        public Mail(string smtpServer, string userName, string userPassword, string from, string[] to, string subject, string body, string[] attachments, bool isBodyHtml)
            : this(smtpServer, userName, userPassword, from, to, subject, body, attachments, isBodyHtml, Encoding.Default) { }

        public Mail(string smtpServer, string userName, string userPassword, string from, string[] to, string subject, string body, string[] attachments, bool isBodyHtml, Encoding encoding)
        {
            this.smtpServer = smtpServer;
            this.userName = userName;
            this.userPassword = userPassword;
            this.from = from;
            this.to = to;
            this.subject = subject;
            this.body = body;
            this.isBodyHtml = isBodyHtml;
            this.encoding = encoding;
            this.attachments = attachments;
        }

        string smtpServer;
        /// <summary>
        /// SMTP服务器地址
        /// </summary>
        public string SmtpServer
        {
            get { return smtpServer; }
            set { smtpServer = value; }
        }

        string userName;
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        string userPassword;
        /// <summary>
        /// 密码
        /// </summary>
        public string UserPassword
        {
            get { return userPassword; }
            set { userPassword = value; }
        }

        string from;
        /// <summary>
        /// 发件人
        /// </summary>
        public string From
        {
            get { return from; }
            set { from = value; }
        }

        string[] to;
        /// <summary>
        /// 收件人
        /// </summary>
        public string[] To
        {
            get { return to; }
            set { to = value; }
        }

        string subject;
        /// <summary>
        /// 主题
        /// </summary>
        public string Subject
        {
            get { return subject; }
            set { subject = value; }
        }

        string body;
        /// <summary>
        /// 内容
        /// </summary>
        public string Body
        {
            get { return body; }
            set { body = value; }
        }

        bool isBodyHtml;
        /// <summary>
        /// 是否以html格式发送
        /// </summary>
        public bool IsBodyHtml
        {
            get { return isBodyHtml; }
            set { isBodyHtml = value; }
        }

        Encoding encoding;
        /// <summary>
        /// 编码格式
        /// </summary>
        public Encoding Encoding
        {
            get { return encoding; }
            set { encoding = value; }
        }

        string[] attachments;
        /// <summary>
        /// 附件
        /// </summary>
        public string[] Attachments
        {
            get { return attachments; }
            set { attachments = value; }
        }

    }
}

///发送类
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.IO;

namespace QSystem.ToolKit
{
    /// <summary>
    /// 类名称:MailPoster
    /// 说  明:邮件发送
    /// 创建日期:08/17/2006 13:23:51
    /// </summary>
    public class MailPoster
    {

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public MailPoster() { }

        public void Send(Mail[] mails)
        {
            if (mails != null)
            {
                foreach (Mail mail in mails)
                    this.Send(mail);
            }
        }

        public void Send(Mail mail)
        {
            SmtpClient Client = new SmtpClient(mail.SmtpServer);

            Client.UseDefaultCredentials = false;
            Client.Credentials = new System.Net.NetworkCredential(mail.UserName, mail.UserPassword);
            Client.DeliveryMethod = SmtpDeliveryMethod.Network;

            if (mail.To != null)
            {

                foreach (string to in mail.To)
                {
                    MailMessage Message = new MailMessage(mail.From, to, mail.Subject, mail.Body);
                    Message.IsBodyHtml = mail.IsBodyHtml;
                    Message.BodyEncoding = mail.Encoding;

                    if (mail.Attachments != null)
                    {
                        foreach (string attachment in mail.Attachments)
                        {
                            Attachment attachData = new Attachment(attachment);
                            Message.Attachments.Add(attachData);
                        }
                    }

                    Client.Send(Message);
                }
            }
        }
    }
}
///调用示例
            Mail mail = new Mail("smtp.xxx.com", "qiusz", "密码", "qiusz@xxx.com",new string[]{ "qiusz@xxx.com" }, "测试邮件标题", "测试邮件内容", null, true);
            MailPoster poster = new MailPoster();
            poster.Send(mail); 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值