示例#1
0
        public static MailMessage ToMailMessage(this System.Net.Mail.MailMessage message)
        {
            var notification = new MailMessage {
                To   = message.To.ToString(),
                From = message.From != null?message.From.ToString() : null,
                           Subject = message.Subject
            };

            if (message.AlternateViews.Count == 0)
            {
                throw new ArgumentException("MailMessage must contain an alternative view.", "message");
            }

            foreach (AlternateView view in message.AlternateViews)
            {
                if (view.ContentType.MediaType == "text/html")
                {
                    using (var reader = new StreamReader(view.ContentStream))
                        notification.HtmlBody = reader.ReadToEnd();
                }

                if (view.ContentType.MediaType == "text/plain")
                {
                    using (var reader = new StreamReader(view.ContentStream))
                        notification.TextBody = reader.ReadToEnd();
                }
            }

            return(notification);
        }
        public async Task SendAsync(MailMessage model)
        {
            var message = CreateMailMessage(model);

            message.Headers.Add("X-Mailer-Machine", Environment.MachineName);
            message.Headers.Add("X-Mailer-Date", SystemClock.UtcNow.ToString());
            message.Headers.Add("X-Auto-Response-Suppress", "All");
            message.Headers.Add("Auto-Submitted", "auto-generated");

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

                await client.ConnectAsync(Settings.Current.SmtpHost, Settings.Current.SmtpPort, GetSecureSocketOption(Settings.Current.SmtpEncryption)).AnyContext();

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

                if (!String.IsNullOrEmpty(Settings.Current.SmtpUser))
                {
                    await client.AuthenticateAsync(Settings.Current.SmtpUser, Settings.Current.SmtpPassword).AnyContext();
                }

                await client.SendAsync(message).AnyContext();

                await client.DisconnectAsync(true).AnyContext();
            }
        }
示例#3
0
        public static System.Net.Mail.MailMessage ToMailMessage(this MailMessage notification)
        {
            var message = new System.Net.Mail.MailMessage {
                Subject = notification.Subject
            };

            if (!String.IsNullOrEmpty(notification.To))
            {
                message.To.Add(notification.To);
            }

            if (!String.IsNullOrEmpty(notification.From))
            {
                message.From = new MailAddress(notification.From);
            }

            if (!String.IsNullOrEmpty(notification.TextBody))
            {
                message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(notification.TextBody, null, "text/plain"));
            }

            if (!String.IsNullOrEmpty(notification.HtmlBody))
            {
                message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(notification.HtmlBody, null, "text/html"));
            }

            return(message);
        }
        private MimeMessage CreateMailMessage(MailMessage notification)
        {
            var message = new MimeMessage {
                Subject = notification.Subject
            };
            var builder = new BodyBuilder();

            if (!String.IsNullOrEmpty(notification.To))
            {
                message.To.AddRange(InternetAddressList.Parse(notification.To));
            }

            if (!String.IsNullOrEmpty(notification.From))
            {
                message.From.AddRange(InternetAddressList.Parse(notification.From));
            }
            else
            {
                message.From.AddRange(InternetAddressList.Parse(Settings.Current.SmtpFrom));
            }

            if (!String.IsNullOrEmpty(notification.Body))
            {
                builder.HtmlBody = notification.Body;
            }

            message.Body = builder.ToMessageBody();
            return(message);
        }
示例#5
0
        public async Task SendAsync(MailMessage model)
        {
            var message = model.ToMailMessage();

            message.Headers.Add("X-Mailer-Machine", Environment.MachineName);
            message.Headers.Add("X-Mailer-Date", SystemClock.UtcNow.ToString());
            message.Headers.Add("X-Auto-Response-Suppress", "All");
            message.Headers.Add("Auto-Submitted", "auto-generated");

            using (var client = new SmtpClient()) {
                // accept all SSL certificates (should this be changed?)
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;

                await client.ConnectAsync(Settings.Current.SmtpHost, Settings.Current.SmtpPort, Settings.Current.SmtpEnableSsl).AnyContext();

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

                if ((Settings.Current.SmtpUser == null) != (Settings.Current.SmtpPassword == null))
                {
                    throw new ArgumentException("Must specify both the SmtpUser and the SmtpPassword, or neither.");
                }

                if (Settings.Current.SmtpUser != null)
                {
                    await client.AuthenticateAsync(Settings.Current.SmtpUser, Settings.Current.SmtpPassword).AnyContext();
                }

                await client.SendAsync(message).AnyContext();

                // we don't care if there is an error at this point.
                Interlocked.Increment(ref _messagesSent);
                await client.DisconnectAsync(true).AnyContext();
            }
        }
示例#6
0
        public static MimeMessage ToMailMessage(this MailMessage notification)
        {
            var message = new MimeMessage();
            var builder = new BodyBuilder();

            if (!String.IsNullOrEmpty(notification.To))
            {
                message.To.Add(new MailboxAddress(notification.To));
            }

            if (!String.IsNullOrEmpty(notification.From))
            {
                message.From.Add(new MailboxAddress(notification.From));
            }
            else
            {
                message.From.Add(new MailboxAddress(Settings.Current.SmtpFrom));
            }

            if (!String.IsNullOrEmpty(notification.TextBody))
            {
                builder.TextBody = notification.TextBody;
            }

            if (!String.IsNullOrEmpty(notification.HtmlBody))
            {
                builder.HtmlBody = notification.HtmlBody;
            }

            message.Body = builder.ToMessageBody();
            return(message);
        }
