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
classmailMessage.From = \\\"saurabh@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 mailResponse.Write(\\\"Mail sent\\\") \\\'message stating the mail is sentEnd 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:
VB.Net :
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@saurabh.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
---------------------------------------------------------------------------------
C#.net :
MailMessage objMail = new MailMessage();
objMail.From = new MailAddress(\\\"from address\\\");
objMail.To.Add(new MailAddress(\\\"to address\\\"));
objMail.Subject = \\\"subject\\\";
objMail.Body = \\\"message data\\\";
objMail.IsBodyHtml = true;
SmtpPermission x = new SmtpPermission(SmtpAccess.ConnectToUnrestrictedPort);
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = false;
smtp.Host = \\\"smtp address\\\";
smtp.Port = 25;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(\\\"username\\\", \\\"password\\\");
smtp.Send(objMail);