Ejemplo n.º 1
0
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="mailBodyEntity">邮件基础信息</param>
        public static SendResultEntity SendMail(MailBodyEntity mailBodyEntity)
        {
            var sendResultEntity = new SendResultEntity();

            if (mailBodyEntity == null)
            {
                throw new ArgumentNullException();
            }

            SendServerConfigurationEntity sendServerConfiguration = new SendServerConfigurationEntity();
            YuebonCacheHelper             yuebonCacheHelper       = new YuebonCacheHelper();
            AppSetting sysSetting = yuebonCacheHelper.Get <AppSetting>("SysSetting");

            if (sysSetting != null)
            {
                sendServerConfiguration.SmtpHost       = DEncrypt.Decrypt(sysSetting.Emailsmtp);
                sendServerConfiguration.SenderAccount  = sysSetting.Emailusername;
                sendServerConfiguration.SenderPassword = DEncrypt.Decrypt(sysSetting.Emailpassword);
                sendServerConfiguration.SmtpPort       = sysSetting.Emailport.ToInt();
                sendServerConfiguration.IsSsl          = sysSetting.Emailssl.ToBool();
                sendServerConfiguration.MailEncoding   = "utf-8";

                mailBodyEntity.Sender        = sysSetting.Emailnickname;
                mailBodyEntity.SenderAddress = sysSetting.Emailusername;
            }
            else
            {
                sendResultEntity.ResultInformation = "邮件服务器未配置";
                sendResultEntity.ResultStatus      = false;
                throw new ArgumentNullException();
            }
            sendResultEntity = SendMail(mailBodyEntity, sendServerConfiguration);
            return(sendResultEntity);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 账户认证
 /// </summary>
 /// <param name="mailBodyEntity">邮件内容</param>
 /// <param name="sendServerConfiguration">发送配置</param>
 /// <param name="client">客户端对象</param>
 /// <param name="sendResultEntity">发送结果</param>
 public static void Authenticate(MailBodyEntity mailBodyEntity, SendServerConfigurationEntity sendServerConfiguration,
                                 SmtpClient client, SendResultEntity sendResultEntity)
 {
     try
     {
         client.Authenticate(sendServerConfiguration.SenderAccount, sendServerConfiguration.SenderPassword);
     }
     catch (AuthenticationException ex)
     {
         sendResultEntity.ResultInformation = $"无效的用户名或密码:{0}" + ex.Message;
         sendResultEntity.ResultStatus      = false;
     }
     catch (SmtpCommandException ex)
     {
         sendResultEntity.ResultInformation = $"尝试验证错误:{0}" + ex.Message;
         sendResultEntity.ResultStatus      = false;
     }
     catch (SmtpProtocolException ex)
     {
         sendResultEntity.ResultInformation = $"尝试验证时的协议错误:{0}" + ex.Message;
         sendResultEntity.ResultStatus      = false;
     }
     catch (Exception ex)
     {
         sendResultEntity.ResultInformation = $"账户认证错误:{0}" + ex.Message;
         sendResultEntity.ResultStatus      = false;
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="mailBodyEntity">邮件基础信息</param>
        /// <param name="sendServerConfiguration">发件人基础信息</param>
        public static SendResultEntity SendMail(MailBodyEntity mailBodyEntity,
                                                SendServerConfigurationEntity sendServerConfiguration)
        {
            var sendResultEntity = new SendResultEntity();

            if (mailBodyEntity == null)
            {
                throw new ArgumentNullException();
            }

            if (sendServerConfiguration == null)
            {
                sendResultEntity.ResultInformation = "邮件服务器未配置";
                sendResultEntity.ResultStatus      = false;
                throw new ArgumentNullException();
            }

            using (var client = new SmtpClient(new ProtocolLogger(MailMessage.CreateMailLog())))
            {
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;

                Connection(mailBodyEntity, sendServerConfiguration, client, sendResultEntity);

                if (sendResultEntity.ResultStatus == false)
                {
                    return(sendResultEntity);
                }

                SmtpClientBaseMessage(client);

                // Note: since we don't have an OAuth2 token, disable
                // the XOAUTH2 authentication mechanism.
                client.AuthenticationMechanisms.Remove("XOAUTH2");

                Authenticate(mailBodyEntity, sendServerConfiguration, client, sendResultEntity);

                if (sendResultEntity.ResultStatus == false)
                {
                    return(sendResultEntity);
                }

                Send(mailBodyEntity, sendServerConfiguration, client, sendResultEntity);

                if (sendResultEntity.ResultStatus == false)
                {
                    return(sendResultEntity);
                }
                client.Disconnect(true);
            }
            return(sendResultEntity);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 设置邮件基础信息
        /// </summary>
        /// <param name="minMessag"></param>
        /// <param name="mailBodyEntity"></param>
        /// <returns></returns>
        public static MimeMessage SetMailBaseMessage(MimeMessage minMessag, MailBodyEntity mailBodyEntity)
        {
            if (minMessag == null)
            {
                throw new ArgumentNullException();
            }
            if (mailBodyEntity == null)
            {
                throw new ArgumentNullException();
            }

            //插入发件人
            minMessag.From.Add(new MailboxAddress(mailBodyEntity.Sender, mailBodyEntity.SenderAddress));

            //插入收件人
            if (mailBodyEntity.Recipients.Any())
            {
                foreach (var recipients in mailBodyEntity.Recipients)
                {
                    minMessag.To.Add(new MailboxAddress("mailAddress", recipients));
                }
            }

            //插入抄送人
            if (mailBodyEntity.Cc != null && mailBodyEntity.Cc.Any())
            {
                foreach (var cC in mailBodyEntity.Cc)
                {
                    minMessag.Cc.Add(new MailboxAddress("mailAddress", cC));
                }
            }

            //插入密送人
            if (mailBodyEntity.Bcc != null && mailBodyEntity.Bcc.Any())
            {
                foreach (var bcc in mailBodyEntity.Bcc)
                {
                    minMessag.Bcc.Add(new MailboxAddress("mailAddress", bcc));
                }
            }

            //插入主题
            minMessag.Subject = mailBodyEntity.Subject;
            return(minMessag);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 组装邮件文本/附件邮件信息
        /// </summary>
        /// <param name="mailBodyEntity">邮件消息实体</param>
        /// <returns></returns>
        public static MimeMessage AssemblyMailMessage(MailBodyEntity mailBodyEntity)
        {
            if (mailBodyEntity == null)
            {
                throw new ArgumentNullException(nameof(mailBodyEntity));
            }
            var message = new MimeMessage();

            //设置邮件基本信息
            SetMailBaseMessage(message, mailBodyEntity);

            var multipart = new Multipart("mixed");

            //插入文本消息
            if (!string.IsNullOrEmpty(mailBodyEntity.Body))
            {
                var alternative = new MultipartAlternative
                {
                    AssemblyMailTextMessage(mailBodyEntity.Body, mailBodyEntity.MailBodyType)
                };
                multipart.Add(alternative);
            }
            if (mailBodyEntity.MailFiles != null)
            {
                //插入附件
                foreach (var mailFile in mailBodyEntity.MailFiles)
                {
                    if (mailFile.MailFilePath != null && File.Exists(mailFile.MailFilePath))
                    {
                        var mimePart = AssemblyMailAttachmentMessage(mailFile.MailFileType, mailFile.MailFileSubType,
                                                                     mailFile.MailFilePath);
                        multipart.Add(mimePart);
                    }
                }
            }

            //组合邮件内容
            message.Body = multipart;
            return(message);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// 连接服务器
 /// </summary>
 /// <param name="mailBodyEntity">邮件内容</param>
 /// <param name="sendServerConfiguration">发送配置</param>
 /// <param name="client">客户端对象</param>
 /// <param name="sendResultEntity">发送结果</param>
 public static void Connection(MailBodyEntity mailBodyEntity, SendServerConfigurationEntity sendServerConfiguration,
                               SmtpClient client, SendResultEntity sendResultEntity)
 {
     try
     {
         client.Connect(sendServerConfiguration.SmtpHost, sendServerConfiguration.SmtpPort, sendServerConfiguration.IsSsl);
     }
     catch (SmtpCommandException ex)
     {
         sendResultEntity.ResultInformation = $"尝试连接时出错:{0}" + ex.Message;
         sendResultEntity.ResultStatus      = false;
     }
     catch (SmtpProtocolException ex)
     {
         sendResultEntity.ResultInformation = $"尝试连接时的协议错误:{0}" + ex.Message;
         sendResultEntity.ResultStatus      = false;
     }
     catch (Exception ex)
     {
         sendResultEntity.ResultInformation = $"服务器连接错误:{0}" + ex.Message;
         sendResultEntity.ResultStatus      = false;
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="mailBodyEntity">邮件内容</param>
        /// <param name="sendServerConfiguration">发送配置</param>
        /// <param name="client">客户端对象</param>
        /// <param name="sendResultEntity">发送结果</param>
        public static void Send(MailBodyEntity mailBodyEntity, SendServerConfigurationEntity sendServerConfiguration,
                                SmtpClient client, SendResultEntity sendResultEntity)
        {
            try
            {
                client.Send(MailMessage.AssemblyMailMessage(mailBodyEntity));
            }
            catch (SmtpCommandException ex)
            {
                switch (ex.ErrorCode)
                {
                case SmtpErrorCode.RecipientNotAccepted:
                    sendResultEntity.ResultInformation = $"收件人未被接受:{ex.Message}";
                    break;

                case SmtpErrorCode.SenderNotAccepted:
                    sendResultEntity.ResultInformation = $"发件人未被接受:{ex.Message}";
                    break;

                case SmtpErrorCode.MessageNotAccepted:
                    sendResultEntity.ResultInformation = $"消息未被接受:{ex.Message}";
                    break;
                }
                sendResultEntity.ResultStatus = false;
            }
            catch (SmtpProtocolException ex)
            {
                sendResultEntity.ResultInformation = $"发送消息时的协议错误:{ex.Message}";
                sendResultEntity.ResultStatus      = false;
            }
            catch (Exception ex)
            {
                sendResultEntity.ResultInformation = $"邮件接收失败:{ex.Message}";
                sendResultEntity.ResultStatus      = false;
            }
        }