Click here to Skip to main content
Click here to Skip to main content

A class for sending emails with attachments in C#.

, 28 Feb 2003
Rate this:
Please Sign up or sign in to vote.
A class for sending emails with attachments in C#.


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 = "";			
emailer.From = "";
emailer.AuthenticationMode = AuthenticationType.Base64;
emailer.User = "myuserid";
emailer.Password = "mypassword";
emailer.Subject = "a test message";
emailer.Body = "this is only a test";
emailer.Attachments.Add(new SmtpAttachment(@"c:\file1.exe"));
emailer.Attachments.Add(new SmtpAttachment(@"c:\file2.txt"));      

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:

emailer.Attachments.Add(new SmtpAttachment(@"c:\mypicture.jpg", 
                        "image/jpg", AttachmentLocation.Inline));

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.

Version History

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.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Steaven Woyan
Web Developer
United States United States
No Biography provided

Comments and Discussions

Generaldate Header problem with not en-US country Pinmemberfravelgue9-Nov-10 0:12 
Questiongreat work but what about SSL? PinmemberAdore C++13-Jun-09 7:32 
Generalproblems in sending emails Pinmemberammartahan16-Jul-06 4:29 
GeneralRe: problems in sending emails PingroupMihaiPopescu4-Jul-10 21:52 
Generalembedding images (inline images) Pinmemberrocder10-Jul-06 3:40 
GeneralThread-safe MessageBox in OnMailSent event handler PinmemberMichael Freidgeim3-Jul-06 18:12 
GeneralStatics in SmtpAttachment class PinmemberTiTi____20-Apr-06 1:43 
GeneralRe: Statics in SmtpAttachment class PinmemberTiTi____21-Apr-06 1:27 
GeneralHelp about to be crazy.. Pinmembercoolshad15-Mar-06 16:37 
Generalgreat! PinmemberMP3Observer14-Mar-06 13:09 
GeneralError with attachments. Pinmembery2k4life4-Feb-06 19:17 
General"Sent:" date wrong in Outlook Pinmembermichaelloveusa31-Jan-06 9:26 
Generalabout the encoding of attachments PinmemberBouarf4-Jan-06 0:20 
Questionhow to send images with in the body of mail Pinmembersreesiri8-Dec-05 2:13 
Generaldateformat not rfc PinmemberKylixs10-Oct-05 14:48 
GeneralRe: dateformat not rfc PinmemberStar936-Jan-06 9:17 
GeneralSmtpClient in .NET 2.0 PinmemberAlexander Kojevnikov14-Sep-05 23:25 
GeneralMail subject whithout correct iso-8859-1 ponctuation Pinmemberandre.lourenco@link.pt12-Sep-05 1:37 
AnswerRe: Mail subject whithout correct iso-8859-1 ponctuation Pinmembergabrielbur14-Nov-07 5:17 
GeneralLittle bug with big effect PinmemberStar937-Jul-05 0:04 
QuestionCan you see the image in your mail? Pinsusshandlim26-Apr-05 1:17 
AnswerRe: Can you see the image in your mail? PinmemberThe_Mega_ZZTer27-Sep-05 8:05 
GeneralRe: Can you see the image in your mail? Pinmembersanish197914-Sep-07 1:34 
GeneralRe: Can you see the image in your mail? Pinmemberhustyang17-Jun-13 17:52 
GeneralBCC in Header Pinmemberstilton@cheese.com13-Apr-05 4:03 
GeneralNice but Spam filters let the Emails not pass PinmemberStar935-Apr-05 15:26 
GeneralRe: Nice but Spam filters let the Emails not pass PinmemberDenis Kozlov3-Jan-06 11:37 
GeneralRe: Nice but Spam filters let the Emails not pass PinmemberStar933-Jan-06 13:02 
GeneralError message handling PinmemberStar9316-Feb-05 3:05 
GeneralDisplayName PinmemberDekkie27-Jan-05 10:44 
GeneralRe: DisplayName PinsussAnonymous16-Feb-05 2:50 
GeneralDisplayName 2 PinsussAnonymous16-Feb-05 2:55 
GeneralRe: DisplayName 2 PinsussAnonymous27-Feb-05 2:30 
GeneralRe: DisplayName 2 PinmemberStar935-Apr-05 16:00 
QuestionNice component. But how to set time? Pinmembertracerken18-Jan-05 22:55 
AnswerRe: Nice component. But how to set time? PinmemberStar9311-Apr-05 13:47 
GeneralRe: Nice component. But how to set time? Pinmembertracerken11-Apr-05 23:21 
GeneralAttachments Pinmemberh_migneron25-Oct-04 8:31 
GeneralRe: Attachments PinmemberSascha Sertel28-Oct-04 7:02 
GeneralRe: Attachments Pinmemberh_migneron28-Oct-04 7:41 
QuestionOnMailSent event in Visual Basic .Net ? PinsussMark Sanchez12-Oct-04 21:45 
AnswerRe: OnMailSent event in Visual Basic .Net ? Pinmembermr_jimmybob14-Oct-04 13:36 
GeneralInline Images not rendering correctly Pinmembers3mt3x22-Aug-04 4:17 
GeneralRe: Inline Images not rendering correctly PinmemberOhadAsor2-Feb-05 7:05 
GeneralRe: Inline Images not rendering correctly Pinmembers3mt3x2-Feb-05 23:08 
GeneralOWA Forward Issues PinsussJoel S Martin4-Aug-04 8:46 
GeneralSolution PinsussJoel S Martin4-Aug-04 9:28 
GeneralMy code. PinmemberAlexandre Kojevnikov11-Jul-04 4:00 
GeneralRe: My code. PinsussAnonymous20-Jul-04 13:47 
GeneralRe: My code. PinmemberFunkyMonkey19-Jun-06 13:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.1 | Last Updated 1 Mar 2003
Article Copyright 2002 by Steaven Woyan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid