English | 简体中文
Email, as a messenger of information in the digital age, has transcended the limitations of time and space and, with its unique and far-reaching influence, has been quietly integrated into the veins of civilisation.
For cloud neutrality, we may need a tool that can help us manage our email more efficiently. Aoxe.Email is such a tool. It is a powerful email client that can help you send emails, and avoids being tied to the cloud.
The Aoxe.Email has the following implementations for sending emails:
This is the first release of version 2 of the Amazon SES API. You can use this API to configure your Amazon SES account, and to send email. This API extends the functionality that exists in the previous version of the Amazon SES API.
This client library enables working with the Microsoft Azure Communication Email service.
MailKit is an Open Source cross-platform .NET mail-client library that is based on MimeKit and optimized for mobile devices.
Use System.Net.Mail.SmtpClient.
PM> Install-Package Aoxe.Aws.SimpleEmail
PM> Install-Package Aoxe.Azure.Email
PM> Install-Package Aoxe.MailKit
PM> Install-Package Aoxe.SmtpClient
All the implements has the same abstractions so we can easily switch between them.
// Please replace the host and port with your own, you can use user name and password for the authentication and so on
serviceCollection.AddMailKit("your host", 25);
serviceCollection.AddSmtpClient("your host", 25);
// The aws and azure email provider has different Add method to match their own constructors
serviceCollection.AddAwsSimpleEmail("awsAccessKeyId", "awsSecretAccessKey");
serviceCollection.AddAzureEmail("connectionString");
Inject the email provider by reference Aoxe.Email.Abstractions.
PM> Install-Package Aoxe.Email.Abstractions
public class EmailService
{
private readonly IEmailProvider _emailProvider;
public EmailService(IEmailProvider emailProvider)
{
_emailProvider = emailProvider;
}
public async Task SendEmailAsync(string subject, string text, string fromAddress, string toAddress)
{
var email = new Abstractions.Models.Email();
email
.Subject(subject)
.TextBody(email)
.EmailFrom(fromAddress)
.EmailTo(toAddress);
// You can send the email with extension method
await email.SendByAsync(_emailProvider);
// Or you can send the email with the provider
await _emailProvider.SendAsync(email);
}
}
All email providers support auto lazy registration, which can be useful for improving performance and reducing resource usage. Here is an example of how to use:
public class EmailService
{
private readonly Lazy<IEmailProvider> _emailProvider;
public EmailService(Lazy<IEmailProvider> emailProvider)
{
_emailProvider = emailProvider;
}
public async Task SendEmailAsync(string subject, string text, string fromAddress, string toAddress)
{
var email = new Abstractions.Models.Email();
email
.Subject(subject)
.TextBody(email)
.EmailFrom(fromAddress)
.EmailTo(toAddress);
// Get the email provider from Lazy<T>
var provider = _emailProvider.Value;
// You can send the email with extension method
await email.SendByAsync(provider);
// Or you can send the email with the provider
await provider.SendAsync(email);
}
}
The Aoxe.Email.Abstractions.Models.Email class is a comprehensive representation of an email message. Here's a brief overview of its structure and functionality:
- It uses the primary constructor to optionally set an ID.
- It has properties for various email components like From, Sender, ReplyTo, Recipients, Content, and Attachments.
- The class implements a fluent interface pattern, allowing method chaining for easy email composition.
Key features:
- Attachment handling (single and multiple)
- Setting email subject, text body, and HTML body
- Managing email addresses (From, To, Cc, Bcc, ReplyTo, Sender)
- Each method returns this, enabling method chaining.
You can new an Email like this:
var (fileName, fileBytes) = await FileHelper.LoadFileBytesAsync("AttachmentTestFile.txt");
_ = new Models.Email
{
From = new EmailAddress("[email protected]", "from"),
Sender = new EmailAddress("[email protected]", "sender"),
Recipients = new EmailRecipients
{
To = [new EmailAddress("[email protected]", "to")],
Cc = [new EmailAddress("[email protected]", "cc")],
Bcc = [new EmailAddress("[email protected]", "bcc")],
},
ReplyTo = [new EmailAddress("[email protected]", "replyTo")],
Content = new EmailContent
{
Subject = "Subject",
TextBody = "TextBody",
HtmlBody = "HtmlBody",
},
Attachments = [new EmailAttachment(fileName, fileBytes)]
};
Also you can use a fluent style:
var email = new Models.Email();
email
.Subject("subject")
.TextBody("textBody")
.HtmlBody("htmlBody")
.EmailFrom("address", "name")
.EmailTo("address0", "name0")
.EmailTo([new EmailAddress("address1", "name1"), new EmailAddress("address2", "name2")])
.EmailCc("address0", "name0")
.EmailCc([new EmailAddress("address1", "name1"), new EmailAddress("address2", "name2")])
.EmailBcc("address0", "name0")
.EmailBcc(
[new EmailAddress("address1", "name1"), new EmailAddress("address2", "name2")]
)
.EmailReplyTo("address0", "name0")
.EmailReplyTo(
[new EmailAddress("address1", "name1"), new EmailAddress("address2", "name2")]
)
.EmailSender("address", "name");
var (fileName, fileBytes) = await FileHelper.LoadFileBytesAsync("AttachmentTestFile.txt");
email
.Attach(fileName, fileBytes)
.Attach([new(fileName, fileBytes), new(fileName, fileBytes)]);
In each implement package the Aoxe.Email.Abstractions.Models.Email has its extension methord to convert it into the specify email model which the email client want:
// Return Amazon.SimpleEmailV2.Model.SendEmailRequest for IAmazonSimpleEmailServiceV2
email.ToSendEmailRequest();
// Return Azure.Communication.Email.EmailMessage for Azure.Communication.Email.EmailClient
email.ToEmailMessage();
// Return MimeKit.MimeMessage for IMailTransport
email.ToMimeMessage();
// Return System.Net.Mail.MailMessage for System.Net.Mail.SmtpClient
email.ToMailMessage();
This will help you to use the email client with more customization, here is the example in Aoxe.MailKit:
var email = new Models.Email();
var mimeMessage = email.ToMimeMessage();
// Just a demo so use memory stream.
var smtpClient = new SmtpClient(new ProtocolLogger(new MemoryStream()));
await smtpClient.ConnectAsync(host, port);
// If need authenticate
await smtpClient.AuthenticateAsync(userName, password);
await _mailTransport.SendAsync(mimeMessage, cancellationToken);
Thank`s for JetBrains for the great support in providing assistance and user-friendly environment for my open source projects.