Exemplo n.º 1
0
        private static async Task SendConfirmationMail(string from, string fromPassword, string customerMail, string customerName)
        {
            // Construct the e-mail model
            EmailComposer <WelcomeMailModel> composer = new EmailComposer <WelcomeMailModel>();
            EmailRequest <WelcomeMailModel>  request  = composer
                                                        .SetModel(new WelcomeMailModel {
                Email = customerMail, Name = customerName
            })
                                                        .SetSubject("Welcome to the company!")
                                                        .SetFrom("*****@*****.**")
                                                        .SetTo(customerMail)
                                                        .Build();

            // Creating the body using the following components:
            // * Use Scriban as the template syntax
            // * Store templates in the project under the 'Templates' application directory
            // * Map e-mail templates by the mail model name (WelcomeMailModel -> Welcome.sbnhtml)
            IMailBodyBuilder builder = new MailBodyBuilder(
                new ScribanCompiler(),
                new AppDirectoryTemplateProvider("Templates", ".sbnhtml"),
                new ViewModelTemplateResolver());

            EmailRequest populatedRequest = await builder.BuildAsync(request);

            // When using a gmail account: https://stackoverflow.com/a/25215834/1842261
            SmtpCredentials credentials = new("smtp.gmail.com", "587", "false", "true", from, fromPassword);
            IMailer         mailer      = new SmtpMailer(credentials);
            await mailer.SendMailAsync(populatedRequest);
        }
Exemplo n.º 2
0
        public async Task Smtp_SendTemplateMail_SimulateDependencyInjection_ShouldSend()
        {
            SmtpCredentials credentials = new("smtp.mailtrap.io", "587", "false", "true", "d3538ae47a016d", "d4add3690c408c");

            EmailComposer <TestMailModel> composer = new();
            EmailRequest <TestMailModel>  request  = composer
                                                     .SetModel(new TestMailModel {
                Email = "*****@*****.**", Name = "Guy Gadbois"
            })
                                                     .SetSubject("Hello world")
                                                     .SetFrom("*****@*****.**")
                                                     .SetTo("*****@*****.**")
                                                     .SetCc("*****@*****.**")
                                                     .SetBcc("*****@*****.**")
                                                     .Build();

            IMailer mailer = new SmtpMailer(credentials);

            IMailBodyBuilder builder = new MailBodyBuilder(
                new ScribanCompiler(),
                new AppDirectoryTemplateProvider("Templates", ".sbnhtml"),
                new ViewModelTemplateResolver());

            EmailRequest populatedRequest = await builder.BuildAsync(request);

            await mailer.SendMailAsync(populatedRequest);
        }
Exemplo n.º 3
0
        public async Task Smtp_SendTemplateMail_WithAttachments_ShouldSend()
        {
            SmtpCredentials credentials = new("smtp.mailtrap.io", "587", "false", "true", "d3538ae47a016d", "d4add3690c408c");

            byte[] txtBytes = File.ReadAllBytes("Attachments\\Attachment.txt");
            byte[] pdfBytes = File.ReadAllBytes("Attachments\\Attachment.pdf");

            List <Attachment> attachments = new()
            {
                new Attachment()
                {
                    ContentBytes = txtBytes, Name = "Attachment.txt"
                },
                new Attachment()
                {
                    ContentBytes = pdfBytes, Name = "Attachment.pdf"
                }
            };

            EmailComposer <TestMailModel> composer = new();
            EmailRequest <TestMailModel>  request  = composer
                                                     .SetModel(new TestMailModel {
                Email = "*****@*****.**", Name = "Guy Gadbois"
            })
                                                     .SetSubject("Hello world")
                                                     .SetFrom("*****@*****.**")
                                                     .SetTo("*****@*****.**")
                                                     .SetCc("*****@*****.**")
                                                     .SetBcc("*****@*****.**")
                                                     .Attach(attachments)
                                                     .Build();

            IMailer mailer = new SmtpMailer(credentials);

            IMailBodyBuilder builder          = new MailBodyBuilder();
            EmailRequest     populatedRequest = await builder
                                                .UseProvider(new AppDirectoryTemplateProvider("Templates", ".sbnhtml"))
                                                .UseResolver(new ViewModelTemplateResolver())
                                                .UseCompiler(new ScribanCompiler())
                                                .BuildAsync(request);

            await mailer.SendMailAsync(populatedRequest);
        }
    }
Exemplo n.º 4
0
        private void DoValidate()
        {
            RedisServer redis = new RedisServer(ConfigurationManager.Get("redisHost"), 6379, ConfigurationManager.Get("redisPassword"));

            string key = "locker-validate-" + Name;

            try
            {
                if (SpiderContext.Validations == null)
                {
                    return;
                }

                var validations = SpiderContext.Validations.GetValidations();

                if (validations != null && validations.Count > 0)
                {
                    foreach (var validation in validations)
                    {
                        validation.CheckArguments();
                    }
                }

                if (redis != null)
                {
                    while (!redis.LockTake(key, "0", TimeSpan.FromMinutes(10)))
                    {
                        Thread.Sleep(1000);
                    }
                }

                var  lockerValue          = redis?.HashGet(ValidateStatusName, Name);
                bool needInitStartRequest = lockerValue != "validate finished";

                if (needInitStartRequest)
                {
                    Logger.Info("开始数据验证 ...");

                    if (validations != null && validations.Count > 0)
                    {
                        MailBodyBuilder builder = new MailBodyBuilder(Name, SpiderContext.Validations.Corporation);
                        foreach (var validation in validations)
                        {
                            builder.AddValidateResult(validation.Validate());
                        }
                        string mailBody = builder.Build();

                        using (EmailClient client = new EmailClient(SpiderContext.Validations.EmailSmtpServer, SpiderContext.Validations.EmailUser, SpiderContext.Validations.EmailPassword, SpiderContext.Validations.EmailSmtpPort))
                        {
                            client.SendMail(new EmaillMessage($"{Name} " + "validation report", mailBody, SpiderContext.Validations.EmailTo)
                            {
                                IsHtml = true
                            });
                        }
                    }
                }
                else
                {
                    Logger.Info("有其他线程执行了数据验证.");
                }

                if (needInitStartRequest)
                {
                    redis?.HashSet(ValidateStatusName, Name, "validate finished");
                }
            }
            catch (Exception e)
            {
                Logger.Error(e.Message, e);
            }
            finally
            {
                redis?.LockRelease(key, 0);
            }
        }