Click here to Skip to main content
11,635,705 members (78,684 online)
Click here to Skip to main content

Sending Email with attachment in ASP.NET using SMTP Server

, 28 Jun 2005 314.4K 19.8K 78
Rate this:
Please Sign up or sign in to vote.
This article explains how to send a mail with attachment using SMTP server and how to configure the SMTP server.

Introduction

This application demonstrates how to send mail with attachments using SMTP server and how to configure the default SMTP server. The System.Web.Mail namespace provides classes for sending email in .NET. The classes involved are MailMessage which manages the content of the mail message and MailAttachment which manages the mail attachments.

Setting SMTP server

Before running the application you need to confirm these server settings:

  1. Make sure that the SMTP server is running, only then it can relay mail. If not, open the IIS window, in IIS look for the "local computer" on the left side tree view under which you will see the "Default SMTP Virtual Server", if it is not available, then you need to install it.
  2. To configure "Default SMTP Virtual Server", right-click on it, go into "Properties", and select "Access" tab, and then click the "Relay" button. With "only the list below" radio button selected, you should see the local IP address: "127.0.0.1", if it's not there, you need to add it.
  3. If you are using "localhost" or "127.0.0.1" as the SmtpMail.SmtpServer, make sure "Anonymous access is allowed". To allow access, open up the IIS. Locate the SMTP virtual server, and right-click select Properties. On the Access tab, click the Authentication button. Make sure "Anonymous Access" is the only checkbox checked.
  4. Use real from and to addresses that exist on the SmtpMail.SmtpServer. Do not use invalid address.

Getting objects

Initially, we need to create a new object for the MailMessage class, we will call it "mailMessage". Then we set the From, To, Cc, Bcc, Subject and Body properties as well as the BodyFormat of this object to the values on our web form:

 /* Create a new blank MailMessage */
 MailMessage mailMessage = new MailMessage ();  
 mailMessage.From = txtSender.Text;
 mailMessage.To = txtReceiver.Text;
 mailMessage.Cc = txtCc.Text;
 mailMessage.Bcc = txtBcc.Text;
 mailMessage.Subject = txtSubject.Text;
 mailMessage.Body = txtBody.Text;
 /* Set the properties of the MailMessage to the
    values on the form  */
 if (rblMailFormat.SelectedItem.Text == "Text")
    mailMessage.BodyFormat = MailFormat.Text;
 else
    mailMessage.BodyFormat = MailFormat.Html;

Making attachments

