using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace Utils
{
    /// <summary>
    /// 邮件发送类
    /// </summary>
    public class EmailHelper
    {
        public EmailHelper()
        {
        }


        #region 属性
        /// <summary>
        /// 邮件主题
        /// </summary>
        public virtual string Title { get; set; }

        /// <summary>
        /// 邮件内容
        /// </summary>
        public virtual string Cont { get; set; }

        /// <summary>
        /// 发邮件地址
        /// </summary>
        public virtual string SendEMail { get; set; }

        /// <summary>
        /// 发邮件密码
        /// </summary>
        public virtual string SendPwd { get; set; }

        /// <summary>
        /// 收邮件地址
        /// </summary>
        public string ReceiveEmail { get; set; }
        #endregion

        #region 方法
        /// <summary>
        /// 发邮件
        /// </summary>
        /// <param name="email"></param>
        public void SendMail(EmailHelper email)
        {
            //发邮件地址和密码 
            email.SendEMail = AppSettingsHelper.Configuration["emailSender:address"];
            email.SendPwd = AppSettingsHelper.Configuration["emailSender:pwd"];

            var smtpServer = GetSmtpServer(email.SendEMail);
            MailAddress addressFrom = new MailAddress(email.SendEMail);
            MailMessage mailMsg = new MailMessage();
            mailMsg.From = addressFrom;
            mailMsg.To.Add(email.ReceiveEmail);
            mailMsg.Subject = email.Title;
            mailMsg.Body = email.Cont;
            mailMsg.IsBodyHtml = true;
            mailMsg.BodyEncoding = Encoding.Default;
            mailMsg.Priority = MailPriority.High;
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = smtpServer;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = new NetworkCredential(email.SendEMail, email.SendPwd);
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.Send(mailMsg);
        }


        /// <summary>
        /// 发邮件给管理员
        /// </summary>
        /// <param name="email"></param>
        public void SendMailToAdmin(EmailHelper email)
        {
            //发邮件地址和密码
            string sendEmail = AppSettingsHelper.Configuration["emailSender:address"];     //发邮件地址
            string sendPwd = AppSettingsHelper.Configuration["emailSender:pwd"];        //发邮件密码
            string receiveEmail = ReceiveEmail;       //收邮件的管理员
            var smtpServer = GetSmtpServer(sendEmail);
            MailAddress addressFrom = new MailAddress(sendEmail);
            MailMessage mailMsg = new MailMessage();
            mailMsg.From = addressFrom;
            mailMsg.To.Add(receiveEmail);
            //同时发送多个邮件
            //mailMsg.To.Add("626056064@qq.com,718083192@qq.com");
            mailMsg.Subject = email.Title;    //邮件标题
            mailMsg.Body = email.Cont;        //邮件内容
            mailMsg.IsBodyHtml = true;
            mailMsg.BodyEncoding = Encoding.Default;
            mailMsg.Priority = MailPriority.High;
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = smtpServer;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = new NetworkCredential(sendEmail, sendPwd);
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.Send(mailMsg);
        }

        public string GetSmtpServer(string sendEmail)
        {
            var smtpServer = "smtp.chinahonden.com";
            return smtpServer;
        }



        /// <summary>
        /// 添加空格
        /// </summary>
        /// <param name="t"></param>
        /// <param name="count"></param>
        public static void AppendNbsp(ref StringBuilder t, int count)
        {
            while (count > 0)
            {
                t.Append("&nbsp;");
                count--;
            }
        }


        /// <summary>
        ///格式化的邮件内容
        /// </summary>
        /// <param name="email_customer"></param>
        /// <returns></returns>
        public string GetAdminBody(string emailContent)
        {
            StringBuilder bodyInfo = new StringBuilder("<html><head><title></title></head><body>");
            bodyInfo.Append(string.Format("管理员您好："));
            //bodyInfo.Append("<br /><br />");
            bodyInfo.Append(string.Format("{0}", emailContent));
            AppendNbsp(ref bodyInfo, 77);
            bodyInfo.Append(string.Format("<p>{0}<br/>", "牛买网"));
            AppendNbsp(ref bodyInfo, 77);
            bodyInfo.Append(string.Format("{0}</body></html>", DateTime.Now.ToShortDateString()));
            return bodyInfo.ToString();
        }

        /// <summary>
        /// 投递项目给投资商邮件模板
        /// </summary>
        /// <param name="emailContent"></param>
        /// <returns></returns>
        public string GetInvestorBody(string emailContent)
        {
            StringBuilder bodyInfo = new StringBuilder("<html><head><title></title></head><body>");
            //bodyInfo.Append(string.Format("尊敬的***公司："));
            //bodyInfo.Append("<br /><br />");
            bodyInfo.Append(string.Format("{0}", emailContent));
            bodyInfo.Append(string.Format("<p>"));
            AppendNbsp(ref bodyInfo, 77);
            bodyInfo.Append(string.Format("{0}<br/>", "融资通网"));
            AppendNbsp(ref bodyInfo, 77);
            bodyInfo.Append(string.Format("{0}</body></html>", DateTime.Now.ToShortDateString()));
            return bodyInfo.ToString();
        }

        #endregion
    }
}
