How to configure email smtp settings in web.config using asp .net
In this asp .net tutorial we will learn about how to configure email smtp settings in web.config to send emails in asp .net.
Sending emails is the major part of any web application, most of the webapplications today needs a functionality to send emails
may be it is used to send newsletters, promotional emails, scheduled reports or username/password informations.Saving email settings in web.config
will help you easily change SMTP settings throughout the webapplicaton without breaking email sending code.
To send emails from asp .net or any other webapplication there is a need of SMTP (Simple Mail Transfer Protocol) server. SMTP is an global internet standard to send and receive emails.
There are multiple ways to use SMTP server to send emails in asp .net:
- Using your own SMTP Server.
If you have hosted your webapplication on server that provides you an option to install and use third party softwares you can install and use any free or paid SMTP servers available in the market.
I have tried HMail server to send emails in asp .net here is the step by step installation guide of HMail server setup.
- SMTP Server provided by Email service providers.
This option is very usefull when you are working on your local system and you need to test your email code. In this scenerio you can use SMTP server provided by email server providers like Gmail, Rediff, Yahoo etc.
You can use these SMTP servers without any charge but there are some limits like you cannot use these SMTP servers on some hosting providers, there are some limits on number of emails send etc.
Here is the list of top 10 email service providers you can use to send emails in your asp .net application.
- SMTP Server provided by hosting providers.
Every hosting provider have their own SMTP servers. You can use hosting provider's server after hosting website on their server. You just need to change SMTP settings
as per provided by the hosting provider. Here is the details information about Sending emails using godaddy hosting provider in asp .net
SMTP Settings in web.config
Required namespaces to send emails in asp .net
using System.Configuration;
using System.Net.Mail;
using System.Net;
using System.Net.Configuration;
Codebehind to send emails in asp .net
SmtpSection secObj = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
using (MailMessage mm = new MailMessage())
{
mm.From = new MailAddress(secObj.From); //--- Email address of the sender
mm.To.Add("emailAddressToSendEmail"); //---- Email address of the recipient.
mm.Subject = "We are trying to send email using SMTP settings."; //---- Subject of email.
mm.Body = "Hello this is content of the email"; //---- Content of email.
SmtpClient smtp = new SmtpClient();
smtp.Host = secObj.Network.Host; //---- SMTP Host Details.
smtp.EnableSsl = secObj.Network.EnableSsl; //---- Specify whether host accepts SSL Connections or not.
NetworkCredential NetworkCred = new NetworkCredential(secObj.Network.UserName, secObj.Network.Password);
//---Your Email and password
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587; //---- SMTP Server port number. This varies from host to host.
smtp.Send(mm);
}
Configure email smtp settings in web.config demo: