I have test this code on my development environment (windows 7, visual studio 2010) and it works grate
public static bool SendMail(string to, string subject, string message)
{
try
{
NetworkCredential loginInfo = new NetworkCredential("mylogin","mypassowrd");
MailMessage msg = new MailMessage();
msg.From = new MailAddress("mylogin");
msg.To.Add(new MailAddress(to));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com",587);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);
return true;
}
catch (Exception)
{
return false;
}
}
But when I move it to my production server (windows server 2008) it’s not working. My initial thoughts was that the firewall is blocking the port, so I create an Outbound Rule to open the port 587 with TCP protocol. Bot this doesn’t work.
Any insight will be appreciated.
Thanks
Did you verify that you can reach port 587 on smtp.gmail.com from some other tool on the server? In a production environment there are numerous places where a port can be filtered – including firewalls and routers in the network and not only the firewall on the server.
One way to verify network connectivity of the application is to try to send a mail in your application and on the same time run the command
netstat -non the console of the server. If the connection to smtp.gmail.com get stuck asSYN_SENTno TCP connection is established.