Click here to Skip to main content
Click here to Skip to main content

How to Send An E-Mail from your ASP.NET Web Application...?

, 29 Sep 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Sending an email from your ASP.NET web application

Introduction

In this tip, I will show you how to send mail from your ASP.NET web application.

Sending Email From Your Web Application

  1. For sending mail, you will need to use these libraries:
    using System.Net.Mail;
    using System.Net; 
  2. Use this code to send mail:
    System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
    	mm.To.Add(new System.Net.Mail.MailAddress("Email Address","Name"));
    	mm.From = new System.Net.Mail.MailAddress("Email Address");
    	mm.Sender = new System.Net.Mail.MailAddress("Email Address","Name");
    	mm.Subject = "This is Test Email";
    	mm.Body = "<h3>This is Testing SMTP Mail Send By Me</h3>";
    	mm.IsBodyHtml = true;
    	mm.Priority = System.Net.Mail.MailPriority.High; // Set Priority to sending mail
    	System.Net.Mail.SmtpClient smtCliend = new System.Net.Mail.SmtpClient();
    	smtCliend.Host = "Your smtp server";
    	smtCliend.Port = 25;    // SMTP port no            
    	smtCliend.Credentials = new NetworkCredential("User Name", "Password");
    	smtCliend.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    	try
    	{
    		smtCliend.Send(mm);
    	}
    	catch (System.Net.Mail.SmtpException ex)
    	{
    		lblMsg.Text = ex.ToString();
    	}
    	catch (Exception exe)
    	{
    		lblMsg.Text = "\n\n\n"+exe.ToString();
    	}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Tejas Vaishnav
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
GeneralWorks for me! PinmemberMotilm31-Oct-14 16:16 
Suggestionyou can have better version at PinmemberMukund Thakker9-Oct-12 4:07 
SuggestionSuggestion For You Sir PinmemberStrange_Pirate28-Sep-12 2:54 
GeneralReason for my vote of 1 An old tip. Pinmembermhamad zarif3-Sep-11 3:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 29 Sep 2012
Article Copyright 2011 by Tejas Vaishnav
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid