Click here to Skip to main content
15,890,690 members
Articles / Web Development / ASP.NET
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
3.67/5 (3 votes)
29 Sep 2012CPOL 39K   9   5
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:
    C#
    using System.Net.Mail;
    using System.Net; 
  2. Use this code to send mail:
    C#
    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)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWorks Pin
Member 1249582529-Aug-17 16:46
Member 1249582529-Aug-17 16:46 
GeneralWorks for me! Pin
Motilm31-Oct-14 15:16
Motilm31-Oct-14 15:16 
Suggestionyou can have better version at Pin
Mukund Thakker9-Oct-12 3:07
professionalMukund Thakker9-Oct-12 3:07 
SuggestionSuggestion For You Sir Pin
Strange_Pirate28-Sep-12 1:54
Strange_Pirate28-Sep-12 1:54 
Hi Want to write so many things over this article.
Main thing is that,You posted this article in VB or C# .? please let us know.

The Namespace you written is syntax of VB and the rest of the code is in C#.
So will you please make it correct.
Here i am posting code in both of VB and C# please take a look

C#:

C#
using System.Net.Mail;
using System.Net;

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


VB:

VB
Import System.Net.Mail;
Import System.Net;

Dim mm As 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
Dim smtCliend As 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 ex As System.Net.Mail.SmtpException
	lblMsg.Text = ex.ToString()
Catch exe As Exception
	lblMsg.Text = vbLf & vbLf & vbLf + exe.ToString()
End Try





Take a look SirThumbs Up | :thumbsup: Thumbs Up | :thumbsup: Thumbs Up | :thumbsup: Thumbs Up | :thumbsup: Thumbs Up | :thumbsup: Thumbs Up | :thumbsup: Thumbs Up | :thumbsup: Thumbs Up | :thumbsup: Thumbs Up | :thumbsup: Thumbs Up | :thumbsup: Blush | :O
Strange_Pirate

GeneralReason for my vote of 1 An old tip. Pin
mhamad zarif3-Sep-11 2:55
mhamad zarif3-Sep-11 2:55 

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

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