To make attachments we need to use the MailAttachment class and for this we create an object attach. The following block of code checks the Open File Dialogs of our web form (the Open File Dialog is an HTML file field control to which we've added the runat="server" property). If there is a value, the file is uploaded, saved on the server and added as an attachment to the email.

/* Beginning of Attachment1 process   & 
   Check the first open file dialog for a attachment */
if (inpAttachment1.PostedFile != null)
{
/* Get a reference to PostedFile object */
HttpPostedFile attFile = inpAttachment1.PostedFile;
 /* Get size of the file */
 int attachFileLength = attFile.ContentLength; 
 /* Make sure the size of the file is > 0  */
 if (attachFileLength > 0)
 {
 /* Get the file name */
 strFileName = Path.GetFileName(inpAttachment1.PostedFile.FileName);
 /* Save the file on the server */      
 inpAttachment1.PostedFile.SaveAs(Server.MapPath(strFileName));  
 /* Create the email attachment with the uploaded file */
 MailAttachment attach = new MailAttachment(Server.MapPath(strFileName));
 /* Attach the newly created email attachment */      
 mailMessage.Attachments.Add(attach);
 /* Store the attach filename so we can delete it later */
 attach1 = strFileName;
 }
}

Now, we send the email with the attachments:

SmtpMail.SmtpServer = "127.0.0.1";
(Or)
SmtpMail.SmtpServer.Insert(0,"127.0.0.1");
SmtpMail.Send (mailMessage);

Finally, if the attachments are available then we need to delete it:

/* Delete the attachements if any */
 if (attach1 != null)
  File.Delete(Server.MapPath(attach1));
 if (attach2 != null)
  File.Delete(Server.MapPath(attach2));
 if (attach3 != null)
       File.Delete(Server.MapPath(attach3));

Various ways to set the SMTP Mail server:

SmtpMail.SmtpServer = "localhost"

or

SmtpMail.SmtpServer = "127.0.0.1"

or

SmtpMail.SmtpServer.Insert( 0, 
    "127.0.0.1 or your mail server name here")

You need to replace "localhost" with the name or IP address of your SMTP Mail server on a Windows desktop computer or you can insert the server but "localhost" is the default value and it usually works.

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

Sivakumar Ramadoss
Web Developer
India India
Sivakumar Ramadoss (SIVA) is a Software Engineer for Adea International Pvt Ltd, Bangalore. His background is in developing relational databases and n-tier applications in Microsoft Technologies.


You may also be interested in...

Comments and Discussions

 
QuestionASP.Net Pin
Member 998846914-Apr-13 23:20
memberMember 998846914-Apr-13 23:20 
Questionerror Pin
Heba Kamel19-Oct-12 5:49
memberHeba Kamel19-Oct-12 5:49 
QuestionHow to send a E-Mail Pin
Uvaraj$8828-May-12 10:09
memberUvaraj$8828-May-12 10:09 
AnswerRe: How to send a E-Mail Pin
Amalraj Ramesh2-Oct-12 20:05
memberAmalraj Ramesh2-Oct-12 20:05 
AnswerRe: How to send a E-Mail Pin
_Amy8-Oct-12 0:58
member_Amy8-Oct-12 0:58 
GeneralMy vote of 5 Pin
member6010-Apr-12 22:21
membermember6010-Apr-12 22:21 
QuestionThanks Pin
amolkhedekar1-Apr-12 20:11
memberamolkhedekar1-Apr-12 20:11 
Generalthanks Pin
MehmoodMemon14-Feb-12 2:25
memberMehmoodMemon14-Feb-12 2:25 
Questionhow to make a perminate receiving email? Pin
pellegm9-Feb-12 2:12
memberpellegm9-Feb-12 2:12 
GeneralMy vote of 4 Pin
Member 822688117-Jan-12 2:36
memberMember 822688117-Jan-12 2:36 
GeneralMy vote of 1 Pin
Shama Shahzadi8-Jul-11 0:12
memberShama Shahzadi8-Jul-11 0:12 
GeneralNot Working Pin
anas khan ahmed14-Oct-10 23:56
memberanas khan ahmed14-Oct-10 23:56 
Generalwhen run the apllication i found this error Pin
jessystar804-Aug-10 0:28
memberjessystar804-Aug-10 0:28 
Generalwindows authentication mode error Pin
abeelzymal31-Aug-09 5:29
memberabeelzymal31-Aug-09 5:29 
GeneralSMTP Virtual Server settings on windows 2008 server Pin
markah_199913-Jul-09 5:06
membermarkah_199913-Jul-09 5:06 
Generali get the error on the first line Inherits="EMailSample.SendMail" Pin
Member 37853799-May-09 23:01
memberMember 37853799-May-09 23:01 
GeneralI'm not understand why i get this configuration error... Pin
Steammike4-Feb-09 21:10
memberSteammike4-Feb-09 21:10 
GeneralRelay Problem Pin
Syed Adeel Zaidi23-Oct-08 10:31
memberSyed Adeel Zaidi23-Oct-08 10:31 
Generalerror... Pin
BruceInFTL15-Jan-08 6:41
memberBruceInFTL15-Jan-08 6:41 
QuestionSending attachment in Winforms app Pin
jlo1438-Mar-07 18:09
memberjlo1438-Mar-07 18:09 
GeneralHelp for understand - Urgent Pin
Yamiyahoo3-Dec-06 22:17
memberYamiyahoo3-Dec-06 22:17 
Generalerror help- The server respond 5.7.1 unable to relay... Pin
Yamiyahoo3-Dec-06 22:12
memberYamiyahoo3-Dec-06 22:12 
GeneralMail Server Pin
anisha1965-Nov-06 8:05
memberanisha1965-Nov-06 8:05 
GeneralRe: Mail Server Pin
manish srivastava11-Jan-07 9:20
membermanish srivastava11-Jan-07 9:20 
GeneralHelp me Pin
princeamrooo7-Aug-06 12:34
memberprinceamrooo7-Aug-06 12:34 
GeneralThanx Pin
yourasp.net4-Jul-06 3:49
memberyourasp.net4-Jul-06 3:49 
GeneralInvalid Mail Attachment ' '. Pin
hexOffender22-May-06 5:20
memberhexOffender22-May-06 5:20 
GeneralRe: Invalid Mail Attachment ' '. Pin
Sivakumar_Ramadoss3-Jun-06 3:03
memberSivakumar_Ramadoss3-Jun-06 3:03 
QuestionHow to retrieve an Attachment file from mail server. Pin
Hendrik (Chrysonite)2-May-06 23:47
memberHendrik (Chrysonite)2-May-06 23:47 
GeneralSMTP server running on remote machine Pin
f_baig786@hotmail.com9-Feb-06 19:03
memberf_baig786@hotmail.com9-Feb-06 19:03 
GeneralDelete files error Pin
altug200522-Dec-05 7:49
memberaltug200522-Dec-05 7:49 
GeneralRe: Delete files error Pin
thaiasp21-Mar-06 0:38
memberthaiasp21-Mar-06 0:38 
GeneralRe: Delete files error Pin
musicdsign19-Oct-06 20:39
membermusicdsign19-Oct-06 20:39 
QuestionRe: Delete files error Pin
Vikram kshatriya13-Apr-08 17:47
memberVikram kshatriya13-Apr-08 17:47 
AnswerRe: Delete files error Pin
pradeep8423-May-08 0:56
memberpradeep8423-May-08 0:56 
AnswerRe: Delete files error [modified] Pin
grossbommi4-Jun-08 1:06
membergrossbommi4-Jun-08 1:06 
Questionhow to check SMTP server is alive using code Pin
tiwari piyush24-Nov-05 2:22
membertiwari piyush24-Nov-05 2:22 
Generalnot delivering to address Pin
sam_ten200127-Oct-05 22:52
membersam_ten200127-Oct-05 22:52 
GeneralRe: not delivering to address Pin
cdotpal9-Mar-06 2:38
membercdotpal9-Mar-06 2:38 
GeneralRe: not delivering to address Pin
Hemant K Jangid24-Apr-06 3:56
memberHemant K Jangid24-Apr-06 3:56 
GeneralRe: not delivering to address Pin
yourasp.net4-Jul-06 3:52
memberyourasp.net4-Jul-06 3:52 
GeneralThanks for the code Pin
Rinda Kotagiri11-Jul-05 23:13
sussRinda Kotagiri11-Jul-05 23:13 
GeneralSmtpServer.Insert -- mistake Pin
Jakub Snopek11-Jul-05 5:46
sussJakub Snopek11-Jul-05 5:46 
GeneralRe: SmtpServer.Insert -- mistake Pin
Sivakumar Ramadoss7-Sep-05 1:47
memberSivakumar Ramadoss7-Sep-05 1:47 
GeneralRe: SmtpServer.Insert -- mistake Pin
mstehle23-Nov-05 9:20
membermstehle23-Nov-05 9:20 
GeneralRe: SmtpServer.Insert -- mistake Pin
ericpaulin28-Nov-05 3:01
memberericpaulin28-Nov-05 3:01 
QuestionWhat if your SMTP server requires you to login? Pin
cbono200030-Jun-05 7:56
membercbono200030-Jun-05 7:56 
AnswerRe: What if your SMTP server requires you to login? Pin
Catalin Radoi6-Feb-06 0:01
memberCatalin Radoi6-Feb-06 0:01 

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
Web01 | 2.8.150728.1 | Last Updated 28 Jun 2005
Article Copyright 2005 by Sivakumar Ramadoss
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid