using System; using System.Threading.Tasks; using MailKit.Net.Smtp; using MailKit.Security; using Microsoft.Extensions.Logging; using Microsoft.Extensions.OptionsModel; using MimeKit; using Yavsc.Abstract.Manage; namespace Yavsc.Services { public class MailSender : IEmailSender { private ILogger _logger; SiteSettings siteSettings; SmtpSettings smtpSettings; public MailSender( ILoggerFactory loggerFactory, IOptions sitesOptions, IOptions smtpOptions, IOptions googleOptions ) { _logger = loggerFactory.CreateLogger(); siteSettings = sitesOptions?.Value; smtpSettings = smtpOptions?.Value; } /// /// /// /// /// /// /// a MessageWithPayloadResponse, /// bool somethingsent = (response.failure == 0 && response.success > 0) /// public async Task SendEmailAsync(string username, string email, string subject, string message) { EmailSentViewModel model = new EmailSentViewModel{ EMail = email }; try { MimeMessage msg = new MimeMessage(); msg.From.Add(new MailboxAddress( siteSettings.Owner.Name, siteSettings.Owner.EMail)); msg.To.Add(new MailboxAddress(username, email)); msg.Body = new TextPart("plain") { Text = message }; msg.Subject = subject; msg.MessageId = MimeKit.Utils.MimeUtils.GenerateMessageId( siteSettings.Authority ); using (SmtpClient sc = new SmtpClient()) { sc.Connect( smtpSettings.Host, smtpSettings.Port, SecureSocketOptions.None); await sc.SendAsync(msg); model.MessageId = msg.MessageId; model.Sent = true; // a duplicate info to remove from the view model, that equals to MessageId == null } } catch (Exception ex) { model.Sent = false; model.ErrorMessage = ex.Message; return model; } return model; } } }