Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
29 Sep 2012 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here