Library.NET.Mailer
2022.4.7.2
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Library.NET.Mailer --version 2022.4.7.2
NuGet\Install-Package Library.NET.Mailer -Version 2022.4.7.2
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="2022.4.7.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Library.NET.Mailer --version 2022.4.7.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Library.NET.Mailer, 2022.4.7.2"
#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=2022.4.7.2 // Install Library.NET.Mailer as a Cake Tool #tool nuget:?package=Library.NET.Mailer&version=2022.4.7.2
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 | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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.
-
net6.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 | 232 | 11/18/2023 |
2022.4.7.2 | 446 | 4/7/2022 |
2022.4.7.1 | 428 | 4/7/2022 |