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:
- 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.
- 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.
- 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.
- Use real from and to addresses that exist on the
SmtpMail.SmtpServer. Do not use invalid address.
Initially, we need to create a new object for the
MailMessage class, we will call it "
mailMessage". Then we set the
Body properties as well as the
BodyFormat of this object to the values on our web form:
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;
if (rblMailFormat.SelectedItem.Text == "Text")
mailMessage.BodyFormat = MailFormat.Text;
mailMessage.BodyFormat = MailFormat.Html;
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.
if (inpAttachment1.PostedFile != null)
HttpPostedFile attFile = inpAttachment1.PostedFile;
int attachFileLength = attFile.ContentLength;
if (attachFileLength > 0)
strFileName = Path.GetFileName(inpAttachment1.PostedFile.FileName);
MailAttachment attach = new MailAttachment(Server.MapPath(strFileName));
attach1 = strFileName;
Now, we send the email with the attachments:
SmtpMail.SmtpServer = "127.0.0.1";
Finally, if the attachments are available then we need to delete it:
if (attach1 != null)
if (attach2 != null)
if (attach3 != null)
Various ways to set the SMTP Mail server:
SmtpMail.SmtpServer = "localhost"
SmtpMail.SmtpServer = "127.0.0.1"
"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.
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.