Last active
June 24, 2024 20:01
-
-
Save ragavendra/04b8ab957cc7e4f46dc93a58c736e8a5 to your computer and use it in GitHub Desktop.
Revisions
-
ragavendra revised this gist
Jun 24, 2024 . 1 changed file with 65 additions and 27 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,16 @@ using System.Net.Mail; namespace TodoApi.Services { public interface IEmailService { public Task SendEmail(string email, string body); } // For non - prod public class DummyEmailService : IEmailService { private readonly ILogger<EtherealEmailService> _logger; private object _dummyEmail; @@ -26,51 +29,86 @@ public async Task SendEmail(string email, string body) } } public class EtherealEmailService : IEmailService { private readonly ILogger<EtherealEmailService> _logger; // private FluentEmail.Core.IFluentEmail _fluentEmail; private SmtpClient _smtpClient; // public FluentEmailService(FluentEmail.Core.IFluentEmail fluentEmail) public EtherealEmailService(SmtpClient smtpClient) { // _fluentEmail = fluentEmail; _smtpClient = smtpClient; } public async Task SendEmail(string email, string body) { /* Enable if you are planning to send attachments var attachment = Attachment.CreateAttachmentFromString(JsonSerializer.Serialize(new { Message = "Hello World!" }), "helloworld.json", Encoding.UTF8, MediaTypeNames.Application.Json);*/ var message = new MailMessage("[email protected]", "[email protected]") { Subject = "Subject here ....", Body = "<h3>Email Body</h3><p>Contents with html!</p>", IsBodyHtml = true, }; var obj = new object(); _smtpClient.SendAsync(message, () => { // Unfortunately _logger instance is not in scope LoL Console.WriteLine($"Email has been sent to {email}"); }); } } } // In Program.cs if (builder.Environment.IsDevelopment()) builder.Services.AddTransient<IEmailService, DummyEmailService>(); // else if (builder.Environment.IsProduction()) else if (builder.Environment.IsProduction()) { /* // use fluent for prod builder.Services .AddFluentEmail("[email protected]") // .AddRazorRenderer() .AddSmtpSender("localhost", 25);*/ SmtpClient smtp = new SmtpClient { //smtp Server address Host = "smtp.ethereal.email", // UseDefaultCredentials = false, // DeliveryMethod = SmtpDeliveryMethod.Network, // Enter here that you are sending smtp User name and password for the server Port = 587, Credentials = new System.Net.NetworkCredential("[email protected]", "passHere"), EnableSsl = true }; builder.Services.AddSingleton(smtp); builder.Services.AddScoped<IEmailService, EtherealEmailService>(); } // In your controller Controller/SomeController.cs private readonly IEmailService _emailService; private readonly ILogger<SomeController> _logger; public SomeController(AppDbContext context, ILogger<SomeController> logger, IEmailService emailService) { _context = context; _logger = logger; _emailService = emailService; } } -
ragavendra created this gist
Jun 24, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,76 @@ namespace TodoApi.Services { public interface IEmailService { public Task SendEmail(string email, string body); } // For non - prod public class DummyEmailService : IEmailService { private readonly ILogger<FluentEmailService> _logger; private object _dummyEmail; public DummyEmailService(object dummyEmail) { _dummyEmail = dummyEmail; } public async Task SendEmail(string email, string body) { // await //_dummyEmail.To(email) // .Body("The body").SendAsync(); _logger.LogInformation("Email has been sent to {0}", email); } } public class FluentEmailService : IEmailService { private readonly ILogger<FluentEmailService> _logger; private FluentEmail.Core.IFluentEmail _fluentEmail; public FluentEmailService(FluentEmail.Core.IFluentEmail fluentEmail) { _fluentEmail = fluentEmail; } public async Task SendEmail(string email, string body) { // await _fluentEmail.To(email) .Body(body).SendAsync(); _logger.LogInformation("Email has been sent to {0}", email); } } } // In Program.cs if (builder.Environment.IsDevelopment()) { builder.Services.AddScoped<IEmailService, DummyEmailService>(); } else if (builder.Environment.IsProduction()) { // use fluent for prod builder.Services .AddFluentEmail("[email protected]") // .AddRazorRenderer() .AddSmtpSender("localhost", 25); builder.Services.AddScoped<IEmailService, FluentEmailService>(); } // In your controller private readonly IEmailService _emailService; public SomeController(AppDbContext context, ILogger<StopsController> logger, IEmailService emailService){ _emailService = emailService; }