예제 #1
0
 public EmailProviderEnum GetImapProvider(ImapSettings settings)
 {
     if (settings.ImapHost == Office365ImapHost && settings.ImapPort == Office365ImapPort)
     {
         return(EmailProviderEnum.Office365);
     }
     return(EmailProviderEnum.Generic);
 }
        public virtual async Task <bool> SendEmailAsync(
            SmtpSettings smtpSettings,
            ImapSettings imapSettings,
            EmailCredentials credentials,
            BaseEmailMessage <string> message)
        {
            var streams = new List <Stream>();

            try
            {
                var emailMessage = GetEmailMessage(credentials.Email, message);
                if (emailMessage == null)
                {
                    return(false);
                }
                message.Body = _emailHelperService.AttachGuidToBody(message.TrackingGuid, message.Body);

                var bodyBuilder = new BodyBuilder {
                    HtmlBody = message.Body
                };

                using (var client = new SmtpClient())
                {
                    if (message.Attachments != null)
                    {
                        PrepareAttachments(emailMessage, streams, message.Attachments, bodyBuilder);
                    }
                    else
                    {
                        emailMessage.Body = bodyBuilder.ToMessageBody();
                    }
                    await client.ConnectAsync(smtpSettings.SmtpHost, smtpSettings.SmtpPort);

                    client.AuthenticationMechanisms.Remove("XOAUTH2");
                    await client.AuthenticateAsync(credentials.Email, credentials.Password);

                    await client.SendAsync(emailMessage);

                    await client.DisconnectAsync(true);

                    if (!string.IsNullOrWhiteSpace(message.SentFolder))
                    {
                        AppendToFolder(imapSettings, credentials, emailMessage, message.SentFolder);
                    }
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(false);
            }
            finally
            {
                streams.ForEach(x => { x.Dispose(); });
            }
        }
예제 #3
0
 public virtual async Task <bool> SendEmailAsync(
     SmtpSettings smtpSettings,
     ImapSettings imapSettings,
     EmailCredentials credentials,
     BaseEmailMessage <string> message)
 =>
 await GetEmailProvider(imapSettings, smtpSettings).SendEmailAsync(smtpSettings,
                                                                   imapSettings,
                                                                   credentials,
                                                                   message);
예제 #4
0
        private IEmailServiceProvider GetEmailProvider(ImapSettings imapSettings, SmtpSettings smtpSettings)
        {
            var imapProvider = GetImapProvider(imapSettings);
            var smtpProvider = GetSmtpProvider(smtpSettings);

            if (smtpProvider == EmailProviderEnum.Office365 && imapProvider == EmailProviderEnum.Office365)
            {
                return(new Office365EmailServiceProvider(_emailHelperService, _viewRenderService, _cacheRepository));
            }
            return(new GenericEmailServiceProvider(_emailHelperService, _viewRenderService, _cacheRepository));
        }
        public async Task <bool> SendEmailAsync(
            SmtpSettings smtpSettings,
            ImapSettings imapSettings,
            EmailCredentials credentials,
            BaseEmailMessage <string> baseEmailMessage)
        {
            var service = GetExchangeService(credentials);
            var message = PrepareMessage(service, baseEmailMessage);

            PrepareAttachments(message, baseEmailMessage.Attachments);
            await message.SendAndSaveCopy(WellKnownFolderName.SentItems);

            return(true);
        }
예제 #6
0
 public Email(bool suppressExceptions = false)
 {
     try
     {
         _suppressExceptions = suppressExceptions;
         _imapSettings       = new ImapSettings(FetchSmtpSettings());
     }
     catch (Exception)
     {
         if (!_suppressExceptions)
         {
             throw;
         }
     }
 }
        private static void AppendToFolder(ImapSettings settings, EmailCredentials credentials,
                                           MimeMessage emailMessage, string folder)
        {
            using (var imapClient = new ImapClient())
            {
                imapClient.Connect(settings.ImapHost, settings.ImapPort);
                imapClient.Authenticate(credentials.Email, credentials.Password);
                var personal   = imapClient.GetFolder(imapClient.PersonalNamespaces[0]);
                var sentFolder = personal.GetSubfolders()
                                 .FirstOrDefault(x => x.Name.ToLowerInvariant() == folder.ToLowerInvariant());
                if (sentFolder != null)
                {
                    sentFolder.Append(emailMessage);
                    sentFolder.Close();
                }

                imapClient.Disconnect(true);
            }
        }
예제 #8
0
        public Email(string server, int port, string username, string password, bool useSSL, bool suppressExceptions = false)
        {
            try
            {
                _suppressExceptions = suppressExceptions;
#if NETFRAMEWORK
                _imapSettings = new ImapSettings
                {
                    Server   = server,
                    Port     = port,
                    Username = username,
                    Password = password,
                    UseSSL   = useSSL
                };
#else
                _imapSettings = new ImapSettings
                {
                    ImapConfiguration = new ImapConfiguration
                    {
                        Host      = server,
                        Port      = port,
                        Username  = username,
                        Password  = password,
                        EnableSsl = useSSL
                    }
                };
#endif
            }
            catch (Exception)
            {
                if (!_suppressExceptions)
                {
                    throw;
                }
            }
        }
예제 #9
0
 public ImapSettings(ImapSettings settings)
 {
     ImapHost = settings.ImapHost;
     ImapPort = settings.ImapPort;
 }
예제 #10
0
 public ImapClientProxyFactory(IImapSettingsProvider settingsProvider)
 {
     _settings = settingsProvider.ProvideImapSettings();
 }