调用MailMessage发送邮件

    /// <summary>
    /// 邮件类
    /// </summary>
    public class SendMail
    {
        #region 私有变量
        private string fromEmail;
        private string fromPwd;
        private List<string> receiveEmail = new List<string>();
        private string subject;
        private string body;
        private string host;
        private int port;
        #endregion

        #region 公共变量
        /// <summary>
        /// 用于发送邮件的邮箱账号
        /// </summary>
        public string FromEmail
        {
            get { return fromEmail; }
            set { fromEmail = value; }
        }

        /// <summary>
        /// 用于发送邮件的邮箱密码
        /// </summary>
        public string FromPwd
        {
            get { return fromPwd; }
            set { fromPwd = value; }
        }

        /// <summary>
        /// 接收邮件的邮箱
        /// </summary>
        public List<string> ReceiveEmail
        {
            get { return receiveEmail; }
            set { receiveEmail = value; }
        }

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

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

        /// <summary>
        /// SMTP服务的主机名称或IP地址
        /// </summary>
        public string Host
        {
            get { return host; }
            set { host = value; }
        }

        /// <summary>
        /// 端口号
        /// </summary>
        public int Port
        {
            get { return port; }
            set { port = value; }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造 赋值
        /// </summary>
        public SendMail()
        {
            fromEmail = "";
       //这里应该为邮箱授权码 fromPwd = ""; subject = ""; body = "!"; host = ""; port = ; } #endregion #region 发送邮件 /// <summary> /// 发送邮件 /// </summary> /// <param name="errorMsg"></param> /// <param name="lstFileName">附件路径集合</param> /// <returns></returns> public bool Send(out string errorMsg, List<string> lstFileName = null) { errorMsg = string.Empty; try { MailMessage mailMsg = new MailMessage(); mailMsg.From = new MailAddress(fromEmail); mailMsg.Subject = subject; mailMsg.Body = body; if (receiveEmail.Count < 1) { errorMsg = "收件人为空"; return false; } foreach (string item in receiveEmail) { mailMsg.To.Add(item); } #region 添加附件 if (lstFileName != null) { foreach (var item in lstFileName) { mailMsg.Attachments.Add(new Attachment(item)); } } #endregion SmtpClient client = new SmtpClient(); client.Host = host; client.Port = port; client.Credentials = new NetworkCredential(fromEmail, fromPwd); client.EnableSsl = true; client.DeliveryFormat = System.Net.Mail.SmtpDeliveryFormat.International; client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; client.Send(mailMsg); } catch (Exception ex) { errorMsg = ex.Message; return false; } return true; } /// <summary> /// 异步发送邮件 /// </summary> ///<param name="lstFileName">附件路径集合</param> /// <param name="errorMsg"></param> public async Task<bool> SendAsync(List<string> lstFileName = null) { try { MailMessage mailMsg = new MailMessage(); mailMsg.From = new MailAddress(fromEmail); mailMsg.Subject = subject; mailMsg.Body = body; if (receiveEmail.Count < 1) { return false; } foreach (string item in receiveEmail) { mailMsg.To.Add(item); } #region 添加附件 if (lstFileName != null) { foreach (var item in lstFileName) { mailMsg.Attachments.Add(new Attachment(item)); } } #endregion SmtpClient client = new SmtpClient(); client.Host = host; client.Port = port; client.Credentials = new NetworkCredential(fromEmail, fromPwd); client.EnableSsl = true; await client.SendMailAsync(mailMsg); } catch { return false; } return true; } #endregion }

 注意,传过来的参数为附件的路径,并且这里的密码应为邮箱授权码。

posted on 2019-02-20 09:59  TheHeist  阅读(504)  评论(0编辑  收藏  举报