.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Email >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Code for sending email

Posted By:Shashi Ray       Posted Date: December 19, 2008    Points: 10    Category: Email    URL: http://www.dotnetspark.com  
 

Code for sending email:

Let's see the code to send an email. This code is executed on the button click control. First of all don't forget to add the

using System.Web.Mail;

namespace which provides the methods and properties for sending an email.

private void Button1_Click(object sender, System.EventArgs e)

{

MailMessage mail = new MailMessage();

mail.To = txtTo.Text;                              //put the TO ADDRESS here.

mail.From = txtFrom.Text;                      //put the FROM ADDRESS here.

mail.Subject = txtSubject.Text;               //put the SUBJECT here.

mail.Body = txtBody.Text;                     //put the BODY here.

SmtpMail.SmtpServer = "localhost";       //put SMTP SERVER you will use here.

SmtpMail.Send(mail);

}

Describe the above Code:

The code you see above is very simple to understand. In the button click event we made the object/instance of the MailMessage class. MailMessage is responsible for sending emails. It also provides several properties and methods. later we assigned several properties.

The line SmtpMail.SmtpServer = "localhost" sets the server for the mail. If you are running the application on your own pc using IIS than your server will be "localhost". If your website is running on the production server you can have different SmtpServer name.

The final line SmtpMail.Send(mail) sends the email to the email address provided.

 

Shashi Ray



     

Further Readings:

Responses
Author: aamir         Company URL: http://www.dotnetspark.com
Posted Date: December 27, 2008

that's helpful

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend