On this blog I regularly publish articles with tips and tricks for the programming language C# .Net.
C# is a modern, object-oriented programming language, which fully ultilizes the possibilites of the .Net framework. I also write about app programming for Android via C#, as well as PHP and Matlab. The difficulty of this blog is supposed to be variable, I hope for beginners and experts there is always something to look for.
If you have questions or suggestions, I am happy about your emails.

Wednesday, October 26, 2016

A long time ago I described how to send and receive emails via C#. At that time this was possible without SSL encryption, but nowadays this is mandatory for the most providers. In this post I thus want to show, how to send and receive emails using the SSL encryption. The core principle stays the same as described in the previous posts - for the basics the reader is thus referred to these.
As another point this post shall explain how emails can be read from an IMAP server - in the last post only a POP3 server was used. Further, a usable email client in C# is presented as well as access information for common email providers.

Briefly repeating the basics: For sending emails .Net functions can be used. For this the class SmtpClient is used, to which we pass the login information, and then send an email as an instance of the class MailMessage.

Receiving emails is a bit more tedious, as for this we have to manually send commands to the server and read its response. For this the usage of external libraries is possible, which abstract the lower layers.
In this post I want to show though how communication with an email server looks like with more details, and implement every part of it.
With a TcpClient we establish the connection to the mail server and then initialize an SslStream, with which we send the commands to the server and receive its responses.
For this there are differencs between a POP3 and an IMAP server.
Possible commands for a POP3 and IMAP server can be found here and here, further I described the commands for a POP3 server in my previous post.
Here I assume your understanding of the concept (but I also think they are kind of self-explanatory). Briefly to the different message format: A POP3 sever ends queries to list all possible emails and show a specific email with the line ".", thus we let the client read as long as it encounters this line. When sending a command name X to an IMAP server, it answers with X OK upon completion - thus we wait for this line.

Below you find the complete code of an email client, with which emails can be sent and received.
The needed accout information (i.e. server, port, username and password) are the same as they have to be entered for example in Outlook.
Here an overview over common providers:

// The POP3 commands LIST and RETR, for which this function is used, finished by outputting "."// as last line.// Thus read as long as this line is found.string TempLine = StreamListener.ReadLine();
while (TempLine != ".")
{
Res.Append(TempLine + "\r\n");
TempLine = StreamListener.ReadLine();
}