Helpful package for generating email body, configuring SMTP Client and sending emails built in .NET Standard 2.0
.
The purpose of the library is to make it easy to send automated e-mails with dynamically amendable bodies right from within your code.
SMTP configuration is easier than ever. Choose a Port of your choice from an enum
value (set by default to the right one, probably, so you don't even have to touch it!),
Then choose your host from 352 different ones I've mapped for you. You do that by accessing a static properties, i.e. Hosts.Gmail
- sorted! You only need your login and pass now, and you're ready to go.
Need to generate some unique content for each e-mail? No problem! You can use placeholders in your e-mail template and set them to be replaced by a variable of your choice. See examples below.
Install-Package OuiMailer -Version 1.0.2
dotnet add package OuiMailer --version 1.0.2
When using dependency injection in .NET Core 3.X, you can register type like so, by registering a type in the ConfigureServices()
method:
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IEmailSender>(provider => new EmailSender(Hosts.Gmail, "myLogin", "myPassword"));
}
MyClass.cs:
public class MyClass
{
private readonly IEmailSender _sender;
public EmailManager(IEmailSender sender)
{
_sender = sender;
}
}
The example project that uses the below code can be found under OuiMailer.Example.csproj
. Feel free to clone and play around!
If you're not using DI, initialize EmailSender class with the default constructor - that's all you need!:
var emailer = new EmailSender(Hosts.Gmail, "[email protected]", "yourPassword");
You can then construct your MailMessage
object (from standard System.Net.Mail
library):
var email = new MailMessage("[email protected]", "[email protected]", "subject", "<p>This is e-mail body!</p>")
{
IsBodyHtml = true
};
You can then simply call the SendEmailAsync
asynchronous method, and that's it!:
await emailer.SendEmailAsync(email);
... Or use a second overload that only takes 3 parameters - string to
, string subject
, string body
:
emailer.From = "[email protected]";
await emailer.SendEmailAsync("[email protected]", "testing2nd overload", exampleEmailBody2);
Generating a body is really easy. Initialize the builder using EmailBodyBuilder.Init()
, then set a template - it can be a string
(SetTemplateString()
) or a file
(SetTemplateFilePath()
,
then, all you have to do is add placeholders by calling AddPlaceholder(string pattern, string replaceWith)
method. Once you have them all, call Run()
method. You can optionally pass a RegexOptions
enum (see example 2)
The code below builds an email directly from the HTML string. Notice the placeholders DATETIMEYEARNOW and HOWISIT. They get replaced dynamically with other values
IEmailBodyBuilder builder1 = EmailBodyBuilder.Init()
.SetTemplateString("<html><head></head><body><p>This will be the whole email. Current year is: DATETIMEYEARNOW and this extension is HOWISIT</body></html>")
.AddPlaceholder("DATETIMEYEARNOW", DateTime.Now.Year.ToString())
.AddPlaceholder("HOWISIT", "awesome!");
var exampleEmailBody1 = builder1.Run();
The code below builds an email from a HTML template file.
IEmailBodyBuilder builder2 = EmailBodyBuilder.Init()
.SetTemplateFilePath(@"replace-with-the-path-of-your-choice\Template\email-template.html")
.AddPlaceholder("you can replace this", "with any string")
.AddPlaceholder("check out my app!", "https://www.paving-app.com");
// notice how we're using optional RegexOptions parameter to choose how we replace the placeholders
var exampleEmailBody2 = builder2.Run(RegexOptions.IgnoreCase);
Once you get the body, you can pass it to the SendEmailAsync()
method as a string msg
parameter, or include as a Body
in the MailMessage
object (remember to set IsBodyHtml
flag to true
).
EmailSender
class implements IEmailSender
interface, which means it can be used straight out of the box when utilizing .NET Identity framework
Use tags for versioning. Check the current iteration (tag) and in cmd:
git checkout [test/master]
git pull
git tag v[Major].[Minor].[Patch]-[beta if test branch]
git push origin [version]
Then, push the code to test and merge into master.