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.
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:
-
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.
-
To create a project in Visual Studio Code:
- Create a project space by creating a folder by the name OCIEmail.
- Open the OCIEmail folder in VS Code File > Open Folder.
- Open the terminal window (ctrl + ~).
- 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.0Expand your project in the VS Code Explorer window.
- Replace the contents of the Program.cs file with the following code block:
using System; using MailKit.Net.Smtp; using MailKit.Security; // Required for SecureSocketOptions when using port 25 or 587. 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 = 465; // Use port numbers 25, 587, or 465. var username = "<smtp username>"; var password = "<smtp password>"; // --- Connection Options --- // For port 465 (SSL/TLS): // Connect using SSL/TLS by setting 'useSsl' to true. client.Connect(host, port, true); /* // For port 25 or 587 (STARTTLS): // Connect with STARTTLS option instead of SSL/TLS. client.Connect(host, port, SecureSocketOptions.StartTls); */ client.Authenticate (username, password); client.Send (message); client.Disconnect (true); } Console.WriteLine("Email sent successfully !!"); } } }Important Guidelines:
-
If using port 465:
Use
client.Connect(host, port, true)for a direct SSL/TLS connection. -
If using port 25 or 587:
Use
client.Connect(host, port, SecureSocketOptions.StartTls).Make sure you have
using MailKit.Security;at the top.
-
-
Replace the following parameters with your own values in the Program.cs file:
- FROM - Replace with your sender email address. Ensure that this email address is added to the Approved Senders list in Email Delivery.
- TO - Replace with your recipient email address.
- SMTP credentials - Replace
<smtp username>and<smtp password>with your Oracle Cloud Infrastructure SMTP username and password generated in the console. - HOST - Replace with the Email Delivery SMTP endpoint. For example, smtp.us-ashburn-1.oraclecloud.com.
- Save the changes and run the following command to send the mail:
dotnet run - Review the output. If the email is successfully sent, the console displays Email sent successfully! Otherwise, it displays an error message.
- Log in to the recipient inbox to verify receipt of the email.