EF 框架使用System.Web.Mail发送邮件

/// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="fromEamil">发送人</param>
        /// <param name="fromEmailPassword">发送人密码:一般账号,要用授权码;企业账号用登录密码</param>
        /// <param name="toEmail">接收人</param>
        /// <param name="title">邮件标题</param>
        /// <param name="content">邮件正文</param>
        /// <param name="smtpHost">发送人主机</param>
        /// <param name="smtpHostPort">发送人主机端口</param>
        /// <param name="file">附件</param>
        /// <param name="copyToEmail">抄送人</param>
        /// <param name="isBCC"></param>
        /// <returns>发送结果</returns>
        public static bool SendEmail(string fromEamil, string fromEmailPassword, string toEmail, string title, string content, string smtpHost, string smtpHostPort = "465", string file = "", string copyToEmail = "", bool isBCC = false)
        {
            if (String.IsNullOrEmpty(fromEamil))
            {
                //AlarmLogger.Loging(LogLevel.Warn3, "发送人为空", "发送人不能为空");
                return false;
            }

            if (String.IsNullOrEmpty(fromEmailPassword))
            {
                //AlarmLogger.Loging(LogLevel.Warn3, "发送人密码为空", "发送人密码不能为空");
                return false;
            }

            if (String.IsNullOrEmpty(toEmail))
            {
                //AlarmLogger.Loging(LogLevel.Warn3, "接收人为空", "接收人不能为空");
                return false;
            }

            if (String.IsNullOrEmpty(smtpHost))
            {
                //AlarmLogger.Loging(LogLevel.Warn3, "发送人主机为空", "发送人主机不能为空");
                return false;
            }

            try
            {
                System.Web.Mail.MailMessage email = new System.Web.Mail.MailMessage();//实例化一个邮件类
                email.Priority = System.Web.Mail.MailPriority.Normal;//邮件的优先级,分为 Low, Normal, High,通常用 Normal即可
                email.From = fromEamil;
                email.To = toEmail;
                email.Subject = title;//邮件标题
                email.BodyFormat = System.Web.Mail.MailFormat.Html;
                email.BodyEncoding = Encoding.UTF8;//邮件正文的编码, 设置不正确, 接收者会收到乱码
                email.Body = content;//邮件正文

                if (!String.IsNullOrEmpty(file))
                {
                    System.Web.Mail.MailAttachment attachment = new System.Web.Mail.MailAttachment(file);
                    email.Attachments.Add(attachment);
                }

                if (!String.IsNullOrEmpty(copyToEmail))
                {
                    if (isBCC == true)
                    {
                        email.Bcc = copyToEmail;
                    }
                    else
                    {
                        email.Cc = copyToEmail;
                    }
                }

                email.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//身份验证
                email.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", email.From);//邮箱登录账号,这里跟前面的发送账号一样就行
                email.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", fromEmailPassword);//这个密码要注意:如果是一般账号,要用授权码;企业账号用登录密码
                email.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", smtpHostPort);//端口
                email.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//SSL加密

                System.Web.Mail.SmtpMail.SmtpServer = smtpHost;
                System.Web.Mail.SmtpMail.Send(email);

                return true;
            }
            catch (Exception ex)
            {
                //AlarmLogger.Loging(LogLevel.Debug5, "发邮件出错", "发邮件出错!" + e.Message);
                return false;
            }
        }
原文地址:https://www.cnblogs.com/xiaoxiaomini/p/12166509.html