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.
47.Imports System.Net.Mail
We will use the following classes in our sample code:
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.SmtpClient
: This class is used to send the message through SMTP server.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.
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
- We have created a message object using the class
MailMessage
. This object is used to add the actual message body in the mail. - With the help of this message object, we have added the To Address and Subject to mail message.
- If you want to add message body in the form of HTML, then use the following lines of code.
63.message.IsBodyHtml = "True"
64.message.Body = "<p>" + "This is a test mail" + "</p>"
- 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. - If you want to send email with attachment, write the following line of code.
58.message.Attachments.Add(New Attachment(Server.MapPath("~/FileName.doc")))
- You can also send CC and Bcc copy using the following lines of code:
59.message.CC.Add("mail1@mailaddress.com")
60.message.Bcc.Add("mail1@mailaddress.com")
- 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.
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 ASP.NET application.
The original version of this article can be found at http://www.givemethecode.blogspot.in/2013/12/ive-done-ithow-to-send-email-in-aspnet.html.