This library provides a simple way to send emails from your ASP.NET Core application.
Install the NuGet package into your project.
public void ConfigureServices(IServiceCollection services)
{
services.AddEmailService(options =>
{
options.Host = "smtp.example.com";
options.Port = 587;
options.Email = "[email protected]";
options.Password = "password";
options.DisplayName = "Example";
options.EnableSsl = true;
options.UseDefaultCredentials = false;
options.EmailTemplatesFolder = "EmailTemplates";
});
}
Create a file EmailTemplates/MyTemplate.html
with the following content:
<!DOCUMENT html>
<html>
<head>
<title>Activate Account</title>
<meta name="subject" content="Activate Account"/>
</head>
<body>
<h1>Activate Account</h1>
<p>Hi {{firstName}},</p>
<p>Thank you for registering with us. Please click on the link below to activate your account.</p>
<p><a href="{{link}}">Activate Account</a></p>
<p>Regards,</p>
<p>Team</p>
</body>
</html>
public class MyController : Controller
{
private readonly IEmailService _emailService;
public MyController(IEmailService emailService)
{
_emailService = emailService;
}
public async Task<IActionResult> SendEmail()
{
Dictionary<string, string> model = new Dictionary<string, string>
{
{ "firstName", "John" },
{ "link", "https://example.com/activate-account" }
};
await _emailService.SendEmailUseTemplate("[email protected]", "MyTemplate", model);
return Ok();
}
}
This project is licensed under the MIT License - see the LICENSE file for details.