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); }
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); }
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); } }