YGMailLib 1.0.18

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

// Install YGMailLib as a Cake Tool
#tool nuget:?package=YGMailLib&version=1.0.18                

YGMailLib

YGMailLib is a library for sending email.

Features

  • HTML Mail
  • SMTP Authentication(CLAM MD5/PLAIN/LOGIN)
  • Internatinalized Domain Name(IDN) Support
  • S/MIME signature and encryption
  • SSL/TLS Support
  • DKIM Support
  • ZIP File Support

Basic Usage

    using YGZipLib;

    public void MailSend()
    {
        // Instances should be Disposed.
        using (YGMailLib.SmtpClient mail = new SmtpClient()) {
            // SMTP Server
            mail.SmtpServer = "smtp.example.com";
            // From Address
            mail.SetAddress(SmtpClient.ADDRESS_TYPE.FROM, "from@example.com");
            // To Address
            mail.SetAddress(SmtpClient.ADDRESS_TYPE.TO, "to@example.com");
            // Subject
            mail.Subject = "Test Mail";
            // Body Text(Line feed code must be CR+LF)
            mail.BodyText = "This is test mail.";
            // Execution of mail transmission
            mail.Send();
        }
    }

  • HTML mail, Attached File
    using YGZipLib;

    public void MailSend(string subject,string body, string html)
    {
        // Instances should be Disposed.
        using (YGMailLib.SmtpClient mail = new SmtpClient())
        {
            // SMTP Server
            mail.SmtpServer = "smtp.example.com";
            // From Address
            mail.SetAddress(SmtpClient.ADDRESS_TYPE.FROM, "from@example.com");
            // To Address
            mail.SetAddress(SmtpClient.ADDRESS_TYPE.TO, "to@example.com");
            // Subject
            mail.Subject = "Test Mail";
            // Body Text(Line feed code must be CR+LF)
            mail.BodyText = "This is test mail.";
            // HTML Text(Line feed code must be CR+LF)
            mail.BodyHTML = "<html><body>This is test mail.</body></html>";
            // Attached File
            mail.SetAttachedFile("AttachedFile.zip", @"D:\AttachedFile.zip");
            // Execution of mail transmission
            mail.Send();
        }
    }

Advanced Usage

  • TCP Settings
        // SMTP Port
        mail.SmtpPort = 587;
        
        // IP address family to use
        mail.IpAddressFamily = SmtpClient.IP_ADDRESS_FAMILY.IPV4_PREFER;

        // Timeout when connecting to SMTP server (milliseconds)
        mail.ConnectionTimeout = 30000;

        // Timeout after sending SMTP command (milliseconds)
        mail.SmtpTimeout = 30000;
  • Encoding

Default,

Header
        B Encode  
BodyText  
        Content-Type: text/plain; charset="utf-8"  
        Content-Transfer-Encoding: 8bit  

HtmlText  
        Content-Type: text/html; charset="utf-8"  
        Content-Transfer-Encoding: base64  
        // Headers are B-encoded by default, but can be changed to Q-encoding.
        mail.HeaderEncoding = SmtpClient.HEADER_ENCODING.Q_ENCODE;

        // Example of changing Encoding
        mail.TextEncoding = YGMailLib.Common.TextEncodings.ISO2022JP;
        mail.TransferEncoding = SmtpClient.TRANSFER_ENCODING.ENCODING_BASE64;
        mail.HtmlTransferEncoding = SmtpClient.HTML_TRANSFER_ENCODING.ENCODING_QUOTED_PRINTABLE;

        // Caution.
        // For 7-bit and 8-bit Transfer-Encoding, line feeds are added when a line exceeds 1000 bytes.
        // If the Encoding (e.g. utf-16) is such that Newline does not become (0x0d+0x0a), BASE64 should be used for TransferEncoding in the body text.

        // Non-.net Framework environments may require the encoding provider to be registered at the beginning of the program.
        System.Text.Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  • Additional headers
        // Adding headers
        // For non-us-ascii characters or long string lengths, BorQ encoding is used.
        mail.SetAdditionalHeader("X-AddHeader", "header value");

        // Set the NoEncoding flag to true if you do not want the additional header to be BorQ encoded
        mail.SetAdditionalHeader("X-AddHeader", "header value", true);

  • SMTP Auth
        mail.SmtpAuth.UseSmtpAuth = SmtpClient.USE_SMTP_AUTH.USE;
        mail.SmtpAuth.User = "userid";
        mail.SmtpAuth.Password = "password";

        // The LOGIN and PLAIN methods are by default only available for TLS connections.
        // To use them for non-TLS connections, set the following properties.
        mail.SmtpAuth.PlainEnabled = SmtpClient.USE_SMTPAUTH_METHOD.USE;
        mail.SmtpAuth.LoginEnabled = SmtpClient.USE_SMTPAUTH_METHOD.USE;

  • S/MIME

