Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C#
Article

A class for sending emails with attachments in C#.

Rate me:
Please Sign up or sign in to vote.
4.82/5 (62 votes)
28 Feb 20032 min read 488.9K   5.2K   127   141
A class for sending emails with attachments in C#.

Introduction

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:

C#
SmtpEmailer emailer = new SmtpEmailer();
emailer.Host = "mymail.host.com";			
emailer.From = "someone@somwhere.com";
emailer.AuthenticationMode = AuthenticationType.Base64;
emailer.User = "myuserid";
emailer.Password = "mypassword";
emailer.Subject = "a test message";
emailer.Body = "this is only a test";
emailer.To.Add("toperson1@nowhere.com");
emailer.To.Add("toperson2@nowhere.com");
emailer.Attachments.Add(new SmtpAttachment(@"c:\file1.exe"));
emailer.Attachments.Add(new SmtpAttachment(@"c:\file2.txt"));      
emailer.SendMessage();

You can also send the body as HTML and include inline images by adding:

C#
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:

C#
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

<img src="cid:MY_FILENAME_WITHOUT_EXTENSION">

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.

License

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSome comments and questions Pin
fjlaga_old25-Jun-03 7:00
fjlaga_old25-Jun-03 7:00 
GeneralRe: Some comments and questions Pin
fjlaga_old25-Jun-03 8:39
fjlaga_old25-Jun-03 8:39 
GeneralNot Working for Me -- a little help please Pin
fjlaga_old25-Jun-03 6:44
fjlaga_old25-Jun-03 6:44 
GeneralRe: Not Working for Me -- a little help please Pin
fjlaga_old25-Jun-03 8:33
fjlaga_old25-Jun-03 8:33 
GeneralProblem sending attachment (help!) Pin
nachete15-Jun-03 23:11
nachete15-Jun-03 23:11 
QuestionMHtml support in Hotmail and yahoo? Pin
rahul_p23-May-03 14:14
rahul_p23-May-03 14:14 
AnswerRe: MHtml support in Hotmail and yahoo? Pin
thom934524-Sep-03 14:35
thom934524-Sep-03 14:35 
QuestionError when the FROM is an invalid mail ? Pin
nachete8-May-03 7:11
nachete8-May-03 7:11 
AnswerRe: Error when the FROM is an invalid mail ? Pin
mouk900013-May-03 1:28
mouk900013-May-03 1:28 
GeneralRe: Error when the FROM is an invalid mail ? Pin
fjlaga_old25-Jun-03 7:05
fjlaga_old25-Jun-03 7:05 
Generalyou are guilty ... Pin
Davide Pizzolato27-Apr-03 6:26
Davide Pizzolato27-Apr-03 6:26 
QuestionPOP3 Class? Pin
wardeaux24-Apr-03 8:27
wardeaux24-Apr-03 8:27 
AnswerRe: POP3 Class? Pin
Thompsonson7-Aug-03 5:17
Thompsonson7-Aug-03 5:17 
GeneralPriority and encoding Pin
hejuns15-Apr-03 15:50
hejuns15-Apr-03 15:50 
QuestionIs it possible that you have an DecodeBodyAsQuotedPrintable ready ? Pin
Network-Solution18-Mar-03 7:32
Network-Solution18-Mar-03 7:32 
GeneralCannot Large Byte Attachment File Pin
kou.shimizu12-Mar-03 6:00
kou.shimizu12-Mar-03 6:00 
GeneralRe: Cannot Large Byte Attachment File Pin
Steaven Woyan12-Mar-03 10:17
Steaven Woyan12-Mar-03 10:17 
GeneralRe: Cannot Large Byte Attachment File Pin
kou.shimizu15-Mar-03 21:47
kou.shimizu15-Mar-03 21:47 
GeneralRe: Cannot Large Byte Attachment File Pin
Chris Porter20-Aug-04 6:18
Chris Porter20-Aug-04 6:18 
GeneralI don't get it Pin
Anonymous3-Mar-03 6:32
Anonymous3-Mar-03 6:32 
GeneralRe: I don't get it Pin
Steaven Woyan4-Mar-03 4:18
Steaven Woyan4-Mar-03 4:18 
GeneralRe: I don't get it Pin
Erick Sgarbi6-Mar-03 0:09
Erick Sgarbi6-Mar-03 0:09 
GeneralRe: I don't get it Pin
John Lyon-Smith18-Aug-03 10:04
John Lyon-Smith18-Aug-03 10:04 
GeneralGREAT!!!! Pin
lordbalrog2-Mar-03 0:09
lordbalrog2-Mar-03 0:09 
GeneralSMTP Server Login Pin
zoomba21-Feb-03 15:49
zoomba21-Feb-03 15:49 

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

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