38

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>
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
rumi
  • 3,293
  • 12
  • 68
  • 109
  • 1
    Just [configure IIS](http://stackoverflow.com/questions/239262/can-i-configure-smtp-in-iis-so-it-relays-to-a-remote-smtp-server) so that SMTP localhost fowards mail to relay.tagadab.com. – mr_plum Oct 07 '13 at 20:42
  • @nunzabar can you kindly give me an example how can we forward it – rumi Oct 07 '13 at 21:02

4 Answers4

84

By setting the values <mailSettings> section of the in the web.config you can just new up an SmtpClient and the client will use those settings.

https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient.-ctor?view=net-6.0#system-net-mail-smtpclient-ctor

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>

C#:

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

However, if authentication is needed, I would suggest using a configuration provider that is more secure for usernames and passwords and settings them with a NetworkCredentials object.

C#:

SmtpClient smtpClient = 
    new SmtpClient(_configuration.SmtpHost, _configuration.SmtpPort);
smtpClient.Credentials = 
    new NetworkCredential(_configuration.EmailUsername, _configuration.EmailPassword)
smtpClient.Send(msgMail);
Brenton Major
  • 500
  • 6
  • 10
Sanjay kumar
  • 1,653
  • 12
  • 18
8

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);
Eric Barker
  • 81
  • 1
  • 2
  • 1
    Thank you for the explanation, I almost didn't use the code from the answer above because I didn't understand how it was going to work in my controller. Much appreciated. – Jamie Nov 13 '18 at 17:46
5

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.
mr_plum
  • 2,448
  • 1
  • 18
  • 31
  • I already tried to add a section in web.config (please see my question )but that does not make a difference. I see exactly the same thing in the IIS when I open SMTP email – rumi Oct 07 '13 at 21:28
  • Looking a little closer, you have to determine how `SmtpMail` is instantiated. Web.config will only be referenced if you use the [default constructor](http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.smtpclient.aspx). – mr_plum Oct 08 '13 at 13:03
0

Screenshot of my web.config email settings

This is the actual code in my web.config file sending YetAnotherForum forum emails and also the same code I use in StarterTrack helpDesk.

Michael
  • 112
  • 4