コード例 #1
0
        public async Task SendMail(MailAddress fromAddress, IEnumerable <MailAddress> toAddress, MailTextMessage textMessage)
        {
            using (var mailKit = new MailKit.Net.Smtp.SmtpClient())
            {
                var message = new MimeMessage();
                message.From.Add(ConvertToMailboxAddress(fromAddress));
                message.To.AddRange(toAddress.Select(ConvertToMailboxAddress).ToList());
                message.Subject = textMessage.Subject;
                message.Body    = new TextPart(TextFormat.Plain)
                {
                    Text = textMessage.Contents
                };

                await mailKit.ConnectAsync(_config.Host, _config.Port, SecureSocketOptions.StartTls);

                if (_config.UseAuthenticate)
                {
                    await mailKit.AuthenticateAsync(Encoding.UTF8, _config.User, _config.Password);
                }
                await mailKit.SendAsync(FormatOptions.Default, message);

                mailKit.Disconnect(true);
            }
        }
コード例 #2
0
        public async Task SendMail(MailAddress fromAddress, IEnumerable <MailAddress> toAddress, MailTextMessage textMessage)
        {
            var message = new SendGridMessage
            {
                From             = new EmailAddress(fromAddress.Address, fromAddress.Name),
                Subject          = textMessage.Subject,
                PlainTextContent = textMessage.Contents
            };

            message.AddTos(toAddress.Select(a => new EmailAddress(a.Address, a.Name)).ToList());
            await _sendGridClient.SendEmailAsync(message);
        }