Click here to Skip to main content
6,305,776 members and growing! (17,733 online)
Email Password   helpLost your password?
General Programming » Internet / Network » Email     Intermediate

A class for sending emails with attachments in C#.

By Steaven Woyan

A class for sending emails with attachments in C#.
C#, Windows, .NET 1.0, Dev
Posted:10 Jun 2002
Updated:28 Feb 2003
Views:208,108
Bookmarked:106 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
66 votes for this article.
Popularity: 7.79 Rating: 4.28 out of 5
10 votes, 17.9%
1

2
1 vote, 1.8%
3
4 votes, 7.1%
4
41 votes, 73.2%
5

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

About the Author

Steaven Woyan


Member

Occupation: Web Developer
Location: United States United States

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 137 (Total in Forum: 137) (Refresh)FirstPrevNext
Questiongreat work but what about SSL? PinmemberAdore C++7:32 13 Jun '09  
Generalproblems in sending emails Pinmemberammartahan4:29 16 Jul '06  
Generalembedding images (inline images) Pinmemberrocder3:40 10 Jul '06  
GeneralThread-safe MessageBox in OnMailSent event handler PinmemberMichael Freidgeim18:12 3 Jul '06  
GeneralStatics in SmtpAttachment class PinmemberTiTi____1:43 20 Apr '06  
GeneralRe: Statics in SmtpAttachment class PinmemberTiTi____1:27 21 Apr '06  
GeneralHelp ..im about to be crazy.. Pinmembercoolshad16:37 15 Mar '06  
Generalgreat! PinmemberMP3Observer13:09 14 Mar '06  
GeneralError with attachments. Pinmembery2k4life19:17 4 Feb '06  
General"Sent:" date wrong in Outlook Pinmembermichaelloveusa9:26 31 Jan '06  
Generalabout the encoding of attachments PinmemberBouarf0:20 4 Jan '06  
Generalhow to send images with in the body of mail Pinmembersreesiri2:13 8 Dec '05  
Generaldateformat not rfc PinmemberKylixs14:48 10 Oct '05  
GeneralRe: dateformat not rfc PinmemberStar939:17 6 Jan '06  
GeneralSmtpClient in .NET 2.0 PinmemberAlexander Kojevnikov23:25 14 Sep '05  
GeneralMail subject whithout correct iso-8859-1 ponctuation Pinmemberandre.lourenco@link.pt1:37 12 Sep '05  
AnswerRe: Mail subject whithout correct iso-8859-1 ponctuation Pinmembergabrielbur5:17 14 Nov '07  
GeneralLittle bug with big effect PinmemberStar930:04 7 Jul '05  
GeneralCan you see the image in your mail? Pinsusshandlim1:17 26 Apr '05  
GeneralRe: Can you see the image in your mail? PinmemberThe_Mega_ZZTer8:05 27 Sep '05  
GeneralRe: Can you see the image in your mail? Pinmembersanish19791:34 14 Sep '07  
GeneralBCC in Header Pinmemberstilton@cheese.com4:03 13 Apr '05  
GeneralNice but Spam filters let the Emails not pass PinmemberStar9315:26 5 Apr '05  
GeneralRe: Nice but Spam filters let the Emails not pass PinmemberDenis Kozlov11:37 3 Jan '06  
GeneralRe: Nice but Spam filters let the Emails not pass PinmemberStar9313:02 3 Jan '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Feb 2003
Editor: Nick Parker
Copyright 2002 by Steaven Woyan
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project