Пример #1
0
        public Sender(ILogger logger)
        {
            var config = new MailerConfiguration();

            _smtpSettings = config.SmtpSettings;
            _logger       = logger;
        }
 public Mailer(MailerConfiguration mailerConfiguration)
 {
     _client = new SmtpClient(mailerConfiguration.Host, mailerConfiguration.Port)
     {
         DeliveryMethod        = SmtpDeliveryMethod.Network,
         UseDefaultCredentials = false
     };
 }
        public static IWebHost BuildWebHost(string[] args)
        {
            var mongoConfiguration  = new MongoConfiguration("mongodb://localhost:27017", "Api");
            var tasksConfiguration  = new TasksConfiguration("http://localhost:34127");
            var mailerConfiguration = new MailerConfiguration("smtp.domain.com", 25);

            return(WebHost.CreateDefaultBuilder(args)
                   .UseStartup <Startup>()
                   .ConfigureServices(configureServices =>
            {
                configureServices.AddSingleton(mongoConfiguration);
                configureServices.AddSingleton(tasksConfiguration);
                configureServices.AddSingleton(mailerConfiguration);
            })
                   .Build());
        }
Пример #4
0
        private void StartApi()
        {
            var mongoConfiguration  = new MongoConfiguration(_testMongoDb.ConnectionString, "Api");
            var tasksConfiguration  = new TasksConfiguration(TasksApiBaseUrl, TasksApiBehavior.ToHttpClient());
            var mailerConfiguration = new MailerConfiguration(FakeSmtpHost, 25);

            var builder = new WebHostBuilder()
                          .UseStartup <Startup>()
                          .ConfigureServices(configureServices =>
            {
                configureServices.AddSingleton(mongoConfiguration);
                configureServices.AddSingleton(tasksConfiguration);
                configureServices.AddSingleton(mailerConfiguration);
            });

            _server = new TestServer(builder);
            Handler = _server.CreateHandler();
        }
Пример #5
0
        public bool SendEmail(ObjectId tenantId, string[] to, string subject, string body, string[] cc = null, string[] bcc = null, string reply = null)
        {
            try
            {
                MailerConfiguration mailConfig = Utilities.Instance.GetMailerConfiguration();

                MailMessage mailMessage = new MailMessage();
                SmtpClient  smtpServer  = new SmtpClient(mailConfig.ClientURL, mailConfig.ClientPort);
                mailMessage.From = new MailAddress(mailConfig.MailAddress);

                if (reply != null)
                {
                    mailMessage.ReplyToList.Add(reply);
                }
                if (cc != null)
                {
                    mailMessage.CC.Add(string.Join(",", cc));
                }
                if (bcc != null)
                {
                    mailMessage.Bcc.Add(string.Join(",", bcc));
                }
                mailMessage.To.Add(to != null && to.Length > 0 ? string.Join(",", to) : mailConfig.MailAddress);
                mailMessage.Subject      = subject;
                mailMessage.IsBodyHtml   = true;
                mailMessage.BodyEncoding = Encoding.GetEncoding("utf-8");
                mailMessage.Body         = body;

                var tenant = _tenant ?? DynamicHelper.GetTenant(_dbPool, _cache, tenantId);
                if (tenant != null)
                {
                    // If Body contains "cid:Logo_Email", add Theme_Logo_Email to attachments
                    if (body.Contains("cid:Logo_Email"))
                    {
                        var logoAttachment = new System.Net.Mail.Attachment(HttpContext.Current.Server.MapPath("~" + tenant.Theme_Logo_Email))
                        {
                            ContentId = "Logo_Email"
                        };
                        mailMessage.Attachments.Add(logoAttachment);
                    }
                    // If Body contains "cid:Bullet", add Theme_Bullet_Image to attachments
                    if (body.Contains("cid:Bullet"))
                    {
                        var bulletAttachment = new System.Net.Mail.Attachment(HttpContext.Current.Server.MapPath("~" + tenant.Theme_Bullet_Image))
                        {
                            ContentId = "Bullet"
                        };
                        mailMessage.Attachments.Add(bulletAttachment);
                    }
                }

                smtpServer.EnableSsl             = true;
                smtpServer.UseDefaultCredentials = false;
                smtpServer.Credentials           = new System.Net.NetworkCredential(mailConfig.Username, mailConfig.Password);

                smtpServer.Send(mailMessage);
            }
            catch (Exception ex)
            {
                if (_log != null)
                {
                    _log.LogError(ex, string.Join(",", to));
                }
            }

            return(true);
        }