C# Email 幫助類 EmailHelper

1. 配置文件 App.confightml

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!-- Smtp 服務器 -->
    <add key="SmtpHost" value="smtp.qq.com" />
    <!-- Smtp 服務器端口 -->
    <add key="SmtpPort" value="25" />
    <!--發送者 Email 地址-->
    <add key="FromEmailAddress" value="xx@qq.com" />
    <!--發送者 Email 密碼-->
    <add key="FormEmailPassword" value="??????" />
  </appSettings>
</configuration>

 

2.幫助類代碼服務器

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
 
namespace ConsoleApplication1
{
    /// <summary>
    /// Author      : yenange
    /// Date        : 2014-02-26
    /// Description : 郵件發送輔助類
    /// </summary>
    public class EmailHelper
    {
        #region [ 屬性(發送Email相關) ]
        private string _SmtpHost = string.Empty;
        private int _SmtpPort = -1;
        private string _FromEmailAddress = string.Empty;
        private string _FormEmailPassword = string.Empty;
 
        /// <summary>
        /// smtp 服務器 
        /// </summary>
        public string SmtpHost
        {
            get
            {
                if (string.IsNullOrEmpty(_SmtpHost))
                {
                    _SmtpHost = ConfigurationManager.AppSettings["SmtpHost"];
                }
                return _SmtpHost;
            }
        }
        /// <summary>
        /// smtp 服務器端口  默認爲25
        /// </summary>
        public int SmtpPort
        {
            get
            {
                if (_SmtpPort == -1)
                {
                    if (!int.TryParse(ConfigurationManager.AppSettings["SmtpPort"], out _SmtpPort))
                    {
                        _SmtpPort = 25;
                    }
                }
                return _SmtpPort;
            }
        }
        /// <summary>
        /// 發送者 Eamil 地址
        /// </summary>
        public string FromEmailAddress
        {
            get
            {
                if (string.IsNullOrEmpty(_FromEmailAddress))
                {
                    _FromEmailAddress = ConfigurationManager.AppSettings["FromEmailAddress"];
                }
                return _FromEmailAddress;
            }
        }
 
        /// <summary>
        /// 發送者 Eamil 密碼
        /// </summary>
        public string FormEmailPassword
        {
            get
            {
                if (string.IsNullOrEmpty(_FormEmailPassword))
                {
                    _FormEmailPassword = ConfigurationManager.AppSettings["FormEmailPassword"];
                }
                return _FormEmailPassword;
            }
        }
        #endregion
 
        #region [ 屬性(郵件相關) ]
        /// <summary>
        /// 收件人 Email 列表,多個郵件地址之間用 半角逗號 分開
        /// </summary>
        public string ToList { get; set; }
        /// <summary>
        /// 郵件的抄送者,支持羣發,多個郵件地址之間用 半角逗號 分開
        /// </summary>
        public string CCList { get; set; }
        /// <summary>
        /// 郵件的密送者,支持羣發,多個郵件地址之間用 半角逗號 分開
        /// </summary>
        public string BccList { get; set; }
        /// <summary>
        /// 郵件標題
        /// </summary>
        public string Subject { get; set; }
        /// <summary>
        /// 郵件正文
        /// </summary>
        public string Body { get; set; }
 
        private bool _IsBodyHtml = true;
        /// <summary>
        /// 郵件正文是否爲Html格式
        /// </summary>
        public bool IsBodyHtml
        {
            get { return _IsBodyHtml; }
            set { _IsBodyHtml = value; }
        }
        /// <summary>
        /// 附件列表
        /// </summary>
        public List<Attachment> AttachmentList { get; set; }
        #endregion
 
