Click here to Skip to main content
15,881,882 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 489.3K   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

 
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 
That's an easy one to answer. I just discovered that in order to use the System.Web.Mail class you need to have Collaboration Data Objects installed (CDO), which basically means you need to have Outlook installed! I can't believe Microsoft tied .NET to Office in this way. If anyone knows different, please let me know.
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 
GeneralRe: SMTP Server Login Pin
Steaven Woyan23-Feb-03 15:48
Steaven Woyan23-Feb-03 15:48 
GeneralRe: SMTP Server Login Pin
zoomba23-Feb-03 15:52
zoomba23-Feb-03 15:52 
GeneralADD CC and BCC Pin
FIMBEL13-Jan-03 22:12
FIMBEL13-Jan-03 22:12 
GeneralRe: ADD CC and BCC Pin
Steaven Woyan23-Feb-03 15:49
Steaven Woyan23-Feb-03 15:49 
GeneralPeriod issue Pin
drogoin19-Nov-02 3:16
drogoin19-Nov-02 3:16 
GeneralRe: Period issue Pin
Paul Ingles19-Nov-02 6:28
Paul Ingles19-Nov-02 6:28 
GeneralExcellent example Pin
Robert Saye11-Oct-02 4:50
Robert Saye11-Oct-02 4:50 
QuestionUsing Default Mail Program? Pin
Swinefeaster26-Sep-02 13:32
Swinefeaster26-Sep-02 13:32 
GeneralError while sending mail in loop Pin
viren30-Aug-02 21:05
viren30-Aug-02 21:05 
GeneralRe: Error while sending mail in loop Pin
Steaven Woyan5-Sep-02 3:56
Steaven Woyan5-Sep-02 3:56 
GeneralRe: Error while sending mail in loop Pin
viren gaitonde7-Oct-02 22:57
sussviren gaitonde7-Oct-02 22:57 
Generalsend email Pin
Shotgun11-Jul-02 11:15
Shotgun11-Jul-02 11:15 
GeneralRe: send email Pin
Steaven Woyan11-Jul-02 16:29
Steaven Woyan11-Jul-02 16:29 
GeneralWhy HTML attachment is not shown in the OutLook Express 5.5 Pin
Blandger2-Jul-02 3:32
Blandger2-Jul-02 3:32 
GeneralRe: Why HTML attachment is not shown in the OutLook Express 5.5 Pin
Steaven Woyan2-Jul-02 5:55
Steaven Woyan2-Jul-02 5:55 

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.