System.Net.Sockets.SocketException on 2008R2 Server

I have a program that I want to run on a server which is able to monitor various servers on a network and send out email notifications when certain situations involving those servers occur.

Currently, I am able to run this program on my local machine, and have it fire off emails (using gmail SMTP). However, when I try to run it on the server via the command prompt, it throws the following exception:

Unhandled Exception: System.Net.Mail.SmtpException: Failure sending mail. --->
System.Net.WebException: Unable to connect to the remote server --->
System.Net.Sockets.SocketException: A connection attempt failed
because the connected party did not properly respond after a period of time,
or established connection failed because connected host has failed to respond
xxx.xxx.xxx.xxx:587

The code related to the sending of the email is fairly simple:

static void sendEmail(MailAddressCollection mailCollection, string emailSender,
                      string emailDisplayName, string emailPassword,
                      string subject, string body) {

    MailAddress fromAddress = new MailAddress(emailSender, emailDisplayName);

    SmtpClient smtpClient = new SmtpClient {
        Host = "smtp.gmail.com",
        Port = 587,
        EnableSsl = true,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential(fromAddress.Address, emailPassword)
    };

    MailMessage message = new MailMessage {
        From = fromAddress,
        Subject = subject,
        Body = body
    };

    foreach (MailAddress mailAddress in mailCollection) {
        message.To.Add(mailAddress);
    }

    smtpClient.Send(message);

}

The server in question is a 2008R2 box, and it does not have Windows Firewall enabled. Also, port 25 is definitely open, as this server can send other types of emails (specifically, those generated by a Dynamics CRM email router).

What could be causing this error?

Answers


The issue was related to Gmail blocking the IP address of the server attempting to send email.

When attempting to send emails using a Gmail account, first attempt logging into Gmail manually on that machine specific at that specific IP address, using a browser. I ultimately had to do this and go through the process of authenticating that the email send attempts from my server (being generated programmatically) were legitimate. If you do not go through this process like I did, Gmail can assume that your server is not a legitimate email sender and can continue to assume all traffic from your IP address is invalid--until you formally validate that IP address as described above.


Need Your Help

Calculate a value to update JProgressBar

java jprogressbar

I would like to display the proportion of an initial value in a JProgressBar.

Implementing a Network Stream for FTPClient from a Read buffer(VC++)

visual-c++ ftp network-programming

I have implemented a read/write stream to read a buffer, manipulate the data(like adding headers and footers during output file creation) and write it to a file. What should I do to instead of writ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.