Esempio n. 1
0
        /// <summary>
        /// 发送邮件并返回状态信息
        /// </summary>
        /// <returns>状态信息</returns>
        public MailState Send()
        {
            MailConfig mailConfig = ConfigHelper.Get <MailConfig>();
            MailState  mailcode   = ValidMail(mailConfig);

            if (mailcode == MailState.None)
            {
                MimeMessage mailMessage = GetMailMessage(mailConfig);
                try
                {
                    using (var client = new SmtpClient())
                    {
                        //连接到Smtp服务器
                        client.Connect(mailConfig.MailServer, mailConfig.Port, mailConfig.EnabledSsl);
                        //登陆
                        client.Authenticate(mailConfig.MailServerUserName, mailConfig.MailServerPassWord);
                        //发送
                        client.Send(mailMessage);
                        //断开
                        client.Disconnect(true);
                    }
                    mailcode = MailState.Ok;
                }
                catch (SmtpCommandException exception)
                {
                    switch (exception.StatusCode)
                    {
                    case SmtpStatusCode.MailboxNameNotAllowed:
                        mailcode = MailState.UserNameOrPasswordError;
                        break;

                    default:
                        mailcode = MailState.SendFailure;
                        break;
                    }
                    if (exception.InnerException is IOException)
                    {
                        mailcode = MailState.AttachmentSizeLimit;
                    }
                    else if (exception.InnerException is WebException)
                    {
                        if (exception.InnerException.InnerException == null)
                        {
                            mailcode = MailState.SmtpServerNotFind;
                        }
                        else if (exception.InnerException.InnerException is SocketException)
                        {
                            mailcode = MailState.PortError;
                        }
                    }
                    else
                    {
                        mailcode = MailState.NonsupportSsl;
                    }
                }
            }
            this.Msg = GetMailStateInfo(mailcode);
            return(mailcode);
        }
Esempio n. 2
0
        /// <summary>
        /// 验证邮件配置信息并返回邮件状态信息
        /// </summary>
        /// <param name="mailSettings">邮件配置文件对象</param>
        /// <returns>邮件状态信息</returns>
        private MailState ValidMail(MailConfig mailSettings)
        {
            MailState none = MailState.None;

            if (string.IsNullOrEmpty(mailSettings.MailFrom) || string.IsNullOrEmpty(mailSettings.MailServer))
            {
                return(MailState.MailConfigIsNullOrEmpty);
            }
            if (this.MailToAddressList == null)
            {
                return(MailState.NoMailToAddress);
            }
            if (string.IsNullOrEmpty(this.Subject))
            {
                return(MailState.NoSubject);
            }
            if (!string.IsNullOrEmpty(this.AttachmentFilePath) && !System.IO.File.Exists(this.AttachmentFilePath))
            {
                none = MailState.FileNotFind;
            }
            return(none);
        }
Esempio n. 3
0
        /// <summary>
        /// 得到电子邮件对象
        /// </summary>
        /// <param name="mailSettings">邮件配置文件类</param>
        /// <returns>MailMessage对象</returns>
        private MimeMessage GetMailMessage(MailConfig mailSettings)
        {
            MimeMessage message = new MimeMessage();

            //收件人地址
            foreach (MailboxAddress address in this.MailToAddressList)
            {
                message.To.Add(address);
            }
            //抄送人地址
            if (this.MailCopyToAddressList != null)
            {
                foreach (MailboxAddress address2 in this.MailCopyToAddressList)
                {
                    message.Cc.Add(address2);
                }
            }
            //回复地址
            if (this.ReplyTo != null)
            {
                message.ReplyTo.Add(this.ReplyTo);
            }
            //发件人地址
            if (!string.IsNullOrEmpty(this.FromName))
            {
                message.From.Add(new MailboxAddress(FromName, mailSettings.MailFrom));
            }
            else
            {
                message.From.Add(new MailboxAddress(mailSettings.MailFrom));
            }
            message.Priority = this.Priority;
            message.Subject  = this.Subject;
            //添加正文内容
            TextPart body;

            if (IsBodyHtml)
            {
                body = new TextPart(TextFormat.Html);
            }
            else
            {
                body = new TextPart(TextFormat.Plain);
            }
            body.SetText(Encoding.UTF8, MailBody);
            var multipart = new Multipart("mixed");

            multipart.Add(body);
            //添加附件
            if (!string.IsNullOrWhiteSpace(AttachmentFilePath))
            {
                //生产一个绝对路径
                var absolutePath = FileHelper.MapPath(AttachmentFilePath);
                //附件
                var attachment = new MimePart()
                {
                    //读取文件(只能用绝对路径)
                    Content = new MimeContent(File.OpenRead(absolutePath), ContentEncoding.Default),
                    //ContentObject = new ContentObject(File.OpenRead(absolutePath), ContentEncoding.Default),
                    ContentDisposition      = new ContentDisposition(ContentDisposition.Attachment),
                    ContentTransferEncoding = ContentEncoding.Base64,
                    //文件名字
                    FileName = Path.GetFileName(absolutePath)
                };
                //添加附件
                multipart.Add(attachment);
            }
            message.Body = multipart;

            return(message);
        }