In
this article i'm going to explain how to Send Mail with configure SMTP
Mail Settings defined in Web.Config File using ASP.NET with C#.
ASP.NET allows applications to send
e-mail by using the Simple Mail Transfer Protocol (SMTP). Here I’ll show
you how to configure smtp mail settings defined in web.config file.
Elements:
<system.Net>:
Contains settings that specify how the
.NET Framework connects to the network.
<mailSettings>:
Configures mail sending options.
<smtp>:
Configures the delivery method and from
address for sending e-mails.
Attributes and Elements
The
following sections describe attributes, child elements, and parent elements.
Attributes
Attribute
|
Description
|
deliveryMethod
|
Specifies the delivery
method for e-mails. Acceptable values are network, pickupDirectoryFromIis,
and specifiedPickupDirectory.
|
from
|
Specifies the from address
for e-mails.
|
Child Elements
Attribute
|
Description
|
specifiedPickupDirectory
|
Configures the local
directory for a Simple Mail Transport Protocol (SMTP) server.
|
network
|
Configures the network
options for an external SMTP server.
|
Properties
Host:
Gets or sets the name or IP address of
the host used for SMTP transactions.
Port :
Gets or sets the port used for SMTP
transactions.
UseDefaultCredentials:
Gets or sets a Boolean value that controls whether the DefaultCredentials are sent with requests.
Designer Source code:
Ex: SendMail.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="600px" align="center">
<tr>
<td colspan="2" align="center"><b>Send Mail</b></td>
</tr>
<tr>
<td> To </td>
<td> <asp:TextBox ID="txtToMail" runat="server"></asp:TextBox> </td>
</tr>
<tr>
<td> Subject </td>
<td> <asp:TextBox ID="txtSubject" runat="server" Width="400"></asp:TextBox></td>
</tr>
<tr>
<td> Message </td>
<td> <asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine"
Width="400px" Height="200px"></asp:TextBox></td>
</tr>
<tr>
<td> </td>
<td>
</td>
</tr>
<tr>
<td colspan="2" align="center"> <asp:Button ID="btnSubmit" runat="server"
OnClick="btnSubmit_Click" Text="Send" /> </td>
</tr>
<tr>
<td colspan="2"><asp:Label ID="lblMsg" runat="server" ></asp:Label> </td>
</tr>
</table>
</div>
</form>
</body>
</html>
Now we have to configure smpt mail settings under <configuration> section in web.config file
Web.Confilg file:
<configuration>
<system.net>
<mailSettings>
<smtp from="yourmail@gmail.com">
<network host="smtp.gmail.com"
port="587"
userName="yourmail@gmail.com"
password="yourpassword"
enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Configuration;
public partial class SendMail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage mailMessage = new MailMessage()
{
Subject = txtSubject.Text,
Body = txtMessage.Text,
IsBodyHtml = false
};
mailMessage.To.Add(new MailAddress(txtToMail.Text));
SmtpClient smtpClient = new SmtpClient();
smtpClient.SendAsync(mailMessage, null);
lblMsg.Text = "Email has been successfully sent..!!";
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
Note: Don’t forgot to add Async="true" attribute to @ Page directive