Пример #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);
            }
        }
Пример #2
0
        public MailMessage ToMailMessage()
        {
            MailMessage mailMessage = null;

            if (IsValid)
            {
                mailMessage = new MailMessage
                {
                    From       = (EmailAddress)From,
                    Subject    = Subject,
                    Body       = Body,
                    IsBodyHtml = IsBodyHtml,
                };

                if (!To.IsEmpty())
                {
                    To.ForEach(e => mailMessage.To.Add(e));
                }

                if (!Bcc.IsEmpty())
                {
                    Bcc.ForEach(e => mailMessage.Bcc.Add(e));
                }
            }
            return(mailMessage);
        }
Пример #3
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);
            }
        }
Пример #4
0
        /// <summary>
        /// Converts <see cref="MailMessage"/> to <see cref="System.Net.Mail.MailMessage"/>.
        /// </summary>
        /// <returns>A <see cref="System.Net.Mail.MailMessage"/>.</returns>
        internal System.Net.Mail.MailMessage ToMailMessage()
        {
            var mail = new System.Net.Mail.MailMessage();

            if (From != null)
            {
                mail.From = new MailAddress(From);
            }
            To.ForEach(a => mail.To.Add(new MailAddress(a)));
            ReplyToList.ForEach(a => mail.ReplyToList.Add(new MailAddress(a)));
            Bcc.ForEach(a => mail.Bcc.Add(new MailAddress(a)));
            CC.ForEach(a => mail.CC.Add(new MailAddress(a)));

            foreach (var header in Headers)
            {
                mail.Headers[header.Key] = header.Value;
            }

            mail.Body = Body;
            if (BodyEncoding != null)
            {
                mail.BodyEncoding = BodyEncoding;
            }
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions;
            mail.IsBodyHtml = IsBodyHtml;
            if (Sender != null)
            {
                mail.Sender = new MailAddress(Sender);
            }
            mail.Subject = Subject;
            if (SubjectEncoding != null)
            {
                mail.SubjectEncoding = SubjectEncoding;
            }

            mail.Priority = Priority;

            return(mail);
        }