The preparation of the certificate will not be explained. Please refer to other resources.
For more information on X509Certificate2, please refer to the documentation.

        // Signing
        mail.Smime.Signing = true;

        // Set the certificate containing the private key to a FROM address.
        mail.SetAddress(SmtpClient.ADDRESS_TYPE.FROM, "from@example.com", new System.Security.Cryptography.X509Certificates.X509Certificate2(@"D:\cert.pfx", "password"));
        // Encryption
        mail.Smime.Encryption = true;

        // Set certificates to all destination addresses. No private key required.
        mail.SetAddress(SmtpClient.ADDRESS_TYPE.TO, "to1@example.com", new System.Security.Cryptography.X509Certificates.X509Certificate2(@"D:\to1.pfx"));
        mail.SetAddress(SmtpClient.ADDRESS_TYPE.TO, "to2@example.com", new System.Security.Cryptography.X509Certificates.X509Certificate2(@"D:\to2.pfx"));

        // If the certificate and private key files are separated
        X509Certificate2 cert = new X509Certificate2(@"D:\to1_cert.pem");
        mail.SetAddress(SmtpClient.ADDRESS_TYPE.TO, "to1@example.com", RSACertificateExtensions.CopyWithPrivateKey(cert, RSA.Create(Tool.ParseRsaParamFile(@"D:\to1_privkey.pem"))));
        // Additional settings

        // Do not check certificates. If a self-signed certificate is used for verification, it is necessary to specify it.
        mail.Smime.CertificateValidation = false;

        // Certificate revocation check. Not checked by default.
        mail.Smime.CertValidRevocationMode = X509RevocationMode.Online;
  • DKIM

DNS configuration and RSAk key creation are not described. Please refer to other documentation.

        mail.Dkim.UseDkim = true;
        mail.Dkim.Domain = "example.com";
        mail.Dkim.Selector = "selector0001";
        // The private key must be in PEM or DER format.
        mail.Dkim.SetRsaParamFile(@"D:\selector0001.pem");
        // If the private key is encrypted, specify the passphrase.
        // Supported Algorithms
        //  cipher:AES-128-CBC, AES-192-CBC, AES-256-CBC, DES-CBC, DES-EDE3-CBC
        mail.Dkim.SetRsaParamFile(@"D:\selector0001.pem", "passphrase");

        // Auid (optional)
        mail.Dkim.Auid = "@example.com";

        // Headers to sign (optional)  
        // Default: from:from:reply-to:subject:date:to:cc:content-type:content-transfer-encoding
        // If the same header is specified twice, the actual number of signatures plus one is output to the signature header list of the DKIM-Signature header. Even if the header does not exist, it is output to the signature header list.
        mail.Dkim.SigHeaderFields = "to:from:date";

About RSA private key loading
Supports reading PKCS1/PKCS8 format.
The following versions are required to read PKCS8 encryption private keys.
NET Framework 4.7.2 or later
NET Standard 2.1 or later
NET (Core) 3.0 or later

  • ZIP File

