Skip to content

Instantly share code, notes, and snippets.

@ragavendra
Last active June 24, 2024 20:01
Show Gist options
  • Save ragavendra/04b8ab957cc7e4f46dc93a58c736e8a5 to your computer and use it in GitHub Desktop.
Save ragavendra/04b8ab957cc7e4f46dc93a58c736e8a5 to your computer and use it in GitHub Desktop.
Email service DI with Ethereal free fake smtp and Dummy service for non -prod
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;
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 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment