In this tutorial you should create a form to allow users to enter e-mail information. When the user submits the form to the server, your application will build a MailMessage object from the email information and then send the email using the SmtpClient class. The namespace System.Net.Mail contains classes which take care of constructing an SMTP-based message. The System.Net.Mail.MailMessages class encapsulated constructing a SMPT-based message, and System.Net.Mail.SmtpClient class provides the mechanism for sending the message to an SMTP server.

In the .aspx file:

  • Create a simple form to capture the sender’s email address, the recipient’s email address, the subject, and the message body.
  • Add a Send button that initiates the sending of email.

In the code-behind class for the page:

  • Create a MailMessage object which will work as a container for the mail message.
  • Set the To, From, Subject and Body properties to the data entered on the form.
  • Create a SmtpClient object (specifying the SMPTP server) which will send a message. This object will expect in most cases:
    • To specify server and port for outgoing e-mail messages. For example you can use smtp.gmail.com and port 587 when you create the object.
    • To enable ssl if the smtp server uses it –
    • To setup credentials
  • Use try/catch block to send a message.

Use Visual Studio to create a new ASP.NET project with name for example SendMail.

The page SendMail.aspx looks like the next picture

How to send email in ASP.NET

How to send email in ASP.NET

and its markup is the following

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”SendMail.aspx.cs” Inherits=”SendMail.SendMail” %>

 

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”https://www.w3.org/1999/xhtml”>

<head runat=”server”>

<title></title>

</head>

<body>

<form id=”form1″ runat=”server”>

<div>

<table style=”width: 56%;” align=”center”>

<tr>

<td align=”center” colspan=”2″ width=”90%”>

&nbsp;

&nbsp;

&nbsp;

<strong>How to ASP.NET Sending Email</strong></td>

</tr>

<tr>

<td align=”right”>

&nbsp;From:</td>

<td align=”left”>

<asp:TextBox ID=”txtFrom” runat=”server”></asp:TextBox>

</td>

</tr>

<tr>

<td align=”right”>

&nbsp;To:</td>

<td align=”left”>

<asp:TextBox ID=”txtTo” runat=”server”></asp:TextBox>

</td>

</tr>

<tr>

<td align=”right”>

Subject</td>

<td align=”left”>

<asp:TextBox ID=”txtSubject” runat=”server”></asp:TextBox>

</td>

</tr>

<tr>

<td align=”right”>

Message</td>

<td align=”left”>

<asp:TextBox ID=”txtMsg” runat=”server” TextMode=”MultiLine”></asp:TextBox>

</td>

</tr>

<tr>

<td align=”center” colspan=”2″>

<asp:Button ID=”btnSend” runat=”server” onclick=”btnSend_Click” Text=”Send” />

</td>

</tr>

</table>

</div>

</form>

</body>

</html>

The code-behind files are the following File SendMail.apsx.cs

using System;

using System.Collections.Generic;

 

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Net.Mail;

 

namespace SendMail

{

public partial class SendMail : System.Web.UI.Page

{

 

protected void Page_Load(object sender, EventArgs e)

{

this.btnSend.Click += new EventHandler(this.btnSend_Click);

}

 

protected void btnSend_Click(object sender, EventArgs e)

{

MailMessage emailMsg = new MailMessage(txtFrom.Text, txtTo.Text);

emailMsg.Subject = txtSubject.Text;

emailMsg.Body = txtMsg.Text;

 

// If you want to attach something uncomment the next lines:

// comment: path to file points to your attachment

//string pathToFile = “c:\\Downloads\\0122_0001.pdf”;

// comment: with media type you can control type of attachment

//Attachment atch = new Attachment(pathToFile, System.Net.Mime.MediaTypeNames.Application.Pdf);

//emailMsg.Attachments.Add(atch);

 

// Note: smtp.gmail.com is server for outgoing email messages and 587 is its port

SmtpClient client = new SmtpClient(“smtp.gmail.com”, 587);

// Note: gmail uses ssl encrypted connection

client.EnableSsl = true;

client.Credentials = new System.Net.NetworkCredential(“YourUserName@gmail.com”, “YourPassword”);

try

{

client.Send(emailMsg);

}

catch (Exception ex)

{

Console.WriteLine(“Exception caught in SendEmailMessage(): {0}”, ex.ToString());

 

}

}

}

}

File SendMail.apsx.designer.cs

//——————————————————————————

// <auto-generated>

// This code was generated by a tool.

//

// Changes to this file may cause incorrect behavior and will be lost if

// the code is regenerated.

// </auto-generated>

//——————————————————————————

 

namespace SendMail {

 

 

public partial class SendMail {

 

/// <summary>

/// form1 control.

/// </summary>

/// <remarks>

/// Auto-generated field.

/// To modify move field declaration from designer file to code-behind file.

/// </remarks>

protected global::System.Web.UI.HtmlControls.HtmlForm form1;

 

/// <summary>

/// txtFrom control.

/// </summary>

/// <remarks>

/// Auto-generated field.

/// To modify move field declaration from designer file to code-behind file.

/// </remarks>

protected global::System.Web.UI.WebControls.TextBox txtFrom;

 

/// <summary>

/// txtTo control.

/// </summary>

/// <remarks>

/// Auto-generated field.

/// To modify move field declaration from designer file to code-behind file.

/// </remarks>

protected global::System.Web.UI.WebControls.TextBox txtTo;

 

/// <summary>

/// txtSubject control.

/// </summary>

/// <remarks>

/// Auto-generated field.

/// To modify move field declaration from designer file to code-behind file.

/// </remarks>

protected global::System.Web.UI.WebControls.TextBox txtSubject;

 

/// <summary>

/// txtMsg control.

/// </summary>

/// <remarks>

/// Auto-generated field.

/// To modify move field declaration from designer file to code-behind file.

/// </remarks>

protected global::System.Web.UI.WebControls.TextBox txtMsg;

 

/// <summary>

/// btnSend control.

/// </summary>

/// <remarks>

/// Auto-generated field.

/// To modify move field declaration from designer file to code-behind file.

/// </remarks>

protected global::System.Web.UI.WebControls.Button btnSend;

}

}