EmailHelper.cs 5.72 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace Hh.Mes.Common.Http
{

    public class EmailHelper
    {

        //var email = new EmailHelper()
        //{
        //    MailFrom = "xxx@qq.com",

        //    //密码或者qq 授权码 隔一段时间可能会变化 
        //    MailPwd = "cexkbsvbfcsibjaj",
        //    Host = "smtp.qq.com",

        //    Nickname = "【发邮件人昵称】我是系统管理员",
        //    MailSubject = "【邮件标题】系统提示",
        //    MailBody = "【邮件内容】今天你吃饭了吗?回答:今天下雨了,气温25度",
        //    MailToArray = new string[] { "p6417562@163.com" }
        //};

        #region Eail 属性

        /// <summary>
        /// 发送者的邮箱
        /// </summary>
        public string MailFrom { get; set; } = "system@haojima.net";

        /// <summary>
        /// 收件人
        /// </summary>
        public string[] MailToArray { get; set; }

        /// <summary>
        /// 抄送
        /// </summary>
        public string[] MailCcArray { get; set; }

        /// <summary>
        /// 邮箱标题
        /// </summary>
        public string MailSubject { get; set; }

        /// <summary>
        /// 邮箱正文
        /// </summary>
        public string MailBody { get; set; }

        /// <summary>
        /// 发送者的邮箱 密码
        /// 直接使用密码可能会提示 不允许使用邮箱名称。 服务器响应为: xxxw
        /// 要使用邮件授权码有安全机制  https://www.cnblogs.com/SharePointFans/p/5187504.html
        /// </summary>
        public string MailPwd { get; set; }

        /// <summary>
        /// 发送者的邮箱 SMTP邮件服务器
        /// 163:smtp.163.com,
        /// qq: smtp.qq.com
        /// </summary>
        public string Host { get; set; } = "smtp.haojima.net";

        /// <summary>
        /// 正文是否是html格式
        /// </summary>
        public bool IsBodyHtml { get; set; } = true;

        /// <summary>
        /// 发送者昵称
        /// </summary>
        public string Nickname { get; set; } = "";

        /// <summary>
        /// 附件
        /// </summary>
        public string[] AttachmentsPath { get; set; }

        //优先级别
        /// <summary>
        /// 优先级别  默认正常优先级
        /// </summary>
        public MailPriority Priority { get; set; } = MailPriority.Normal;

        #endregion

        /// <summary>
        /// Item1 为true 发送成功, Item2:返回信息
        /// </summary>
        /// <returns></returns>
        public Tuple<bool, string> Send()
        {
            var myMail = InitMailAddress();

            #region 在有附件的情况下添加附件

            try
            {
                if (AttachmentsPath != null && AttachmentsPath.Length > 0)
                {
                    Attachment attachFile = null;
                    foreach (string path in AttachmentsPath)
                    {
                        attachFile = new Attachment(path);
                        myMail.Attachments.Add(attachFile);
                    }
                }
            }
            catch (Exception err)
            {
                return new Tuple<bool, string>(false, "在添加附件时有错误:" + err);
            }

            #endregion

            return SendEmailMethod(myMail);
        }

        private MailMessage InitMailAddress()
        {
            //使用指定的邮件地址初始化MailAddress实例
            var maddr = new MailAddress(MailFrom, Nickname);
            //初始化MailMessage实例
            var myMail = new MailMessage();

            //向收件人地址集合添加邮件地址
            if (MailToArray != null)
            {
                for (int i = 0; i < MailToArray.Length; i++)
                {
                    myMail.To.Add(MailToArray[i].ToString());
                }
            }

            //向抄送收件人地址集合添加邮件地址
            if (MailCcArray != null)
            {
                for (int i = 0; i < MailCcArray.Length; i++)
                {
                    myMail.CC.Add(MailCcArray[i].ToString());
                }
            }

            //发件人地址
            myMail.From = maddr;

            //电子邮件的标题
            myMail.Subject = MailSubject;

            //电子邮件的主题内容使用的编码
            myMail.SubjectEncoding = Encoding.UTF8;

            //电子邮件正文
            myMail.Body = MailBody;

            //电子邮件正文的编码
            myMail.BodyEncoding = Encoding.Default;

            //邮件优先级
            myMail.Priority = Priority;

            myMail.IsBodyHtml = IsBodyHtml;
            return myMail;
        }

        private Tuple<bool, string> SendEmailMethod(MailMessage myMail)
        {
            var smtp = new SmtpClient
            {
                //指定发件人的邮件地址和密码以验证发件人身份
                Credentials = new NetworkCredential(MailFrom, MailPwd), //115                 //设置SMTP邮件服务器
                Host = Host
            };
            // smtp.EnableSsl = true;
            //smtp.Port = 587;
            try
            {
                //将邮件发送到SMTP邮件服务器
                smtp.Send(myMail);
                return new Tuple<bool, string>(true, "邮件发送成功,请稍后查看收件邮件,邮件可能会延迟,请耐心等待!");
            }
            catch (SmtpException ex)
            {
                return new Tuple<bool, string>(false, "邮件发送失败!" + ex);
            }
        }
    }
}