Пример #1
0
        /// <summary>
        /// Send email to single recipent
        /// </summary>
        /// <param name="smtpOptions">Pass null for default parameters</param>
        /// <param name="to">Email Address</param>
        /// <param name="from">Email Address</param>
        /// <param name="subject">Email Subject</param>
        /// <param name="plainTextMessage">If plain text message</param>
        /// <param name="htmlMessage">If html message</param>
        /// <param name="replyTo">Reply to email address</param>
        /// <returns></returns>
        public async Task <bool> SendEmailAsync(SmtpOptions smtpOptions, string to, string from,
                                                string subject, string plainTextMessage, string htmlMessage,
                                                string replyTo = null)
        {
            var hasPlainText = !string.IsNullOrWhiteSpace(plainTextMessage);
            var hasHtml      = !string.IsNullOrWhiteSpace(htmlMessage);
            var message      = new MimeMessage();

            #region Default Configuration
            if (smtpOptions == null)
            {
                smtpOptions = defaultOptions;
            }
            #endregion

            #region Argument Exceptions
            if (string.IsNullOrWhiteSpace(to))
            {
                throw new ArgumentException("no to address provided");
            }

            if (string.IsNullOrWhiteSpace(from))
            {
                throw new ArgumentException("no from address provided");
            }

            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentException("no subject provided");
            }

            if (!hasPlainText && !hasHtml)
            {
                throw new ArgumentException("no message provided");
            }
            #endregion

            message.From.Add(new MailboxAddress("", from));
            if (!string.IsNullOrWhiteSpace(replyTo))
            {
                message.ReplyTo.Add(new MailboxAddress("", replyTo));
            }
            message.To.Add(new MailboxAddress("", to));
            message.Subject = subject;

            //m.Importance = MessageImportance.Normal;
            //Header h = new Header(HeaderId.Precedence, "Bulk");
            //m.Headers.Add()

            BodyBuilder bodyBuilder = new BodyBuilder();
            if (hasPlainText)
            {
                bodyBuilder.TextBody = plainTextMessage;
            }

            if (hasHtml)
            {
                bodyBuilder.HtmlBody = htmlMessage;
            }

            message.Body = bodyBuilder.ToMessageBody();

            try
            {
                using (var client = new SmtpClient())
                {
                    await client.ConnectAsync(
                        smtpOptions.Server,
                        smtpOptions.Port,
                        smtpOptions.UseSsl)
                    .ConfigureAwait(false);

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

                    // Note: only needed if the SMTP server requires authentication
                    if (smtpOptions.RequiresAuthentication)
                    {
                        await client.AuthenticateAsync(smtpOptions.User, smtpOptions.Password)
                        .ConfigureAwait(false);
                    }

                    await client.SendAsync(message).ConfigureAwait(false);

                    await client.DisconnectAsync(true).ConfigureAwait(false);

                    return(true);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error:" + ex.Message);
                throw;
            }
        }
Пример #2
0
        /// <summary>
        ///  Send email to multiple recipent
        /// </summary>
        /// <param name="smtpOptions">Pass null for default parameters</param>
        /// <param name="toCsv">Comma seperated multiple email addresses</param>
        /// <param name="from">Email Address</param>
        /// <param name="subject">Email Subject</param>
        /// <param name="plainTextMessage">If plain text message</param>
        /// <param name="htmlMessage">If html message</param>
        /// <returns></returns>
        public async Task SendMultipleEmailAsync(SmtpOptions smtpOptions, string toCsv, string from,
                                                 string subject, string plainTextMessage, string htmlMessage)
        {
            if (smtpOptions == null)
            {
                smtpOptions = defaultOptions;
            }

            if (string.IsNullOrWhiteSpace(toCsv))
            {
                throw new ArgumentException("no to addresses provided");
            }

            if (string.IsNullOrWhiteSpace(from))
            {
                throw new ArgumentException("no from address provided");
            }

            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentException("no subject provided");
            }

            var hasPlainText = !string.IsNullOrWhiteSpace(plainTextMessage);
            var hasHtml      = !string.IsNullOrWhiteSpace(htmlMessage);

            if (!hasPlainText && !hasHtml)
            {
                throw new ArgumentException("no message provided");
            }

            var message = new MimeMessage();

            message.From.Add(new MailboxAddress("", from));
            string[] addresses = toCsv.Split(',');

            foreach (string item in addresses)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    message.To.Add(new MailboxAddress("", item));;
                }
            }

            message.Subject    = subject;
            message.Importance = MessageImportance.High;

            BodyBuilder bodyBuilder = new BodyBuilder();

            if (hasPlainText)
            {
                bodyBuilder.TextBody = plainTextMessage;
            }

            if (hasHtml)
            {
                bodyBuilder.HtmlBody = htmlMessage;
            }

            message.Body = bodyBuilder.ToMessageBody();

            using (var client = new SmtpClient())
            {
                await client.ConnectAsync(
                    smtpOptions.Server,
                    smtpOptions.Port,
                    smtpOptions.UseSsl).ConfigureAwait(false);

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

                // Note: only needed if the SMTP server requires authentication
                if (smtpOptions.RequiresAuthentication)
                {
                    await client.AuthenticateAsync(
                        smtpOptions.User,
                        smtpOptions.Password).ConfigureAwait(false);
                }

                await client.SendAsync(message).ConfigureAwait(false);

                await client.DisconnectAsync(true).ConfigureAwait(false);
            }
        }
Пример #3
0
 public EmailSender()
 {
     defaultOptions = new SmtpOptions();
 }