ASKit.Mail.MailKit 1.0.0

dotnet add package ASKit.Mail.MailKit --version 1.0.0                
NuGet\Install-Package ASKit.Mail.MailKit -Version 1.0.0                
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="ASKit.Mail.MailKit" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ASKit.Mail.MailKit --version 1.0.0                
#r "nuget: ASKit.Mail.MailKit, 1.0.0"                
#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 ASKit.Mail.MailKit as a Cake Addin
#addin nuget:?package=ASKit.Mail.MailKit&version=1.0.0

// Install ASKit.Mail.MailKit as a Cake Tool
#tool nuget:?package=ASKit.Mail.MailKit&version=1.0.0                

ASKit.Mail.MailKit

About

Implementation of a service for sending emails using the MailKit library. Definitions from the ASKit.Common library are used.

namespace ASKit.Common.Mail;

// Mail service interface
public interface ISkMailService
{
    // Sending a mail message
    Task SendEmailAsync(SkMailMessage mailMessage);
}

Defining messages limited to types:

namespace ASKit.Common.Mail;

public record SkMailMessage(string To, string Subject, string Content,
                        IEnumerable<SkAttachment>? Attachments = null);
/// <summary>
/// Attachment to email message
/// </summary>
/// <param name="ContentId">File name or file path if Bytes is not specified</param>
/// <param name="ContentType">Mime type, if not specified then "application/octet-stream"</param>
/// <param name="Bytes">Content</param>
public record SkAttachment(string ContentId, string? ContentType = null, byte[]? Bytes = null);

Message includes multiple addresses in the destination fields (To, CC, BCC, ReplyTo) and also uses several different forms of addresses (RFC 5322, Appendix A.1.2, page 45).

Mailbox examples:

  • admin@domen
  • Admin <admin@domen>
  • "Admin Alex" <admin@domen>
  • "Admin Alex" <admin@domen>, supervisor@domen

Attention! Mailboxes are separated by , or ;, so these characters should not be included in the Name part of the mailbox (in quotes).

ContentType from list of known mime-types. You can use the System.Net.Mime namespace:

var contentType = (new ContentType { MediaType = MediaTypeNames.Application.Pdf }).ToString();

Configuration

appsettings.json

{
  // ...
  "MailOptions": {
    "To": "", // if defined, it is added to the main To, while the main To can be empty
    "Cc": "",
    "Bcc": "",
    "ReplyTo": "",
    "From": "",
    "TextFormat": 1, // 0 - Plain, 1 - Html (default)
    "SmtpServer": "",
    "Port": 465,
    "UseSsl": false,
    "UserName": "",
    "Password": "",
    "RequireCredentials": true,
    "DeliveryMethod": 1, // 1 - Network (default),
                         // 2 - SpecifiedPickupDirectory,
                         // 3 - NetworkAndSpecifiedPickupDirectory 
    "PickupDirectoryLocation": ""
  }
  // ...
}

program.cs

using ASKit.Mail.Extensions.DependencyInjection;
// ...
builder.Services.AddSkMailService(builder.Configuration);
// ...
var app = builder.Build();
// ...
// Testing mail service 
using (var scope = app.Services.CreateScope())
{
    var services = scope.ServiceProvider;
    var mailService = services.GetRequiredService<ISkMailService>();

    await mailService.SendEmailAsync(new SkMailMessage("Admin <unknown@gmail.com>, supervisor@domen", 
        "Test", "<h3>Body message</h3>"));

    byte[] myTextBytes = Encoding.ASCII.GetBytes("<h1>Hello, world</h1>");
    var attachments = new List<SkAttachment> { 
        new SkAttachment("appsettings.json"),
        new SkAttachment("MyFile.html", 
                        (new ContentType { MediaType = MediaTypeNames.Text.Html }).ToString(),
                        myTextBytes)
    };
    await mailService.SendEmailAsync(new SkMailMessage("Admin <unknown@gmail.com>",
        "Test with attachment", "<h3>Body message</h3>", attachments));
}
// ...
app.Run();

Resources

Product 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.

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
1.0.0 208 7/26/2023