Click here to Skip to main content
15,885,890 members
Articles / Web Development / ASP.NET
Tip/Trick

[Mail Sent]How to Send an Email in ASP.NET Application

Rate me:
Please Sign up or sign in to vote.
2.19/5 (6 votes)
7 Feb 2014CPOL3 min read 43.4K   14  
In this tip, you will learn how to send an email in your ASP.NET application.

Introduction

In this tip, you will learn how to send an email in your ASP.NET application. This tip uses the Gmail account to send an email so you can send email on the fly. Here, you will get the sample code to send an email in ASP.NET application.

Background

When I started my career in ASP.NET, I was totally clueless how to send an email in ASP.NET application. I Googled it and found too many answers and some of them didn't even work for me at all. After trying a couple of solutions, finally I was able to send my first email through an ASP.NET web application.

Using the Code

The ASP.NET namespace System.Net.Mail is handy when it comes to sending an email in ASP.NET application. So the first thing is to include this namespace in your code behind file. We will use the appropriate classes of this namespace for sending mail.

VB.NET
47.Imports System.Net.Mail 

We will use the following classes in our sample code:

  1. MailMessage: This class is used to add the content in a mail. The content could be the email body, subject, recipient address and sender address.
  2. SmtpClient: This class is used to send the message through SMTP server.
  3. Attachment: Using this class, we can attach a file to the email.

Now I will come to the real thing. How to write code for sending a sample mail in an ASP.NET application. Here is the step by step procedure to send an email in ASP.NET.

The Real Thing

Step 1: Open Visual Studio from your desktop.

Step 2: Create a new website and name it as "SendMail".

Step 3: Create a new page and name it SendMail.aspx.

Step 4: Add a text box on the page and also add a button and label it as "Send Mail".

Step 5: In code behind of this page (on button's click event), copy paste the following code:

VB.NET
47.Protected Sub SendMail_Click(sender As Object, e As System.EventArgs) Handles btnSendMail.Click
48.        Dim message As MailMessage = New MailMessage()
49.        message.To.Add(New MailAddress("test@test.com"))
50.        message.Subject = "This is a test message"
51.        message.Body = "This is a test mail"
52.        Dim mclient As SmtpClient = New SmtpClient()
53.        mclient.Host = "smtp.gmail.com"
54.        mclient.Port = 587 
55.        mclient.EnableSsl = True
56.        message.Attachments.Add(New Attachment(Server.MapPath("~/FileName.doc")))
57.        message.CC.Add("mail1@mailaddress.com")
58.        message.Bcc.Add("mail1@mailaddress.com") 
59.        mclient.Credentials = New System.Net.NetworkCredential("Gmail user id", "Gmail password")
60.        mclient.Send(message)
61.End Sub

Explanation

  1. We have created a message object using the class MailMessage. This object is used to add the actual message body in the mail.
  2. With the help of this message object, we have added the To Address and Subject to mail message.
  3. If you want to add message body in the form of HTML, then use the following lines of code:
    VB.NET
    63.message.IsBodyHtml = "True"
    64.message.Body = "<p>" + "This is a test mail" + "</p>"
  4. We have created an mClient object by using the class SmtpClient class. This object is used to send the message to the recipient. We have set the different properties like Host, Port, EnableSSL of this object of Gmail server.
  5. If you want to send email with attachment, write the following line of code:
    VB.NET
    58.message.Attachments.Add(New Attachment(Server.MapPath("~/FileName.doc"))) 
  6. You can also send CC and Bcc copy using the following lines of code:
    VB.NET
    59.message.CC.Add("mail1@mailaddress.com")
    60.message.Bcc.Add("mail1@mailaddress.com") 
  7. Finally, we have provided the Gmail account credentials by using the following line of code. The first parameter is GMail account User ID and the second parameter is Password.
    VB.NET
    61.mclient.Credentials = New System.Net.NetworkCredential("Gmail user id", "Gmail password")

Points of Interest

This is perhaps the easiest and simplest method for newbies to send an email in ASP.NET application. You can also use your personal GMail account to send email in an ASP.NET application.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
Subodh Raghav, a Software Architect and certified web enthusiast, acquired his formal education and training at Aligarh Muslim University, India. He holds a Masters Degree in Computer Application and as of the present time; takes pride in working in the I.T. industry.

Subodh’s first article had been featured and published in EzineArticles.com and ArticlesBase.com among many others. As a programming enthusiast and a self-confessed “programming addict”, he has created various software applications to maximize the lifestyles of the modern internet users and other people who share his passion for computer technology. With over 1K+ original posts in his blog, he continues to impart his expertise to others by way of posting new and valuable information that can make a major difference in people’s lives.

Subodh’s hobbies and personal interests include writing informative blogs and surfing the internet; and he also takes inspiration from music and physical sports such as Cricket. Currently, he works for a IT Company in Noida, India; and is the author of his own blog.

Comments and Discussions

 
-- There are no messages in this forum --