        #region [ 構造函數 ]
        /// <summary>
        /// 構造函數 (body默認爲html格式)
        /// </summary>
        /// <param name="toList">收件人列表</param>
        /// <param name="subject">郵件標題</param>
        /// <param name="body">郵件正文</param>
        public EmailHelper(string toList, string subject, string body)
        {
            this.ToList = toList;
            this.Subject = subject;
            this.Body = body;
        }
        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="toList">收件人列表</param>
        /// <param name="subject">郵件標題</param>
        /// <param name="isBodyHtml">郵件正文是否爲Html格式</param>
        /// <param name="body">郵件正文</param>
        public EmailHelper(string toList, string subject, bool isBodyHtml, string body)
        {
            this.ToList = toList;
            this.Subject = subject;
            this.IsBodyHtml = isBodyHtml;
            this.Body = body;
        }
        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="toList">收件人列表</param>
        /// <param name="ccList">抄送人列表</param>
        /// <param name="bccList">密送人列表</param>
        /// <param name="subject">郵件標題</param>
        /// <param name="isBodyHtml">郵件正文是否爲Html格式</param>
        /// <param name="body">郵件正文</param>
        public EmailHelper(string toList, string ccList, string bccList, string subject, bool isBodyHtml, string body)
        {
            this.ToList = toList;
            this.CCList = ccList;
            this.BccList = bccList;
            this.Subject = subject;
            this.IsBodyHtml = isBodyHtml;
            this.Body = body;
        }
        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="toList">收件人列表</param>
        /// <param name="ccList">抄送人列表</param>
        /// <param name="bccList">密送人列表</param>
        /// <param name="subject">郵件標題</param>
        /// <param name="isBodyHtml">郵件正文是否爲Html格式</param>
        /// <param name="body">郵件正文</param>
        /// <param name="attachmentList">附件列表</param>
        public EmailHelper(string toList, string ccList, string bccList, string subject, bool isBodyHtml, string body, List<Attachment> attachmentList)
        {
            this.ToList = toList;
            this.CCList = ccList;
            this.BccList = bccList;
            this.Subject = subject;
            this.IsBodyHtml = isBodyHtml;
            this.Body = body;
            this.AttachmentList = attachmentList;
        }
        #endregion
 
        #region [ 發送郵件 ]
        /// <summary>
        /// 發送郵件
        /// </summary>
        /// <returns></returns>
        public void Send()
        {
            SmtpClient smtp = new SmtpClient();                 //實例化一個SmtpClient
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;   //將smtp的出站方式設爲 Network
            smtp.EnableSsl = false;                             //smtp服務器是否啓用SSL加密
            smtp.Host = this.SmtpHost;                          //指定 smtp 服務器地址
            smtp.Port = this.SmtpPort;                          //指定 smtp 服務器的端口,默認是25,若是採用默認端口,可省去
            smtp.UseDefaultCredentials = true;                  //若是你的SMTP服務器不須要身份認證,則使用下面的方式,不過,目前基本沒有不須要認證的了
            smtp.Credentials = new NetworkCredential(this.FromEmailAddress, this.FormEmailPassword);    //若是須要認證,則用下面的方式
 
            MailMessage mm = new MailMessage(); //實例化一個郵件類
            mm.Priority = MailPriority.Normal; //郵件的優先級,分爲 Low, Normal, High,一般用 Normal便可
            mm.From = new MailAddress(this.FromEmailAddress, "管理員", Encoding.GetEncoding(936));
 
            //收件人
            if (!string.IsNullOrEmpty(this.ToList))
                mm.To.Add(this.ToList);
            //抄送人
            if (!string.IsNullOrEmpty(this.CCList))
                mm.CC.Add(this.CCList);
            //密送人
            if (!string.IsNullOrEmpty(this.BccList))
                mm.Bcc.Add(this.BccList);
 
            mm.Subject = this.Subject;                      //郵件標題
            mm.SubjectEncoding = Encoding.GetEncoding(936); //這裏很是重要,若是你的郵件標題包含中文,這裏必定要指定,不然對方收到的極有多是亂碼。
            mm.IsBodyHtml = this.IsBodyHtml;                //郵件正文是不是HTML格式
            mm.BodyEncoding = Encoding.GetEncoding(936);    //郵件正文的編碼, 設置不正確, 接收者會收到亂碼
            mm.Body = this.Body;                            //郵件正文
            //郵件附件
            if (this.AttachmentList != null && this.AttachmentList.Count > 0)
            {
                foreach (Attachment attachment in this.AttachmentList)
                {
                    mm.Attachments.Add(attachment);
                }
            }
            //發送郵件,若是不返回異常, 則大功告成了。
            smtp.Send(mm);
        }
        #endregion
    }
}

 

3.測試代碼app

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Configuration;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try 
            {            
                EmailHelper email = new EmailHelper("xxx@qq.com,xxx@qq.com", "測試郵件2", "<html><body><div style='color:red;'>測試內容</div></body></html>");
                email.Send();
                Console.WriteLine("發送成功!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("發送失敗!失敗緣由:");
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
    }//end of class Program
}//end of namespace
相關文章
相關標籤/搜索