In this article I will explain with an example, how to read SMTP Mail settings from
Web.Config file in
ASP.Net using C# and VB.Net.
The SMTP Mail settings are stored in the
mailSettings sub-section of the
system.net section of the
Web.Config file.
The
SmtpSection class of the
System.Net.Configuration namespace is used to read the SMTP Mail settings from
Web.Config file in
ASP.Net using C# and VB.Net.
HTML Markup
The HTML Markup consists of following controls:
TextBox – For capturing the values of Recipient
Email address, Subject and Body.
Button – For sending email.
The Button has been assigned with an OnClick event handler.
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>To:</td>
<td>
<asp:TextBox ID="txtTo" runat="server" /></td>
</tr>
<tr>
<td>Subject:</td>
<td>
<asp:TextBox ID="txtSubject" runat="server" /></td>
</tr>
<tr>
<td valign="top">Body:</td>
<td>
<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Height="150" Width="200" /></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnSend" Text="Send" runat="server" OnClick="SendEmail" /></td>
</tr>
</table>
Mail Server Settings in Web.Config file
The following Mail Server settings need to be saved in the
Web.Config file.
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="sender@gmail.com">
<network
host="smtp.gmail.com"
port="587"
enableSsl="true"
userName="sender@gmail.com"
password="SenderGmailPassword"
defaultCredentials="true"/>
</smtp>
</mailSettings>
</system.net>
MailMessage Class Properties
Following are the required properties of the MailMessage class.
From – Sender’s email address.
To – Recipient(s) Email Address.
CC – Carbon Copies. (If any)
BCC – Blind Carbon Copies. (If any)
Subject – Subject of the Email.
Body – Body of the Email.
IsBodyHtml – Specify whether body contains text or HTML mark up.
Attachments – Attachments. (If any)
ReplyTo – ReplyTo Email address.
SMTP Class Properties
Following are the properties of the SMTP class.
Host – SMTP Server URL. (Gmail: smtp.gmail.com)
EnableSsl – Specify whether your host accepts SSL Connections. (Gmail: True)
UseDefaultCredentials – Set to True in order to allow authentication based on the Credentials of the Account used to send emails.
Credentials – Valid login credentials for the SMTP server. (Gmail: email address and password)
Port – Port Number of the SMTP server. (Gmail: 587)
Namespaces
You will need to import the following namespaces.
C#
using System.Net;
using System.Net.Mail;
using System.Configuration;
using System.Net.Configuration;
VB.Net
Imports System.Net
Imports System.Net.Mail
Imports System.Configuration
Imports System.Net.Configuration
Sending Email using SMTP Mail Settings from Web.Config file in ASP.Net
When
SendEmail button is clicked, the email setting is read from
SmtpSection section of
Web.Config file.
Then, Subject and Body are fetched from their respective TextBoxes in the Contact Us Form and all these values are set into an object of the MailMessage class.
Finally, an object of the
SmtpClient class is created and the values of
Host and
Port are fetched from the
SmtpSection of the
Web.Config file and the
Email is sent using Sent method and a success message is displayed in
JavaScript Alert Message Box using
RegisterStartupScript method.
C#
protected void SendEmail(object sender, EventArgs e)
{
//Read SMTP section from Web.Config.
SmtpSection smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
using (MailMessage mm = new MailMessage(smtpSection.From, txtTo.Text))
{
//Setting Subject and Body.
mm.Subject = txtSubject.Text;
mm.Body = txtBody.Text;
mm.IsBodyHtml = false;
//Sending email.
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = smtpSection.Network.Host;
smtp.EnableSsl = smtpSection.Network.EnableSsl;
NetworkCredential networkCred = new NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password);
smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials;
smtp.Credentials = networkCred;
smtp.Port = smtpSection.Network.Port;
smtp.Send(mm);
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Email sent.');", true);
}
}
}
VB.Net
Protected Sub SendEmail(ByVal sender As Object, ByVal e As EventArgs)
'Read SMTP section from Web.Config.
Dim smtpSection As SmtpSection = CType(ConfigurationManager.GetSection("system.net/mailSettings/smtp"), SmtpSection)
Using mm As MailMessage = New MailMessage(smtpSection.From, txtTo.Text)
'Setting Subject and Body.
mm.Subject = txtSubject.Text
mm.Body = txtBody.Text
mm.IsBodyHtml = False
'Sending email.
Using smtp As SmtpClient = New SmtpClient()
smtp.Host = smtpSection.Network.Host
smtp.EnableSsl = smtpSection.Network.EnableSsl
Dim networkCred As NetworkCredential = New NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password)
smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials
smtp.Credentials = networkCred
smtp.Port = smtpSection.Network.Port
smtp.Send(mm)
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Email sent.');", True)
End Using
End Using
End Sub
Possible Errors
Errors while sending Email using Gmail
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Solution
Screenshots
Email Form
Received Email
Downloads