發送郵件工具類MailHelper

using System;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading;

namespace 落地頁測試代碼
{
    public class MailHelper
    {
        /// <summary>
        /// 郵件服務器地址
        /// </summary>
        public string MailServer { get; set; }
        /// <summary>
        /// 用戶名
        /// </summary>
        public string MailUserName { get; set; }
        /// <summary>
        /// 密碼
        /// </summary>
        public string MailPassword { get; set; }
        /// <summary>
        /// 名稱
        /// </summary>
        public string MailName { get; set; }

        /// <summary>
        /// 同步發送郵件
        /// </summary>
        /// <param name="to">收件人郵箱地址</param>
        /// <param name="subject">主題</param>
        /// <param name="body">內容</param>
        /// <param name="encoding">編碼</param>
        /// <param name="isBodyHtml">是否Html</param>
        /// <param name="enableSsl">是否SSL加密鏈接</param>
        /// <returns>是否成功</returns>
        public bool Send(string to, string subject, string body, string encoding = "UTF-8", bool isBodyHtml = true, bool enableSsl = false)
        {
            try
            {
                MailMessage message = new MailMessage();
                // 接收人郵箱地址
                message.To.Add(new MailAddress(to));
                message.From = new MailAddress(MailUserName, MailName);
                message.BodyEncoding = Encoding.GetEncoding(encoding);
                message.Body = body;
                //GB2312
                message.SubjectEncoding = Encoding.GetEncoding(encoding);
                message.Subject = subject;
                message.IsBodyHtml = isBodyHtml;

                SmtpClient smtpclient = new SmtpClient(MailServer, 25);
                smtpclient.Credentials = new System.Net.NetworkCredential(MailUserName, MailPassword);
                //SSL鏈接
                smtpclient.EnableSsl = enableSsl;
                smtpclient.Send(message);
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 異步發送郵件 獨立線程
        /// </summary>
        /// <param name="to">郵件接收人</param>
        /// <param name="title">郵件標題</param>
        /// <param name="body">郵件內容</param>
        /// <param name="port">端口號</param>
        /// <returns></returns>
        public void SendByThread(string to, string title, string body, int port = 25)
        {
            new Thread(new ThreadStart(delegate()
            {
                try
                {
                    SmtpClient smtp = new SmtpClient();
                    //郵箱的smtp地址
                    smtp.Host = MailServer;
                    //端口號
                    smtp.Port = port;
                    //構建發件人的身份憑據類
                    smtp.Credentials = new NetworkCredential(MailUserName, MailPassword);
                    //構建消息類
                    MailMessage objMailMessage = new MailMessage();
                    //設置優先級
                    objMailMessage.Priority = MailPriority.High;
                    //消息發送人
                    objMailMessage.From = new MailAddress(MailUserName, MailName, System.Text.Encoding.UTF8);
                    //收件人
                    objMailMessage.To.Add(to);
                    //標題
                    objMailMessage.Subject = title.Trim();
                    //標題字符編碼
                    objMailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
                    //正文
                    objMailMessage.Body = body.Trim();
                    objMailMessage.IsBodyHtml = true;
                    //內容字符編碼
                    objMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
                    //發送
                    smtp.Send(objMailMessage);
                }
                catch (Exception)
                {
                    throw;
                }

            })).Start();
        }
    }
}

相關文章
相關標籤/搜索