In this blog, you will learn how to create Outlook email messages (including MSG, EML, EMLX, or MHTML) and send emails synchronously or asynchronously using C# in .NET or .NET Core.
The email has become a primary source of exchanging messages and sharing content such as documents, images, or other types of files. The frequency of using emails is more within the online systems for sharing important notifications or documents with multiple entities. In such cases, preparing a template and sending emails to multiple stakeholders manually could be a time consuming and hectic task.
Here comes the need for an Email Automation Service using which you can create and send emails seamlessly. So in this article, I am going to teach you how to create email messages of popular Outlook formats and automate the process of sending emails programmatically. After reading this article, you will be able to:
- create and save an email message in C#,
- create an email with HTML body in C#,
- create an email with a particular encoding in C#,
- send emails synchronously using SMTP in C#,
- send emails asynchronously using SMTP in C#,
- and send bulk emails in C#.
Install Aspose.Email – C# Email Manipulation API
In order to automate the process of creating and sending email messages, we will use Aspose.Email for .NET which is a powerful email manipulation and processing API. It supports popular email formats including MS Outlook messages such as MSG, EML/EMLX, etc. You can directly download the API’s DLL or install it via NuGet Package Manager or Package Manager Console in Visual Studio.
PM> Install-Package Aspose.Email
After you have created a C# (Console, ASP.NET, etc.) application and installed Aspose.Email for .NET, you can begin creating and sending the emails in C#.
Create an Email Message in C#
In order to create or manipulate email messages, Aspose.Email provides the MailMessage class. This class lets you set all the properties of an email message (Subject, To, From and Body), add attachments, and save the message as different email formats such as EML, MSG, MHTML, etc. The following are the steps to create an email message:
- Create an instance of MailMessage class.
- Set the message’s properties.
- Save the email message using MailMessage.Save() method.
The following code sample shows how to create and save an email message in C#.
Create an Email with HTML Body in C#
The HTML tags are used to make the body of the email more structured, presentable and enriched with different elements such as tables, images, lists, and so on. In case you want to create an email with having HTML body, you use MailMessage.HtmlBody property. The following code sample shows how to create an email with HTML body in C#.
Create an Email with a Particular Encoding in C#
You can also specify your desired encoding standard to tell the browser or email application about how to interpret the characters in the email’s body. The MailMessage.BodyEncoding property is used for this purpose. The following code sample shows how to set body encoding when creating an email using C#.
Send Emails Synchronously or Asynchronously in C#
Now, once you have created the email message, you can send it to its recipients synchronously or asynchronously. The SmtpClient class lets you send the email messages using SMTP (Simple Mail Transfer Protocol). The following are the steps to send an email in C# using Aspose.Email for .NET.
- Create or load an email message using the MailMessage class.
- Create an instance of SmtpClient class and set host, username, password and port number.
- Send email synchronously or asynchronously using SmtpClient.Send or SmtpClient.BeginSend method respectively.
Send an Email Synchronously in C#
Send an Email Asynchronously in C#
Send Bulk Emails in C#
You can also create and send emails in bulk. The MailMessageCollection class is used to contain the collection of email messages you want to send. The following code sample shows how to send bulk emails in C#.
You can explore the documentation of Aspose.Email for .NET to learn more about the API.