Click here to Skip to main content
15,885,278 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.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

 
GeneralRe: Is this class usable in a production environment? Pin
Anonymous25-Mar-04 1:30
Anonymous25-Mar-04 1:30 
GeneralRe: Is this class usable in a production environment? Pin
AppMaker889-Apr-04 3:52
AppMaker889-Apr-04 3:52 
GeneralRe: Is this class usable in a production environment? Pin
Nnabu2-Oct-04 7:18
Nnabu2-Oct-04 7:18 
GeneralRe: Is this class usable in a production environment? Pin
Gfw9-Apr-04 9:00
Gfw9-Apr-04 9:00 
GeneralFor Steaven Woyan! IMPORTANT! Pin
Alexander Kojevnikov13-Feb-04 2:24
Alexander Kojevnikov13-Feb-04 2:24 
GeneralRe: For Steaven Woyan! IMPORTANT! Pin
Steaven Woyan13-Feb-04 6:28
Steaven Woyan13-Feb-04 6:28 
GeneralRe: For Steaven Woyan! IMPORTANT! Pin
DaberElay22-May-04 20:52
DaberElay22-May-04 20:52 
GeneralRated as spam Pin
the-unforgiven15-Jan-04 11:51
the-unforgiven15-Jan-04 11:51 
Hi,
when I send test-mails with attachment (a simple aspi.log file), it gets rated as spam:

X-Mailer: smw.smtp.SmtpEmailer
DATE: Donnerstag, 15. Januar 2004
From: from@memyselfandi.de
To: to@memyselfandi.de
Reply-To: from@memyselfandi.de
SUBJECT: [SPAM] subject smtp-test
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="#SEPERATOR1#"
X-Spam-Flag: YES
X-Spam-Checker-Version: SpamAssassin 2.61 (1.212.2.1-2003-12-09-exp) on
p15140275.pureserver.info
X-Spam-Level: *****
X-Spam-Status: Yes, hits=5.7 required=4.9 tests=MIME_BASE64_ILLEGAL,
NO_REAL_NAME,RATWARE_HASH_2,RATWARE_HASH_2_V2 autolearn=no
version=2.61
X-Spam-Report: * 2.4 RATWARE_HASH_2_V2 Bulk email fingerprint (hash 2 v2)
found * 0.3 NO_REAL_NAME From: does not include a real name *
1.2 RATWARE_HASH_2 Bulk email fingerprint (hash 2)
found * 1.7 MIME_BASE64_ILLEGAL RAW: base64 attachment
uses illegal characters

rgds
the-unforgiven
GeneralRe: Rated as spam Pin
Volx28-Feb-04 10:16
Volx28-Feb-04 10:16 
GeneralEven more fixes! Pin
Alexander Kojevnikov23-Dec-03 6:15
Alexander Kojevnikov23-Dec-03 6:15 
GeneralRe: Even more fixes! Pin
gxbacher21-Jan-04 3:35
gxbacher21-Jan-04 3:35 
GeneralRe: Even more fixes! Pin
gxbacher21-Jan-04 4:15
gxbacher21-Jan-04 4:15 
GeneralRe: Even more fixes! Pin
Alexander Kojevnikov21-Jan-04 8:16
Alexander Kojevnikov21-Jan-04 8:16 
GeneralRe: Even more fixes! Pin
gxbacher30-Jan-04 3:40
gxbacher30-Jan-04 3:40 
GeneralRe: Even more fixes! Pin
Alexander Kojevnikov13-Feb-04 2:13
Alexander Kojevnikov13-Feb-04 2:13 
GeneralInline images - more fixes. Pin
Alexander Kojevnikov3-Dec-03 7:40
Alexander Kojevnikov3-Dec-03 7:40 
GeneralIt work only with no auth. Pin
ndiaz25-Nov-03 10:28
ndiaz25-Nov-03 10:28 
GeneralCannot send email to yahoo.com Pin
fdsadasffsa26-Oct-03 17:00
fdsadasffsa26-Oct-03 17:00 
Generaldoc file as attachment Pin
rsilswal12-Oct-03 22:38
rsilswal12-Oct-03 22:38 
GeneralProblems downloading source Pin
Wayne Gibson22-Sep-03 0:58
Wayne Gibson22-Sep-03 0:58 
GeneralInline images. Pin
Alexander Kojevnikov24-Jul-03 4:50
Alexander Kojevnikov24-Jul-03 4:50 
GeneralRe: Inline images. Pin
Alexander Kojevnikov25-Jul-03 6:34
Alexander Kojevnikov25-Jul-03 6:34 
GeneralErrors sending attachments... Pin
sameersavla22-Jul-03 10:40
sameersavla22-Jul-03 10:40 
GeneralRe: Errors sending attachments... Pin
sameersavla5-Aug-03 11:18
sameersavla5-Aug-03 11:18 
GeneralRe: Errors sending attachments... Pin
zhoulhh13-Aug-03 5:07
zhoulhh13-Aug-03 5:07 

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.