|
Sending Email with ASP.NET
One of the common functionalities used in Web development is sending email from
a Web page. A common use of sending email from a Web page is allowing site visitors
to fill in comments via an HTML form and send them to the Webmaster. The
.NET Framework makes the task of sending email from a Web page relatively simple.
In order to send an email from an ASP.NET Web page you need to use the SmtpMail class
found in the System.Web.Mail namespace, which contains a
static method Send.
Sending Email
The namespace that needs to be imported to send an email is the System.Web.Mail namespace.
We use the SmtpMail and MailMessage classes
of this namespace for this purpose. The MailMessage class provides properties and
methods for constructing an email message. To start, open a Web Forms page, drag a
Button control on to the form, switch to code view and paste the following code.
Imports System.Web.Mail
'namespace to be imported
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click
Dim mailMessage As New MailMessage()
'creating an instance of the MailMessage class
mailMessage.From = "xyz@mydomain.com"
'senders email address
mailMessage.To = "abc@sendersemail.com"
'recipient's email address
mailMessage.Cc = "carboncopy@sendersemail.com"
'email address of the Cc recipient
mailMessage.Bcc = "blindcarboncopy@sendersemail.com"
'email address of the Bcc recipient
mailMessage.Subject = "Hello"
'subject of the email message
mailMessage.BodyFormat = MailFormat.Text
'message text format. Can be text or html
mailMessage.Body = "This tutorial is sending email with an ASP.NET app."
'message body
mailMessage.Priority = MailPriority.Normal
'email priority. Can be low, normal or high
SmtpMail.SmtpServer = "mail.yourserver.com"
'mail server used to send this email. modify this line based on your mail server
SmtpMail.Send(mailMessage)
'using the static method "Send" of the SmtpMail class to send the mail
Response.Write("Mail sent")
'message stating the mail is sent
End Sub
|
The above code sends an email when the button is clicked. That's fine for the
purpose of learning but when you have a feedback form on your Web site with textboxes,
labels, etc, you need to slightly modify the above code. The following is the
complete, functional code for an ASP.NET page that sends an email to the Webmaster
of the site.
To start, drag seven labels, six textboxes and two buttons on to the Web forms page
designer. The user interface for this sample can be found at the bottom of this
page. The modified code looks as follows:
Imports System.Web.Mail
Private Sub SendMail_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles SendMail.Click
Dim mailMessage As New MailMessage()
mailMessage.From = TextBox1.Text
mailMessage.To = "admin@startvbdotnet.com"
'you also can set this to TextBox2.Text
mailMessage.Cc = TextBox3.Text
mailMessage.Bcc = TextBox4.Text
mailMessage.Subject = TextBox5.Text
mailMessage.BodyFormat = MailFormat.Text
mailMessage.Body = TextBox6.Text
'textbox6 TextMode property is set to MultiLine
mailMessage.Priority = MailPriority.Normal
SmtpMail.SmtpServer = "mail.yourserver.com"
'mail server used to send this email. modify this line based on your mail server
SmtpMail.Send(mailMessage)
Label6.Text = "Your mail was sent"
'message stating the mail is sent
End Sub
Private Sub Reset_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Reset.Click
TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
TextBox4.Text = " "
TextBox5.Text = " "
TextBox6.Text = " "
'resetting all the value to default i.e null
End Sub
|
Next>>Sending an HTML email with attachment
Live Code Demo
Sending Email
with ASP.NET
Label6
Do not enter any
values and click the button. The TextBoxes and Button below are provided to give you
an idea about the user interface for the sample mentioned above.
From
To
Cc
Bcc
Subject
Message
|