Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Sending emails in using SMTP server settings from web.config - Part 78

Suggested Videos
Part 75 - Logging exceptions to database
Part 76 - Customizing exception Logging
Part 77 - Sending emails using

In the previous session, we discussed about sending emails using All the SMTP server settings were configured in code. In this session, we will discuss about specifying these settings in web.config file.

Web.config settings for SMTP server. All the attributes here are self explanatory.
    <smtp deliveryMethod="Network" >
      <network host="" enableSsl="true" port="587"
        userName="" password="your_password"/>

Since the SMTP server settings are now configured in web.config. In code all you have to do is
1. Create an instance of the MailMessage class. Specify the FROM & TO email address, subject and Body
2. Create an instance of SmtpClient class, and send the email using Send() method. The SMTP settings will be automatically picked up from web.config file, when sending email.

SendEmail() method code
public static void SendEmail(string emailbody)
    // Specify the from and to email address
    MailMessage mailMessage = new MailMessage
        ("", "");
    // Specify the email body
    mailMessage.Body = emailbody;
    // Specify the email Subject
    mailMessage.Subject = "Exception";

    // No need to specify the SMTP settings as these 
    // are already specified in web.config
    SmtpClient smtpClient = new SmtpClient();
    // Finall send the email message using Send() method


  1. I am unable to configure my application to send emails using gmail. Please help..
    Below is the code:


    MailMessage msg = new MailMessage();
    msg.From = new MailAddress(txtemail.Text);
    msg.Subject = txtsubject.Text;
    msg.Body = txtcomments.Text;
    SmtpClient sm = new SmtpClient("", 587);
    sm.EnableSsl = true;
    sm.DeliveryMethod = SmtpDeliveryMethod.Network;
    sm.UseDefaultCredentials = false;
    sm.Credentials = new System.Net.NetworkCredential("", "XXXX");
    Response.Write("Email sent");
    catch (Exception ex)

    Error: Failure sending mail

  2. Dear sir,
    Bundles of thanks for such a help full material on .net
    I have a problem in sending email exception occurred given below.
    System.Net.Mail.SmtpException was unhandled by user code
    Message=Failure sending mail.
    at System.Net.Mail.SmtpClient.Send(MailMessage message)
    at WebException.Logger.SendMail(String emailbody) in c:\users\numan\documents\visual studio 2010\Projects\WebException\WebException\Logger.cs:line 58
    at WebException.WebForm1.Page_Load(Object sender, EventArgs e) in c:\users\numan\documents\visual studio 2010\Projects\WebException\WebException\WebForm1.aspx.cs:line 16
    at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
    at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
    at System.Web.UI.Control.OnLoad(EventArgs e)
    at System.Web.UI.Control.LoadRecursive()
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    InnerException: System.Net.WebException
    Message=Unable to connect to the remote server
    at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout)
    at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback)
    at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
    at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
    at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
    at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
    at System.Net.Mail.SmtpClient.GetConnection()
    at System.Net.Mail.SmtpClient.Send(MailMessage message)
    InnerException: System.Net.Sockets.SocketException
    Message=No connection could be made because the target machine actively refused it
    at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
    at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)

  3. I used this code for my blog on godaddy hostings. The code is working but mails are receiving in the spam box. How can i fix? Thanks in advance and my sincere thanks and appreciated for your work venkat. Your a great tutor, keep posting latest videos on

  4. getting error The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required


It would be great if you can help share these free resources