A
Author
I have a SendMail method, which I have been using for a while. It
sends email by using my gmail account. It has been working perfectly
in my previous company.
Now I work for the government, and I am trying to re-use this method,
and it throws an exception which says:
<quote>
No connection could be made because the target machine actively
refused it 72.14.247.109:587
</quote>
I could ping 72.14.247.109, but telneting to this IP at port 587
failed. See details below. My c# code is also pasted at the end. I
googled a little, some people say that the firewall might be blocking
the request. But, I don't quite understand the logic here: It is
google, not my side, that is refusing the request, right?
C:\>ping 72.14.247.109
Pinging 72.14.247.109 with 32 bytes of data:
Reply from 72.14.247.109: bytes=32 time=17ms TTL=244
Reply from 72.14.247.109: bytes=32 time=21ms TTL=244
Reply from 72.14.247.109: bytes=32 time=16ms TTL=244
Reply from 72.14.247.109: bytes=32 time=16ms TTL=244
Ping statistics for 72.14.247.109:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 16ms, Maximum = 21ms, Average = 17ms
C:\>telnet 72.14.247.109 587
Connecting To 72.14.247.109...Could not open connection to the host,
on port 587
: Connect failed
// My C# code: SendMail
private static void SendMail(string fromAddress, string toAddress,
string ccAddress, string bccAddress, string subject, string
messageBody)
{
string smtpHost = "smtp.gmail.com";
int smtpPort = 587;
string smtpUserName = "my_user_name";
string smtpPassword = "my_password";
bool smtpUseSsl = true;
bool smtpUseDefaultCredentials = false;
MailMessage message = new MailMessage();
message.From = new MailAddress(fromAddress);
message.To.Add(new MailAddress(toAddress));
if (ccAddress.Length > 5)
{
message.CC.Add(new MailAddress(ccAddress));
}
if (bccAddress.Length > 5)
{
message.Bcc.Add(new MailAddress(bccAddress));
}
message.Subject = subject;
message.Body = messageBody;
System.Net.NetworkCredential cred = new
System.Net.NetworkCredential(smtpUserName, smtpPassword);
System.Net.Mail.SmtpClient mailClient = new
System.Net.Mail.SmtpClient(smtpHost, smtpPort);
mailClient.EnableSsl = smtpUseSsl;
mailClient.UseDefaultCredentials =
smtpUseDefaultCredentials;
mailClient.Credentials = cred;
mailClient.Send(message);
}
sends email by using my gmail account. It has been working perfectly
in my previous company.
Now I work for the government, and I am trying to re-use this method,
and it throws an exception which says:
<quote>
No connection could be made because the target machine actively
refused it 72.14.247.109:587
</quote>
I could ping 72.14.247.109, but telneting to this IP at port 587
failed. See details below. My c# code is also pasted at the end. I
googled a little, some people say that the firewall might be blocking
the request. But, I don't quite understand the logic here: It is
google, not my side, that is refusing the request, right?
C:\>ping 72.14.247.109
Pinging 72.14.247.109 with 32 bytes of data:
Reply from 72.14.247.109: bytes=32 time=17ms TTL=244
Reply from 72.14.247.109: bytes=32 time=21ms TTL=244
Reply from 72.14.247.109: bytes=32 time=16ms TTL=244
Reply from 72.14.247.109: bytes=32 time=16ms TTL=244
Ping statistics for 72.14.247.109:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 16ms, Maximum = 21ms, Average = 17ms
C:\>telnet 72.14.247.109 587
Connecting To 72.14.247.109...Could not open connection to the host,
on port 587
: Connect failed
// My C# code: SendMail
private static void SendMail(string fromAddress, string toAddress,
string ccAddress, string bccAddress, string subject, string
messageBody)
{
string smtpHost = "smtp.gmail.com";
int smtpPort = 587;
string smtpUserName = "my_user_name";
string smtpPassword = "my_password";
bool smtpUseSsl = true;
bool smtpUseDefaultCredentials = false;
MailMessage message = new MailMessage();
message.From = new MailAddress(fromAddress);
message.To.Add(new MailAddress(toAddress));
if (ccAddress.Length > 5)
{
message.CC.Add(new MailAddress(ccAddress));
}
if (bccAddress.Length > 5)
{
message.Bcc.Add(new MailAddress(bccAddress));
}
message.Subject = subject;
message.Body = messageBody;
System.Net.NetworkCredential cred = new
System.Net.NetworkCredential(smtpUserName, smtpPassword);
System.Net.Mail.SmtpClient mailClient = new
System.Net.Mail.SmtpClient(smtpHost, smtpPort);
mailClient.EnableSsl = smtpUseSsl;
mailClient.UseDefaultCredentials =
smtpUseDefaultCredentials;
mailClient.Credentials = cred;
mailClient.Send(message);
}