Integrating MailKit with Email Delivery

Use MailKit to send emails through the Email Delivery service.

Configure MailKit to Send Email Through Email Delivery

MailKit is an open source cross-platform email framework for .NET applications. As SmtpClient is no more a suggested approach to send mails in .NET applications, you can instead use MailKit to send and receive emails through Email Delivery. Before you use MailKit, set up a sample MailKit code and test it with the Email Delivery configuration.

Important

The following instructions contain sample code for your convenience and must be used as a reference.

To set up MailKit sample code and test the Email Delivery configuration:

  1. Ensure that Email Delivery is configured to send email. See Getting Started.

    Note

    The SMTP credentials are required to configure MailKit to use Email Delivery. Be sure to note the user name and password when you generate the SMTP credentials.

  2. To create a project in Visual Studio Code:

    1. Create a project space by creating a folder by the name OCIEmail.
    2. Open the OCIEmail folder in VS Code File > Open Folder.
    3. Open the terminal window (ctrl + ~).
    4. To create the project template and add the dependency of MailKit, run the following commands in the terminal.
      dotnet new console --framework net6.0
      dotnet add package MailKit --version 2.15.0 

      Expand your project in the VS Code Explorer window.

  3. Replace the contents of the Program.cs file with the following code block:
    using System;
    
    using MailKit.Net.Smtp;
    using MailKit;
    using MimeKit;
    
    namespace Program {
        class TestOCIEmail
        {
            public static void Main (string[] args)
            {
                var message = new MimeMessage ();
                message.From.Add (new MailboxAddress ("<FROM NAME>", "<FROM>"));
                message.To.Add (new MailboxAddress ("<TO NAME>", "<TO>"));
                message.Subject = "Mail from OCI ED service";
    
                message.Body = new TextPart ("Html") {
                    Text = @"
                    <h1>OCI Email Delivery test</h1>
                    <p>This email was sent with OCI Email Delivery using the 
                    <a href='https://github.com/jstedfast/MailKit'>MailKit Package</a>
                    for .Net .</p>"
                };
    
                using (var client = new SmtpClient ()) {
    
                    var host = "<HOST>";
                    var port = 587;
                    var username = "<smtp username>";
                    var password = "<smtp password>";
                    
                    client.Connect (host, port, false);
    
                    client.Authenticate (username, password);
    
                    client.Send (message);
                    client.Disconnect (true);
                }
               Console.WriteLine("Email send successfully !!");
            }
        }
    }
    
  4. Replace the following parameters with your own values in the Program.cs file:
    1. FROM - Replace with your sender email address. Ensure that this email address is added to the Approved Senders list in Email Delivery.
    2. TO - Replace with your recipient email address.
    3. SMTP credentials - Replace smtp_username and smtp_password with your Oracle Cloud Infrastructure SMTP username and password generated in the console.
    4. HOST - Replace with the Email Delivery SMTP endpoint. For example, smtp.us-ashburn-1.oraclecloud.com.
  5. Save the changes and run the following command to send the mail:
    dotnet run
  6. Review the output. If the email is successfully sent, the console displays Email sent successfully! Otherwise, it displays an error message.
  7. Log into the recipient inbox to verify receipt of the email.