Click here to Skip to main content
11,479,034 members (59,542 online)
Click here to Skip to main content

A class for sending emails with attachments in C#.

, 28 Feb 2003 337.9K 3.6K 123
Rate this:
Please Sign up or sign in to vote.
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:

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:

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

 <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

Share

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 Pin
fravelgue9-Nov-10 0:12
memberfravelgue9-Nov-10 0:12 
Questiongreat work but what about SSL? Pin
Adore C++13-Jun-09 7:32
memberAdore C++13-Jun-09 7:32 
Generalproblems in sending emails Pin
ammartahan16-Jul-06 4:29
memberammartahan16-Jul-06 4:29 
GeneralRe: problems in sending emails Pin
MihaiPopescu4-Jul-10 21:52
groupMihaiPopescu4-Jul-10 21:52 
Generalembedding images (inline images) Pin
rocder10-Jul-06 3:40
memberrocder10-Jul-06 3:40 
GeneralThread-safe MessageBox in OnMailSent event handler Pin
Michael Freidgeim3-Jul-06 18:12
memberMichael Freidgeim3-Jul-06 18:12 
GeneralStatics in SmtpAttachment class Pin
TiTi____20-Apr-06 1:43
memberTiTi____20-Apr-06 1:43 
GeneralRe: Statics in SmtpAttachment class Pin
TiTi____21-Apr-06 1:27
memberTiTi____21-Apr-06 1:27 
GeneralHelp ..im about to be crazy.. Pin
coolshad15-Mar-06 16:37
membercoolshad15-Mar-06 16:37 
Generalgreat! Pin
MP3Observer14-Mar-06 13:09
memberMP3Observer14-Mar-06 13:09 
GeneralError with attachments. Pin
y2k4life4-Feb-06 19:17
membery2k4life4-Feb-06 19:17 
General"Sent:" date wrong in Outlook Pin
michaelloveusa31-Jan-06 9:26
membermichaelloveusa31-Jan-06 9:26 
Generalabout the encoding of attachments Pin
Bouarf4-Jan-06 0:20
memberBouarf4-Jan-06 0:20 
Questionhow to send images with in the body of mail Pin
sreesiri8-Dec-05 2:13
membersreesiri8-Dec-05 2:13 
Generaldateformat not rfc Pin
Kylixs10-Oct-05 14:48
memberKylixs10-Oct-05 14:48 
GeneralRe: dateformat not rfc Pin
Star936-Jan-06 9:17
memberStar936-Jan-06 9:17 
GeneralSmtpClient in .NET 2.0 Pin
Alexander Kojevnikov14-Sep-05 23:25
memberAlexander Kojevnikov14-Sep-05 23:25 
GeneralMail subject whithout correct iso-8859-1 ponctuation Pin
andre.lourenco@link.pt12-Sep-05 1:37
memberandre.lourenco@link.pt12-Sep-05 1:37 
AnswerRe: Mail subject whithout correct iso-8859-1 ponctuation Pin
gabrielbur14-Nov-07 5:17
membergabrielbur14-Nov-07 5:17 
GeneralLittle bug with big effect Pin
Star937-Jul-05 0:04
memberStar937-Jul-05 0:04 
QuestionCan you see the image in your mail? Pin
handlim26-Apr-05 1:17
susshandlim26-Apr-05 1:17 
AnswerRe: Can you see the image in your mail? Pin
The_Mega_ZZTer27-Sep-05 8:05
memberThe_Mega_ZZTer27-Sep-05 8:05 
GeneralRe: Can you see the image in your mail? Pin
sanish197914-Sep-07 1:34
membersanish197914-Sep-07 1:34 
GeneralRe: Can you see the image in your mail? Pin
hustyang17-Jun-13 17:52
memberhustyang17-Jun-13 17:52 
GeneralBCC in Header Pin
stilton@cheese.com13-Apr-05 4:03
memberstilton@cheese.com13-Apr-05 4:03 
GeneralNice but Spam filters let the Emails not pass Pin
Star935-Apr-05 15:26
memberStar935-Apr-05 15:26 
GeneralRe: Nice but Spam filters let the Emails not pass Pin
Denis Kozlov3-Jan-06 11:37
memberDenis Kozlov3-Jan-06 11:37 
GeneralRe: Nice but Spam filters let the Emails not pass Pin
Star933-Jan-06 13:02
memberStar933-Jan-06 13:02 
GeneralError message handling Pin
Star9316-Feb-05 3:05
memberStar9316-Feb-05 3:05 
GeneralDisplayName Pin
Dekkie27-Jan-05 10:44
memberDekkie27-Jan-05 10:44 
GeneralRe: DisplayName Pin
Anonymous16-Feb-05 2:50
sussAnonymous16-Feb-05 2:50 
GeneralDisplayName 2 Pin
Anonymous16-Feb-05 2:55
sussAnonymous16-Feb-05 2:55 
GeneralRe: DisplayName 2 Pin
Anonymous27-Feb-05 2:30
sussAnonymous27-Feb-05 2:30 
GeneralRe: DisplayName 2 Pin
Star935-Apr-05 16:00
memberStar935-Apr-05 16:00 
QuestionNice component. But how to set time? Pin
tracerken18-Jan-05 22:55
membertracerken18-Jan-05 22:55 
AnswerRe: Nice component. But how to set time? Pin
Star9311-Apr-05 13:47
memberStar9311-Apr-05 13:47 
GeneralRe: Nice component. But how to set time? Pin
tracerken11-Apr-05 23:21
membertracerken11-Apr-05 23:21 
GeneralAttachments Pin
h_migneron25-Oct-04 8:31
memberh_migneron25-Oct-04 8:31 
GeneralRe: Attachments Pin
Sascha Sertel28-Oct-04 7:02
memberSascha Sertel28-Oct-04 7:02 
GeneralRe: Attachments Pin
h_migneron28-Oct-04 7:41
memberh_migneron28-Oct-04 7:41 
QuestionOnMailSent event in Visual Basic .Net ? Pin
Mark Sanchez12-Oct-04 21:45
sussMark Sanchez12-Oct-04 21:45 
AnswerRe: OnMailSent event in Visual Basic .Net ? Pin
mr_jimmybob14-Oct-04 13:36
membermr_jimmybob14-Oct-04 13:36 
GeneralInline Images not rendering correctly Pin
s3mt3x22-Aug-04 4:17
members3mt3x22-Aug-04 4:17 
GeneralRe: Inline Images not rendering correctly Pin
OhadAsor2-Feb-05 7:05
memberOhadAsor2-Feb-05 7:05 
GeneralRe: Inline Images not rendering correctly Pin
s3mt3x2-Feb-05 23:08
members3mt3x2-Feb-05 23:08 
GeneralOWA Forward Issues Pin
Joel S Martin4-Aug-04 8:46
sussJoel S Martin4-Aug-04 8:46 
GeneralSolution Pin
Joel S Martin4-Aug-04 9:28
sussJoel S Martin4-Aug-04 9:28 
GeneralMy code. Pin
Alexandre Kojevnikov11-Jul-04 4:00
memberAlexandre Kojevnikov11-Jul-04 4:00 
GeneralRe: My code. Pin
Anonymous20-Jul-04 13:47
sussAnonymous20-Jul-04 13:47 
GeneralRe: My code. Pin
FunkyMonkey19-Jun-06 13:48
memberFunkyMonkey19-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
Web04 | 2.8.150520.1 | Last Updated 1 Mar 2003
Article Copyright 2002 by Steaven Woyan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid