How to configure SMTP settings in web.config How to configure SMTP settings in web.config asp.net asp.net

How to configure SMTP settings in web.config


Web.Config 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>


I don't have enough rep to answer ClintEastwood, and the accepted answer is correct for the Web.config file. Adding this in for code difference.

When your mailSettings are set on Web.config, you don't need to do anything other than new up your SmtpClient and .Send. It finds the connection itself without needing to be referenced. You would change your C# from this:

SmtpClient smtpClient = new SmtpClient("smtp.sender.you", Convert.ToInt32(587));System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("username", "password");smtpClient.Credentials = credentials;smtpClient.Send(msgMail);  

To this:

SmtpClient smtpClient = new SmtpClient();smtpClient.Send(msgMail);


Set IIS to forward your mail to the remote server. The specifics vary greatly depending on the version of IIS. For IIS 7.5:

  1. Open IIS Manager
  2. Connect to your server if needed
  3. Select the server node; you should see an SMTP option on the right in the ASP.NET section
  4. Double-click the SMTP icon.
  5. Select the "Deliver e-mail to SMTP server" option and enter your server name, credentials, etc.