Note:
- This tutorial requires access to Oracle Cloud. To sign up for a free account, see Get started with Oracle Cloud Infrastructure Free Tier.
- It uses example values for Oracle Cloud Infrastructure credentials, tenancy, and compartments. When completing your lab, substitute these values with ones specific to your cloud environment.
Set up C-Sharp code to use Oracle Cloud Infrastructure Email Delivery service
Introduction
The Oracle Cloud Infrastructure Email Delivery service provides a fast and reliable managed solution for sending secured, high-volume marketing and transactional emails.
The Email Delivery service provides tools necessary to send application-generated email for mission-critical communications such as receipts, fraud detection alerts, multi-factor identity verification, and password resets.
Note: The platform is optimized for both bulk/marketing and transactional email and not for personal correspondence email.
If you want to send emails in C# with Oracle Cloud Infrastructure (OCI) Email Delivery service which uses modern protocols for sending mail, the .NET SmtpClient
Class is no longer used. However, we can use an open source package named Mailkit which has an email client that supports a wide range of modern protocols.
Oracle Cloud Infrastructure’s Email Deliverability team manages the platform using key deliverability metrics to ensure the best sending reputation possible for your emails. The following items are provided to you when you send emails using the Email Delivery service:
- Unique mailbox provider SMTP configurations on our Mail Transfer Agents (MTA)
- Bounce collection
- User complaint collection
- Email authentication standards
- Deliverability performance
When you use Email Delivery, it becomes your outbound email server. If you have an existing email server, you can keep it and configure it to send through Email Delivery. The Email Delivery service takes care of the feedback loops and platform reputation automatically.
Objective
Set up C# code to use Oracle Cloud Infrastructure Email Delivery service.
Prerequisites
-
Note the GitHub Mailkit address: https://github.com/jstedfast/mailkit.
-
See Microsoft’s recommendation about not using the
SMTPClient
Class for new developments when using modern protocols and recommending MailKit as an option. -
MailKit is very easy to install, assuming you already have the environment set up with .NET, and the project created with your code to send emails, run the following command to install MailKit.
\>\> dotnet add package MailKit \--version 3.5.0
Task 1: Configure OCI Email Delivery
Note: The following configuration is for example purposes and does not take into account the best practices for configuring the service.
For more information, see Deliverability Best Practices.
-
In the Oracle Cloud portal, access the OCI Email Delivery service.
-
Create a domain for sending messages.
-
Configure an Approved Sender for sending messages.
Task 2: Set up the C# code
-
Below is an example code that can be modified according to your needs, remember that an Approved Sender must already be created in the OCI Email Delivery console (as shown in Task 1).
using System; using MailKit.Net.Smtp; using MailKit; using MimeKit; namespace MyApp // Note: actual namespace depends on the project name. { internal class Program { static void Main(string\[\] args) { try { // Replace these values with the actual values for your email account and OCI Email Delivery service var fromAddress = \"[[user@testdomain.com]{.underline}](mailto:userfrom@testdomain.com)\"; var toAddress = \"[[user@example.com]{.underline}](mailto:userto@example.com)\"; var smtpUsername = \"ocid1.user.oc1..aaaaaaaa4xxxxxxxxxg6vwabewu6f4xxxxxxxxxxhpbe3d56gc7ljwt2a@ocid1.tenancy.oc1..[aaaaaaaah2c25pobzyzvkcznnozputxfxtz3qvewrqaga7z66tdjrgvigbiq.ck.com]{.underline}\"; var smtpPassword = \"XXXXXXXXXXX\"; var smtpHost = \"[[smtp.]{.underline}email[.us-ashburn-1.oci.oraclecloud.com]{.underline}](http://smtp.email.us-ashburn-1.oci.oraclecloud.com)\"; var smtpPort = 587; var message = new MimeMessage (); message.From.Add (new MailboxAddress (\"Ivan Vasquez\", fromAddress)); message.To.Add (new MailboxAddress (\"Ivan Vasquez\", toAddress)); message.Subject = \"Test Email from OCI\"; message.Body = new TextPart (\"Html\") { Text = @\" \<h1\>OCI Email Delivery test\</h1\> \<p\>This email was sent from Email Delivery Service\</p\>\" }; using (var client = new SmtpClient ()) { client.Connect (smtpHost, smtpPort, false); client.Authenticate (smtpUsername, smtpPassword); client.Send (message); client.Disconnect (true); } Console.WriteLine(\"Email send successfully !!\"); } catch (Exception ex) { Console.WriteLine(\"Error sending email: \" + ex.Message); } } } }
-
Build your code using the following command:
\>\>dotnet build
. No errors are displayed. -
Run the following command:
\>\>dotnet run
. The email was successfully sent.
Related Links
Acknowledgments
Authors - Leandro Camargo (Cloud Specialist A-Team), Ivan Vasquez (Cloud Specialist A-Team)
More Learning Resources
Explore other labs on docs.oracle.com/learn or access more free learning content on the Oracle Learning YouTube channel. Additionally, visit education.oracle.com/learning-explorer to become an Oracle Learning Explorer.
For product documentation, visit Oracle Help Center.
Set up C-Sharp code to use Oracle Cloud Infrastructure Email Delivery service
F81094-01
May 2023
Copyright © 2023, Oracle and/or its affiliates.