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
- //Create a POP3 client
- Pop3Client pop = new Pop3Client();
- //Set host, username, password etc. for the client
- pop.Host = "outlook.office365.com";
- pop.Username = "[email protected]";
- pop.Password = "password";
- pop.Port = 995;
- pop.EnableSsl = true;
- //Connect the server
- pop.Connect();
- //Get the first message by its sequence number
- MailMessage message = pop.GetMessage(1);
- //Parse the message
- 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 ------------------");
- //Save the message to disk using its subject as file name
- message.Save(message.Subject + ".eml", MailMessageFormat.Eml);
- Console.WriteLine("Message Saved.");


Nagaraju GrandhiPosted Mar 20, 2019, 1:31 AM
Can i send mails using IMAP or POP protocol in C#. if yes give me information to how
Nagaraju GrandhiPosted Mar 20, 2019, 1:29 AM
Hello Dheeraj,