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

 
GeneralBCC in Header Pin
stilton@cheese.com13-Apr-05 3:03
stilton@cheese.com13-Apr-05 3:03 
GeneralNice but Spam filters let the Emails not pass Pin
93Current5-Apr-05 14:26
93Current5-Apr-05 14:26 
GeneralRe: Nice but Spam filters let the Emails not pass Pin
Denis Kozlov3-Jan-06 10:37
Denis Kozlov3-Jan-06 10:37 
GeneralRe: Nice but Spam filters let the Emails not pass Pin
93Current3-Jan-06 12:02
93Current3-Jan-06 12:02 
GeneralError message handling Pin
93Current16-Feb-05 2:05
93Current16-Feb-05 2:05 
GeneralDisplayName Pin
Dekkie27-Jan-05 9:44
Dekkie27-Jan-05 9:44 
GeneralRe: DisplayName Pin
Anonymous16-Feb-05 1:50
Anonymous16-Feb-05 1:50 
GeneralDisplayName 2 Pin
Anonymous16-Feb-05 1:55
Anonymous16-Feb-05 1:55 
GeneralRe: DisplayName 2 Pin
Anonymous27-Feb-05 1:30
Anonymous27-Feb-05 1:30 
GeneralRe: DisplayName 2 Pin
93Current5-Apr-05 15:00
93Current5-Apr-05 15:00 
QuestionNice component. But how to set time? Pin
tracerken18-Jan-05 21:55
tracerken18-Jan-05 21:55 
AnswerRe: Nice component. But how to set time? Pin
93Current11-Apr-05 12:47
93Current11-Apr-05 12:47 
GeneralRe: Nice component. But how to set time? Pin
tracerken11-Apr-05 22:21
tracerken11-Apr-05 22:21 
GeneralAttachments Pin
Hugo Migneron25-Oct-04 7:31
Hugo Migneron25-Oct-04 7:31 
GeneralRe: Attachments Pin
Sascha Sertel28-Oct-04 6:02
Sascha Sertel28-Oct-04 6:02 
GeneralRe: Attachments Pin
Hugo Migneron28-Oct-04 6:41
Hugo Migneron28-Oct-04 6:41 
QuestionOnMailSent event in Visual Basic .Net ? Pin
Mark Sanchez12-Oct-04 20:45
Mark Sanchez12-Oct-04 20:45 
AnswerRe: OnMailSent event in Visual Basic .Net ? Pin
mr_jimmybob14-Oct-04 12:36
mr_jimmybob14-Oct-04 12:36 
GeneralInline Images not rendering correctly Pin
s3mt3x22-Aug-04 3:17
s3mt3x22-Aug-04 3:17 
GeneralRe: Inline Images not rendering correctly Pin
OhadAsor2-Feb-05 6:05
OhadAsor2-Feb-05 6:05 
GeneralRe: Inline Images not rendering correctly Pin
s3mt3x2-Feb-05 22:08
s3mt3x2-Feb-05 22:08 
GeneralOWA Forward Issues Pin
Joel S Martin4-Aug-04 7:46
sussJoel S Martin4-Aug-04 7:46 
GeneralSolution Pin
Joel S Martin4-Aug-04 8:28
sussJoel S Martin4-Aug-04 8:28 
GeneralMy code. Pin
Alexander Kojevnikov11-Jul-04 3:00
Alexander Kojevnikov11-Jul-04 3:00 
GeneralRe: My code. Pin
Anonymous20-Jul-04 12:47
Anonymous20-Jul-04 12:47 

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.