Sperick
Sperick

Reputation: 2761

System.Net.Mail code cannot access smtp server

I'm been using System.Net.Mail to create and send emails via a smtp server with my .Net code. The code for doing this is shown below.

            MailMessage message = new MailMessage();
            message.From = new MailAddress(ConfigurationManager.AppSettings["fromName"]);
            message.To.Add(new MailAddress(toField));
            message.Subject = GetMessageSubject(File);
            message.Body = GetMessageBody(responseRecords);
            message.IsBodyHtml = true;

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

the corresponding code in the config file is:

<!-- Configuration for specifying SMTP host for sending e-mails-->
  <system.net>
<mailSettings >
  <smtp>
     <network host="smtp-abc123.com" port="25" defaultCredentials="true"/> 
  </smtp>
</mailSettings>
  </system.net>
<appSettings>
<add key="toName" value="[email protected]" />
<add key="fromName" value="[email protected]" />
</appSettings>

When I run this locally I use credentials corresponding to my local environment and our smtp server. When I deploy the code to the QA department they use their own details and their own smtp server and so change the data in the config file.

This has worked fine in previous deployments. However they are now getting the following error:

"Failure sending mail - Unable to connect to the remote server"

The .Net code is in a service application that is located on an application server. I logged onto this app server and was able to connect to the smtp server successfully using a telnet command and the credentials in the smtp file.

I don't know what to do next in order to try and resolve this issue.

Upvotes: 4

Views: 937

Answers (1)

John Cruz
John Cruz

Reputation: 1592

Check firewall and anti-virus settings to make sure the server and/or port is not getting blocked.

Upvotes: 4

Related Questions