This is my personal wrapper for FluentEmail.Mailkit. It is designed to make my life easier interacting with my other programs.
IEmailer emailer = new FluentEmailerMailKit();
emailer.EmailOptions = new()
{
FromAddress = new("sender@test.com", "Tester"),
ToAddresses = new List<AddressModel>() { new("recipient@test.com"), new("recipient2@test.com") },
CcAddresses = new List<AddressModel>() { new("recipient3@test.com"), new("recipient4@test.com") },
BccAddresses = new List<AddressModel>() { new("recipient5@test.com"), new("recipient6@test.com") },
ReplyToAddress = new(),
Subject = "Test from Sender",
Body = "This is a test from the Tester program",
Attachments = new List<FileInfo>() { new(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "New Text Document.txt")) },
Template = String.Empty,
IsBodyHTML = true
}
emailer.SmtpOptions = new()
{
Server = "mailserver.test.com",
Port = 25,
User = String.Empty,
Password = String.Empty,
UseSsl = false,
RequiresAuthentication = false,
PreferredEncoding = String.Empty,
UsePickupDirectory = false,
MailPickupDirectory = String.Empty,
SocketOptions = SecureSocketOptions.Auto
}
SendResponse result = await emailer.SetOptions().SendEmailAsync();
if (result.Successful)
{
Console.WriteLine("Success!");
}
else
{
StringBuilder stringBuilder = new();
foreach (var err in result.ErrorMessages)
{
stringBuilder.AppendLine(err);
}
Console.WriteLine(stringBuilder.ToString());
}
emailer.SetOptions(
new AddressModel("sender@test.com", "Tester"),
new List<AddressModel>() { new("recipient@test.com"), new("recipient2@test.com") },
new List<AddressModel>() { new("recipient3@test.com"), new("recipient4@test.com") },
new List<AddressModel>() { new("recipient5@test.com"), new("recipient6@test.com") },
new AddressModel(),
"Test from Sender",
"This is a test from the Tester program",
new List<FileInfo>() { new(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "New Text Document.txt")) },
"",
true,
"mailserver.test.com",
25,
"",
"",
false,
false,
"",
false,
"",
SecureSocketOptions.Auto
)
.SendEmail();
fromAddress
: Sender informationreplyToAddress
(optional): Address info for replies. Defaults tofromAddress
.toAddresses
,ccAddresses
(optional),bccAddresses
(optional): Recipient listssubject
(optional): Subject line. Defaults tostring.Empty
.body
(optional): The body of the email. Defaults tostring.Empty
.template
(optional): Template for the email body. Defaults tostring.Empty
. (see example below)isBodyHtml
(optional): Should thebody
be rendered as HTML?
server
: SMTP server name or IP addressport
(optional): TCP port the emailer uses to send the mail. Defaults to25
.user
,password
(both optional ifrequiresAuthentication
isfalse
): Credentials passed to the SMTP server for sending mail. Defaults tostring.Empty
.useSSL
(optional): Does the server use SSL? Defaults tofalse
.requiresAuthentication
(optional): Does the server require authentication to send mail? Defaults tofalse
.preferredEncoding
(optional): Preferred encoding for the email. Defaults tostring.Empty
.usePickupDirectory
(optional): Does the email use a pickup directory for delivery? Defaults tofalse
.mailPickupDirectory
(optional): Pickup directory for mail delivery. Defaults tostring.Empty
.socketOptions
(optional): Provides a way of specifying the SSL and/or TLS encryption that should be used for a connection. Defaults toAuto
, that is, If the server does not support SSL or TLS, then the connection will continue without any encryption.
SendResponse result = await emailer.SetOptions().SendEmailAsync();
SendResponse result = emailer.SetOptions(
new AddressModel("sender@test.com", "Tester"),
new List<AddressModel>() { new("recipient@test.com"), new("recipient2@test.com") },
new List<AddressModel>() { new("recipient3@test.com"), new("recipient4@test.com") },
new List<AddressModel>() { new("recipient5@test.com"), new("recipient6@test.com") },
new AddressModel(),
"Test from Sender",
"This is a test from the Tester program",
new List<FileInfo>() { new(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "New Text Document.txt")) },
"",
true,
"mailserver.test.com",
25,
"",
"",
false,
false,
"",
false,
"",
SecureSocketOptions.Auto
)
.SendEmail();
var template = new StringBuilder();
template.AppendLine("Dear @Model.FirstName,");
template.AppendLine("<p>Thanks for purchasing @Model.ProductName. We hope you enjoy it! </p>");
template.AppendLine("- COS Team");
emailer.EmailOptions.Template = template.ToString();
SendResponse result = emailer.SetOptions().SendEmailWithTemplate(new {FirstName = "John", ProductName = "Epic Razors"});
NOTE: to make this work, you have to add the following to your .csproj file: per this MS doc
<PropertyGroup>
...
<PreserveCompilationContext>true</PreserveCompilationContext>
...
</PropertyGroup>
- Using a different renderer than
RazorRenderer()
. That is why you have to put the<PreserveCompilationContext>true</PreserveCompilationContext>
in the .csproj file.