Attachments can be sent in a ZIP archive.

        // Add Attachment
        mail.SetAttachedFile("Test.xlsx", @"D:\Text.xlsx");
        mail.SetAttachedFile("Test.pptx", @"D:\Text.pptx");
        mail.SetAttachedFile("Test.txt", @"D:\Text.txt");

        // Specify a ZIP archive file name.
        mail.Zip.FileName = "attached.zip";

        // To create an encrypted ZIP archive, specify a password.
        mail.Zip.Password = "password";

        // To encrypt with AES, change the Encryption Algorithm.
        mail.Zip.EncryptionAlgorithm = SmtpClient.ZIP_ENCRYPTION_ALGORITHM.AES256;

        // The encoding of the file name is obtained by "System.Text.Encoding.Default.CodePage".
        // In environments other than .net Framework, Default is utf8, so specify Encoding as necessary.
        mail.Zip.FileNameEncoding = System.Text.Encoding.GetEncoding("shift-jis");

        // Maximum number of processes for creating ZIPs archives
        // If 0, the smaller of the number of processors or 4
        mail.Zip.SemaphoreCount = 4;

  • Internatinalized Domain Name(IDN)
        // example "user@国際化Example.net"
        mail.SetAddress(SmtpClient.ADDRESS_TYPE.TO, "user@国際化Example.net");

        mail.EnableSmtpUtf8 = false;  // default
        // If the domain is an internationalized domain, the mail header and SMTP command address are converted to Punycode.
        // Header: user@xn--example-qc2l530af79n.net (user@国際化Example.net)
        // SMTP Command: <user@xn--example-qc2l530af79n.net>

        mail.EnableSmtpUtf8 = true;
        // If the SMTP server supports SMTPUTF8, addresses are sent without Punycode conversion.
        // Mail headers are without Punycode conversion only when TextEncoding is UTF8.
        // Header: user@国際化Example.net
        // SMTP Command: <user@国際化Example.net> SMTPUTF8

  • Output eml file
        // Output to a file
        mail.WriteEml(@"D:\mail.eml");
        
        // Output to Stream
        mail.WriteEml(st);

        // Output to Byte array
        byte[] emlData = mail.WriteEml();
  • Asynchronous Process
        // Some statement

        // Execution of mail transmission
        await mail.SendAsync();

        // Some statement
  • Other Settings
        // Client host name, used for Message-Id.
        mail.MachineName = "client.example.com";

        // Specifies the action to be taken when an error occurs in RCPT TO (specify destination address).
        // If IGNORE is selected, when there are multiple destinations, even if an error occurs, the command is sent if there is an accepted address.
        mail.IfRcptToCommandFails = SmtpClient.IF_RCPT_TO_FAILS.IGNORE;
        
        // The Message-Id header is not added.
        mail.MessageIdRequired = false;

        // Specifies the size of mail with large attachments to be separated (byte)
        mail.SeparatedSize = 1000000;

        // Delivery Status Notification
        mail.DSN = SmtpClient.DELIVERY_STATUS_NOTIFICATION.SUCCESS;

        // Specify the X-Mail-Agent header; if null, the library name "YGMailLib" is used. h header is not added if empty.
        mail.XMailAgent = null;

        // Change Temporary Directory from System.IO.Path.GetTempPath()
        // Temporary Directory is used for ZIP files and DKIM.
        mail.TempDir = @"D:\temp";
  • Debug
        // Transaction log can be obtained for debugging.
        System.Diagnostics.Debug.WriteLine(mail.SmtpTranLog);

Note

License

"YGMailLib" is under MIT license

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.18 138 1/18/2024
1.0.17 148 12/2/2023
1.0.16 189 6/16/2023
1.0.15 157 6/8/2023
1.0.14 152 6/5/2023
1.0.13 161 5/29/2023
1.0.12 145 5/19/2023
1.0.11 165 4/28/2023
1.0.10 161 4/28/2023
1.0.9 193 4/26/2023
1.0.8 275 4/7/2023 1.0.8 is deprecated because it has critical bugs.