Email -- short for electronic mail -- is a method of exchanging messages between people using electronics. It's now being widely used in our daily communication. In this blog, I will show how to send, receive and delete email messages programmatically using C# and a .net Email component.
Prerequisite
Before start, we need to add reference to the project by use the following PowerShell command in Visual Studio Nuget Package Manager Console.
- PM> Install-Package Spire.Email
Using the code
Sending emails
MailMessage class is used to create email messages. To send email messages, we’ll need to use the SmtpClient class. For the following demo, I’m sending an email with plain text body, you can also create an email with html body by using the BodyHtml property of MailMessage object, instead of BodyText property.
- MailAddress addressFrom = "[email protected]";
- MailAddress addressTo = "[email protected]";
- MailMessage message = new MailMessage(addressFrom, addressTo);
- message.Subject = "Invitation";
- message.BodyText = "Hi Shawn,\r\n" + "This Saturday is my birthday and my parents will hold a simple celebrating party for me. I am glad to invite you to come to the party. Blair, Emma and Roan will also be invited. I am sure we will have a good time. We will have dinner at 18:30 so that you are wished to come at 18:15. My mother is a good cook and you will enjoy the dishes. After the dinner, we will play some small games and then eat the cake. My parents and I sincerely expect you to come and hope to see you then.\r\n " + "Sincerely Leon ";
- SmtpClient smtp = new SmtpClient();
- smtp.Host = "smtp.outlook.com";
- smtp.ConnectionProtocols = ConnectionProtocols.Ssl;
- smtp.Username = addressFrom.Address;
- smtp.Password = "password";
- smtp.Port = 587;
- smtp.SendOne(message);
- Console.WriteLine("From : " + message.From.ToString());
- Console.WriteLine("To : " + message.To.ToString());
- Console.WriteLine("Subject: " + message.Subject);
- Console.WriteLine("------------------- BODY -----------------");
- Console.WriteLine(message.BodyText);
- Console.WriteLine("------------------- END ------------------");
- Console.WriteLine("Message Sent.");
Receiving and saving emails
Pop3Client and ImapClient is used for receiving email messages. Let’s see how we can retrieve an email message from pop and imap server, and then save it to disk. When saving the email messages, we can choose the message format such as eml, emlx, mhtml and msg from the MailMessageFormat enumeration.
Using Pop3Client
-
- Pop3Client pop = new Pop3Client();
-
- pop.Host = "outlook.office365.com";
- pop.Username = "[email protected]";
- pop.Password = "password";
- pop.Port = 995;
- pop.EnableSsl = true;
-
- pop.Connect();
-
- MailMessage message = pop.GetMessage(1);
-
- Console.WriteLine("------------------ HEADERS ---------------");
- Console.WriteLine("From : " + message.From.ToString());
- Console.WriteLine("To : " + message.To.ToString());
- Console.WriteLine("Date : " + message.Date.ToString(CultureInfo.InvariantCulture));
- Console.WriteLine("Subject: " + message.Subject);
- Console.WriteLine("------------------- BODY -----------------");
- Console.WriteLine(message.BodyText);
- Console.WriteLine("------------------- END ------------------");
-
- message.Save(message.Subject + ".eml", MailMessageFormat.Eml);
- Console.WriteLine("Message Saved.");
Using ImapClient
-
- ImapClient imap = new ImapClient();
-
- imap.Host = "outlook.office365.com";
- imap.Port = 143;
- imap.Username = "[email protected]";
- imap.Password = "password";
- imap.ConnectionProtocols = ConnectionProtocols.Ssl;
-
- imap.Connect();
-
- imap.Select("Inbox");
-
- MailMessage message = imap.GetFullMessage(1);
-
- Console.WriteLine("------------------ HEADERS ---------------");
- Console.WriteLine("From : " + message.From.ToString());
- Console.WriteLine("To : " + message.To.ToString());
- Console.WriteLine("Date : " + message.Date.ToString(CultureInfo.InvariantCulture));
- Console.WriteLine("Subject: " + message.Subject);
- Console.WriteLine("------------------- BODY -----------------");
- Console.WriteLine(message.BodyText);
- Console.WriteLine("------------------- END ------------------");
-
- message.Save(message.Subject + ".eml", MailMessageFormat.Eml);
- Console.WriteLine("Message Saved.");
Deleting emails
All email messages can be deleted using the DeleteAllMessages() method of Pop3Client class. We can also delete a specific email message with the DeleteMessage() method.
-
- Pop3Client pop3 = new Pop3Client();
-
- pop3.Host = "outlook.office365.com";
- pop3.Username = "[email protected]";
- pop3.Password = "password";
- pop3.Port = 995;
- pop3.EnableSsl = true;
-
- pop3.Connect();
-
- Pop3MessageInfoCollection messages = pop3.GetAllMessages();
- Console.WriteLine("Number of messages before deleting: " + messages.Count);
-
- pop3.DeleteMessage(2);
-
-
-
- messages = pop3.GetAllMessages();
- Console.WriteLine("Number of messages after deleting: " + messages.Count);
Conclusion
This blog only illustrates the send, receive and delete email functions using C#. The Spire.Email component has rich features such as search email messages, add and extract attachment, get the mailbox and message information, and manipulate folders etc. And it supports C#, vb.net, and asp.net applications. You can try it yourself.