Exemplo n.º 1
0
        public override void Send()
        {
            try
            {
                var mail = new MailMessage
                {
                    From         = From,
                    IsBodyHtml   = IsBodyHtml,
                    Subject      = Subject,
                    BodyEncoding = Encoding.UTF8,
                    Body         = Body
                };

                To?.ForEach(address => mail.To.Add(address));

                Cc?.ForEach(address => mail.To.Add(address));

                Bcc?.ForEach(address => mail.To.Add(address));

                var smtpClient = new SmtpClient
                {
                    Host           = Host,
                    Port           = Port,
                    EnableSsl      = EnableSsl,
                    Credentials    = new NetworkCredential(UserName, Password),
                    DeliveryMethod = SmtpDeliveryMethod.Network
                };

                smtpClient.Send(mail);
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }
Exemplo n.º 2
0
        public virtual void Send()
        {
            try
            {
                var mail = new MailMessage
                {
                    From         = From,
                    IsBodyHtml   = IsBodyHtml,
                    Subject      = Subject,
                    BodyEncoding = Encoding.UTF8,
                    Body         = Body
                };

                if (To != null)
                {
                    To.ForEach(address => mail.To.Add(address));
                }

                if (Cc != null)
                {
                    Cc.ForEach(address => mail.To.Add(address));
                }

                if (Bcc != null)
                {
                    Bcc.ForEach(address => mail.To.Add(address));
                }

                var smtpClient = new SmtpClient
                {
                    Host        = Host,
                    Port        = Port,
                    EnableSsl   = EnableSsl,
                    Credentials = new NetworkCredential(UserName, Password),
                };

                smtpClient.Send(mail);
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }
Exemplo n.º 3
0
        public override void Execute()
        {
            var mailMessage = new MailMessage
            {
                IsBodyHtml = true,
                Body       = HtmlBody,
                Subject    = Subject,
            };

            if (string.IsNullOrEmpty(ReplyTo) == false)
            {
                try
                {
                    mailMessage.ReplyToList.Add(new MailAddress(ReplyTo));
                }
                catch
                {
                    // we explicitly ignore bad reply to emails
                }
            }

            // Send a notification of the comment to the post author
            if (SendTo != null)
            {
                SendTo.ForEach(x => mailMessage.To.Add(x));
            }
            if (Cc != null)
            {
                Cc.ForEach(email => mailMessage.CC.Add(email));
            }
            if (Bcc != null)
            {
                Bcc.ForEach(email => mailMessage.Bcc.Add(email));
            }

            using (var smtpClient = new SmtpClient())
            {
#if !DEBUG
                smtpClient.Send(mailMessage);
#endif
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// SMTP邮件Sender的类的封装
        /// </summary>
        public override async Task <bool> Send()
        {
            var isSended = false;

            try
            {
                //qq域名邮箱的smtp(smtp.qq.com);如果是腾讯企业邮箱,需要改成smtp.exmail.qq.com
                var smtpServer = "smtp.qq.com";
                var port       = 25;              //端口号
                var userName   = "******"; //用户名
                var password   = "******";        //密码

                var message = new MailMessage();
                To.ForEach(email =>
                {
                    if (email.IsEmail())
                    {
                        message.To.Add(email);
                    }
                });
                message.Sender = new MailAddress(userName, userName);
                if (Cc.AnyOne())
                {
                    Cc.ForEach(email =>
                    {
                        if (email.IsEmail())
                        {
                            message.CC.Add(email);
                        }
                    });
                }
                message.Subject = Encoding.Default.GetString(Encoding.Default.GetBytes(Subject));
                if (From.IsBlank())
                {
                    From = userName;
                }
                message.From = DisplayName.IsBlank() ? new MailAddress(From) : new MailAddress(From, Encoding.Default.GetString(Encoding.Default.GetBytes(DisplayName)));

                message.Body                        = Encoding.Default.GetString(Encoding.Default.GetBytes(Body));
                message.BodyEncoding                = Encoding.GetEncoding("GBK");
                message.HeadersEncoding             = Encoding.UTF8;
                message.SubjectEncoding             = Encoding.UTF8;
                message.IsBodyHtml                  = true;
                message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                message.ReplyToList.Add(new MailAddress(From));
                if (Attachments.AnyOne())
                {
                    Attachments.ForEach(x =>
                    {
                        message.Attachments.Add(new Attachment(x));
                    });
                }

                var smtp = new SmtpClient(smtpServer, port)
                {
                    DeliveryMethod        = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials           = new NetworkCredential(userName, password),
                    Timeout   = 30000,
                    EnableSsl = true
                };

                smtp.SendCompleted += smtp_SendCompleted;
                await smtp.SendMailAsync(message);

                isSended = true;
            }
            catch (Exception ex)
            {
                Log.Error("发送邮件失败", ex);
            }
            return(isSended);
        }