コード例 #1
0
        private MailMessage CreateMessage(FWMail mail)
        {
            var message = new MailMessage
            {
                Subject    = mail.Subject,
                Body       = mail.Message,
                IsBodyHtml = mail.IsHtml,
                From       = new MailAddress(_settings.From, _settings.FromDisplayName)
            };

            foreach (var to in mail.To)
            {
                message.To.Add(new MailAddress(to.Address, to.DisplayName));
            }

            return(message);
        }
コード例 #2
0
        /// <summary>
        /// Sends an email asynchronously.
        /// </summary>
        /// <returns>A System.Threading.Tasks.Task that on completion updates the output.</returns>
        public async Task SendEmailAsync(FWMail mail)
        {
            if (mail.Debug && !_settings.Debug)
            {
                await Task.FromCanceled(new CancellationToken(true));
            }

            MailMessage message = CreateMessage(mail);

            using (var smtpClient = new SmtpClient(_settings.Host, _settings.Port))
            {
                var password = _settings.Password;
                if (_settings.SecurePassword)
                {
                    var crypto = FWEncryption.Create();
                    var key    = CreateKey();

                    password = crypto.Decrypt(password, key);
                }
                smtpClient.Credentials = new NetworkCredential(_settings.User, password);
                smtpClient.EnableSsl   = _settings.EnableSsl;
                await smtpClient.SendMailAsync(message);
            }
        }