Welcome, Guest [LogIn] / [Register] / [Forgot Password ?]
Home > Technical Tips > ASP.Net > Sending Email with ASP.NET ?
Sending Email with ASP.NET ?
Posted by Saurabh Kumar on Thursday, Nov 19, 2009
| More

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);

Mirela  says :
January 28, 2010 at 02:48 am
esto es una prueba para ver si recivo el mensaja en varias lineas
Mirela  says :
January 28, 2010 at 02:44 am
esto es una prueba para ver si manda el texto en varias lineas
Sushil  says :
November 20, 2009 at 12:34 pm
thanks for the coding you save my life. Can you help me more ? I have created a website through asp.net for me but i do not know what are the basic steps to follow like " what is copyright" and how to do it plz plz tell me what are the essential steps to have in a website. Thanks thanks thanks for advance
Admin  says :
November 19, 2009 at 11:33 pm
Hi Sushil Please check it details about sending mail through C#.net
Sushil  says :
November 17, 2009 at 04:37 pm
hi there . Thanks for the coding of mailmsg through asp.net web page but can you covert it into c# coding i guess it is as vb.net plz convert it into c# thanks again you are really helping me .
Add Your Comments/Suggestions :
Name:
Email:
Website:
Your Comments:
 
(required)
(will not be published) (required)


©Copyright 2010 saurabhdeveloper.com