This is a SMTP client implementation in C#. It handles attachments and sending the body as HTML. It can also do basic (AUTH LOGIN PLAIN) and Base64 (AUTH LOGIN) authentication.
A little while back, I went looking for a (free) class/library to send email with in C#. I found several examples, but none fit my needs. They mostly demonstrated the basics and did not get into attachments. So, I decided to write my own implementation.
I reviewed several RFCs (821,822 mostly) and set about the task. The initial process was pretty straight forward and proved a good lesson in Tcp communication in C#. The attachment process was a little confusing. I understood how to do what I needed to, but was unsure of exactly what, and in what order, to do. The RFC for that and the MIME related ones aren't exactly crystal clear. That's when I found PJ Naughter's CSMTPConnection class. His implementation helped me to see the correct sequencing/structure of the MIME parts and was overall very useful. The version on The Code Project is a little old however, I would recommend getting it from PJ's site. Many thanks to him for a great example.
There is a second project in the solution which is a basic email form that uses this class.
The main class can be used as follows
Example usage of class:
SmtpEmailer emailer = new SmtpEmailer();
emailer.Host = "mymail.host.com";
emailer.From = "email@example.com";
emailer.AuthenticationMode = AuthenticationType.Base64;
emailer.User = "myuserid";
emailer.Password = "mypassword";
emailer.Subject = "a test message";
emailer.Body = "this is only a test";
You can also send the body as HTML and include inline images by adding:
emailer.SendAsHTML = true;
When you send as HTML and you want to include images inline in the HTML, you would add the attachment as follows:
The image will be given an ID that is it filename without extension. You then refer to the image in the html as
There is also an event,
OnMailSent for use with the
SendMessageAsync method. If people find this useful, then I will post updates/enhancements along the way. Enjoy.
1.4 - Added plain text (AUTH LOGIN PLAIN) and base64 (AUTH LOGIN) authentication.
Added CC/BCC properties.
1.3 - Fixed MIME header sequences and several issues with Outlook vs Outlook Express.
1.2 - Fixed quoted-printable encoding problem and an issue with Outlook.
1.1 - Added HTML body support, better MIME handling.
1.0 - Initial release.