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

I'm trying to fix an email issue with an inherited website and don't have access to the code (i.e. just the compiled files). This site needs to be hosted on a new web server having a different SMTP server.

Upon decompiling bits of the code I can see emails are sent using method like below in code snippet and SMTP is set as smtpMail.SmtpServer="localhost" but my new webserver's SMTP server is "relay.tagadab.com" how can we possibly configure this in web.config so that localhost is taken as "relay.tagadab.com"

Imports Microsoft.VisualBasic, System.Web.Mail

Shared Sub SendMail(ByVal ToAdd, ByVal FromAdd, ByVal Message, ByVal Subject)

    Dim msgMail As New MailMessage()

    msgMail.To = ToAdd
    msgMail.From = FromAdd
    msgMail.Subject = Subject
    msgMail.Headers.Add("X-Mailer", "ASP.NET")

    msgMail.BodyFormat = MailFormat.Text
    msgMail.Body = Message
    'SmtpMail.SmtpServer = "mail.the-radiator.com"
    SmtpMail.SmtpServer = "localhost"
    SmtpMail.Send(msgMail)

End Sub

I added this section in my web.config but that does not make a difference

<system.net>
    <mailSettings>
        <smtp>
            <network host="relay.tagadab.com" port="25" />
        </smtp>
     </mailSettings>
</system.net>

This question is related to asp.net .net vb.net smtp email-integration

The answer is


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);

Web.Config file:

<configuration>
 <system.net>
        <mailSettings>
            <smtp from="[email protected]">
                <network host="smtp.gmail.com" 
                 port="587" 
                 userName="[email protected]" 
                 password="yourpassword" 
                 enableSsl="true"/>
            </smtp>
        </mailSettings>
</system.net>
</configuration>

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.

Examples related to asp.net

RegisterStartupScript from code behind not working when Update Panel is used You must add a reference to assembly 'netstandard, Version=2.0.0.0 No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization How to use log4net in Asp.net core 2.0 Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state How to create roles in ASP.NET Core and assign them to users? How to handle Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause() ASP.NET Core Web API Authentication Could not load file or assembly 'CrystalDecisions.ReportAppServer.CommLayer, Version=13.0.2000.0 WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery

Examples related to .net

You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to use Bootstrap 4 in ASP.NET Core No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Update .NET web service to use TLS 1.2 EF Core add-migration Build Failed What is the difference between .NET Core and .NET Standard Class Library project types? Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Nuget connection attempt failed "Unable to load the service index for source" Token based authentication in Web API without any user interface

Examples related to vb.net

How to get parameter value for date/time column from empty MaskedTextBox HTTP 415 unsupported media type error when calling Web API 2 endpoint variable is not declared it may be inaccessible due to its protection level Differences Between vbLf, vbCrLf & vbCr Constants Simple working Example of json.net in VB.net How to open up a form from another form in VB.NET? Delete a row in DataGridView Control in VB.NET How to get cell value from DataGridView in VB.Net? Set default format of datetimepicker as dd-MM-yyyy How to configure SMTP settings in web.config

Examples related to smtp

5.7.57 SMTP - Client was not authenticated to send anonymous mail during MAIL FROM error PHPMailer - SMTP ERROR: Password command failed when send mail from my server php function mail() isn't working Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required "An attempt was made to access a socket in a way forbidden by its access permissions" while using SMTP Getting error while sending email through Gmail SMTP - "Please log in via your web browser and then try again. 534-5.7.14" SmtpException: Unable to read data from the transport connection: net_io_connectionclosed How to configure SMTP settings in web.config Send mail via CMD console Mail not sending with PHPMailer over SSL using SMTP

Examples related to email-integration

Trying to get Laravel 5 email to work How to configure SMTP settings in web.config Can there be an apostrophe in an email address? Undefined symbols for architecture i386: _OBJC_CLASS_$_SKPSMTPMessage", referenced from: error Sending emails in Node.js? How to send 100,000 emails weekly? Is it possible to add an HTML link in the body of a MAILTO link