Click here to Skip to main content
16,019,976 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 513.6K   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

 
QuestionEmail Receiving Notification Pin
Member 1205378011-Nov-15 20:42
Member 1205378011-Nov-15 20:42 
Generaldate Header problem with not en-US country Pin
fravelgue8-Nov-10 23:12
fravelgue8-Nov-10 23:12 
Questiongreat work but what about SSL? Pin
AhmedOsamaMoh13-Jun-09 6:32
AhmedOsamaMoh13-Jun-09 6:32 
Generalproblems in sending emails Pin
ammartahan16-Jul-06 3:29
ammartahan16-Jul-06 3:29 
GeneralRe: problems in sending emails Pin
MihaiPopescu4-Jul-10 20:52
MihaiPopescu4-Jul-10 20:52 
Generalembedding images (inline images) Pin
rocder10-Jul-06 2:40
rocder10-Jul-06 2:40 
GeneralThread-safe MessageBox in OnMailSent event handler Pin
Michael Freidgeim3-Jul-06 17:12
Michael Freidgeim3-Jul-06 17:12 
GeneralStatics in SmtpAttachment class Pin
TiTi____20-Apr-06 0:43
TiTi____20-Apr-06 0:43 
GeneralRe: Statics in SmtpAttachment class Pin
TiTi____21-Apr-06 0:27
TiTi____21-Apr-06 0:27 
GeneralHelp ..im about to be crazy.. Pin
coolshad15-Mar-06 15:37
coolshad15-Mar-06 15:37 
Generalgreat! Pin
MP3Observer14-Mar-06 12:09
MP3Observer14-Mar-06 12:09 
GeneralError with attachments. Pin
y2k4life4-Feb-06 18:17
y2k4life4-Feb-06 18:17 
General"Sent:" date wrong in Outlook Pin
michaelloveusa31-Jan-06 8:26
michaelloveusa31-Jan-06 8:26 
Generalabout the encoding of attachments Pin
Bouarf3-Jan-06 23:20
Bouarf3-Jan-06 23:20 
Questionhow to send images with in the body of mail Pin
sreesiri8-Dec-05 1:13
sreesiri8-Dec-05 1:13 
Generaldateformat not rfc Pin
Kylixs10-Oct-05 13:48
Kylixs10-Oct-05 13:48 
GeneralRe: dateformat not rfc Pin
93Current6-Jan-06 8:17
93Current6-Jan-06 8:17 
GeneralSmtpClient in .NET 2.0 Pin
Alexander Kojevnikov14-Sep-05 22:25
Alexander Kojevnikov14-Sep-05 22:25 
GeneralMail subject whithout correct iso-8859-1 ponctuation Pin
Member 64558312-Sep-05 0:37
Member 64558312-Sep-05 0:37 
AnswerRe: Mail subject whithout correct iso-8859-1 ponctuation Pin
gabrielbur14-Nov-07 4:17
gabrielbur14-Nov-07 4:17 
GeneralLittle bug with big effect Pin
93Current6-Jul-05 23:04
93Current6-Jul-05 23:04 
QuestionCan you see the image in your mail? Pin
handlim26-Apr-05 0:17
handlim26-Apr-05 0:17 
AnswerRe: Can you see the image in your mail? Pin
The_Mega_ZZTer27-Sep-05 7:05
The_Mega_ZZTer27-Sep-05 7:05 
GeneralRe: Can you see the image in your mail? Pin
sanish197914-Sep-07 0:34
sanish197914-Sep-07 0:34 
GeneralRe: Can you see the image in your mail? Pin
hustyang17-Jun-13 16:52
hustyang17-Jun-13 16:52 

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.