Library.NET.Mailer
2023.11.18.1
dotnet add package Library.NET.Mailer --version 2023.11.18.1
NuGet\Install-Package Library.NET.Mailer -Version 2023.11.18.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Library.NET.Mailer" Version="2023.11.18.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Library.NET.Mailer --version 2023.11.18.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Library.NET.Mailer, 2023.11.18.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Library.NET.Mailer as a Cake Addin #addin nuget:?package=Library.NET.Mailer&version=2023.11.18.1 // Install Library.NET.Mailer as a Cake Tool #tool nuget:?package=Library.NET.Mailer&version=2023.11.18.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Library.NET.Mailer
Disclaimer
This is my personal wrapper for FluentEmail.Mailkit. It is designed to make my life easier interacting with my other programs.
Implementing IEmailer
Create a new instance:
IEmailer emailer = new FluentEmailerMailKit();
Set EmailOptions
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
}
Set SmtpOptions
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
}
Send the email
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());
}
Alternatively, you can set EmailOptions
and SmtpOptions
in .SetOptions()
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();
Methods
Options:
EmailOptions
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?
SmtpOptions
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.
SendEmail & SendEmailAsync
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();
SendEmailWithTemplate<T>(T templateModel) & SendEmailWithTemplateAsync<T>(T templateModel)
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>
Planned implementations
- Using a different renderer than
RazorRenderer()
. That is why you have to put the<PreserveCompilationContext>true</PreserveCompilationContext>
in the .csproj file.
Outside Dependencies
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- FluentEmail.MailKit (>= 3.0.2)
- FluentEmail.Razor (>= 3.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2023.11.18.1 | 252 | 11/18/2023 |
2022.4.7.2 | 466 | 4/7/2022 |
2022.4.7.1 | 450 | 4/7/2022 |