示例#7
0
        public async Task SendAsync(MailMessage model) {
            var client = new SmtpClient();
            var message = model.ToMailMessage();
            message.Headers.Add("X-Mailer-Machine", Environment.MachineName);
            message.Headers.Add("X-Mailer-Date", DateTime.Now.ToString());

            await client.SendMailAsync(message);

            Interlocked.Increment(ref _messagesSent);
        }
示例#8
0
        public async Task SendAsync(MailMessage model)
        {
            var client  = new SmtpClient();
            var message = model.ToMailMessage();

            message.Headers.Add("X-Mailer-Machine", Environment.MachineName);
            message.Headers.Add("X-Mailer-Date", DateTime.Now.ToString());

            await client.SendAsync(message);

            Interlocked.Increment(ref _messagesSent);
        }
        public async Task SendAsync(MailMessage model)
        {
            _logger.LogTrace("Creating Mail Message from model");

            var message = CreateMailMessage(model);

            message.Headers.Add("X-Mailer-Machine", Environment.MachineName);
            message.Headers.Add("X-Mailer-Date", SystemClock.UtcNow.ToString());
            message.Headers.Add("X-Auto-Response-Suppress", "All");
            message.Headers.Add("Auto-Submitted", "auto-generated");

            using (var client = new SmtpClient(new ExtensionsProtocolLogger(_logger))) {
                string host       = _emailOptions.SmtpHost;
                int    port       = _emailOptions.SmtpPort;
                var    encryption = GetSecureSocketOption(_emailOptions.SmtpEncryption);
                _logger.LogTrace("Connecting to SMTP server: {SmtpHost}:{SmtpPort} using {Encryption}", host, port, encryption);

                var sw = Stopwatch.StartNew();
                await client.ConnectAsync(host, port, encryption).AnyContext();

                _logger.LogTrace("Connected to SMTP server took {Duration:g}", sw.Elapsed);

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

                string user = _emailOptions.SmtpUser;
                if (!String.IsNullOrEmpty(user))
                {
                    _logger.LogTrace("Authenticating {SmtpUser} to SMTP server", user);
                    sw.Restart();
                    await client.AuthenticateAsync(user, _emailOptions.SmtpPassword).AnyContext();

                    _logger.LogTrace("Authenticated to SMTP server took {Duration:g}", user, sw.Elapsed);
                }

                _logger.LogTrace("Sending message: to={To} subject={Subject}", message.Subject, message.To);
                sw.Restart();
                await client.SendAsync(message).AnyContext();

                _logger.LogTrace("Sent Message took {Duration:g}", sw.Elapsed);

                sw.Restart();
                await client.DisconnectAsync(true).AnyContext();

                _logger.LogTrace("Disconnected from SMTP server took {Duration:g}", sw.Elapsed);
                sw.Stop();
            }

            _lastSuccessfulConnection = SystemClock.UtcNow;
        }
示例#10
0
        public async Task SendAsync(MailMessage model) {
            var message = model.ToMailMessage();
            message.Headers.Add("X-Mailer-Machine", Environment.MachineName);
            message.Headers.Add("X-Mailer-Date", DateTime.UtcNow.ToString());

            var client = new SmtpClient();
            if (!String.IsNullOrEmpty(Settings.Current.SmtpHost)) {
                client.Host = Settings.Current.SmtpHost;
                client.Port = Settings.Current.SmtpPort;
                client.EnableSsl = Settings.Current.SmtpEnableSsl;
                client.Credentials = new NetworkCredential(Settings.Current.SmtpUser, Settings.Current.SmtpPassword);
            }

            await client.SendMailAsync(message).AnyContext();
            Interlocked.Increment(ref _messagesSent);
        }
示例#11
0
        public async Task SendAsync(MailMessage model)
        {
            var message = model.ToMailMessage();

            message.Headers.Add("X-Mailer-Machine", Environment.MachineName);
            message.Headers.Add("X-Mailer-Date", SystemClock.UtcNow.ToString());

            var client = new SmtpClient();

            if (!String.IsNullOrEmpty(Settings.Current.SmtpHost))
            {
                client.Host        = Settings.Current.SmtpHost;
                client.Port        = Settings.Current.SmtpPort;
                client.EnableSsl   = Settings.Current.SmtpEnableSsl;
                client.Credentials = new NetworkCredential(Settings.Current.SmtpUser, Settings.Current.SmtpPassword);
            }

            await client.SendMailAsync(message).AnyContext();

            Interlocked.Increment(ref _messagesSent);
        }
示例#12
0
        public static MailMessage ToMailMessage(this System.Net.Mail.MailMessage message) {
            var notification = new MailMessage {
                To = message.To.ToString(),
                From = message.From != null ? message.From.ToString() : null,
                Subject = message.Subject
            };

            if (message.AlternateViews.Count == 0)
                throw new ArgumentException("MailMessage must contain an alternative view.", nameof(message));

            foreach (AlternateView view in message.AlternateViews) {
                if (view.ContentType.MediaType == "text/html")
                    using (var reader = new StreamReader(view.ContentStream))
                        notification.HtmlBody = reader.ReadToEnd();

                if (view.ContentType.MediaType == "text/plain")
                    using (var reader = new StreamReader(view.ContentStream))
                        notification.TextBody = reader.ReadToEnd();

            }

            return notification;
        }