0

I tried to send a mail to an SMTP mail server using the code given below. But, I am getting an error -

Error: System.Reflection.TargetInvocationException: Exception has 
been thrown by the target of an invocation. ---> 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 123.456.789:587
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, 
SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, 
Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState 
state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)

This exception tells me the possible list of causes - 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 123.456.789:587

But it does not tell me exactly what is the cause - port number, wrong username, password or something else. How do I find out what is the exact cause.

Here is the code I used - Sending email with attachments from C#, attachments arrive as Part 1.2 in Thunderbird

using System.Net;
using System.Net.Mail;

public void email_send()
{
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    mail.From = new MailAddress("your mail@gmail.com");
    mail.To.Add("to_mail@gmail.com");
    mail.Subject = "Test Mail - 1";
    mail.Body = "mail with attachment";

    System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
    mail.Attachments.Add(attachment);

    SmtpServer.Port = 587;
    SmtpServer.Credentials = new System.Net.NetworkCredential("your 
    mail@gmail.com", "your password");
    SmtpServer.EnableSsl = true;

    SmtpServer.Send(mail);

}
Community
  • 1
  • 1
Steam
  • 9,368
  • 27
  • 83
  • 122
  • 1
    `because the connected party did not properly respond after a period of time` most probably there is no server at that IP address. – L.B Dec 30 '13 at 18:44
  • 1
    It tells you for sure that it's not username/password, it's just not able to connect within a timely manner. Check the IP/port. You can do it manually via telnet if you know how. – Daniel DiPaolo Dec 30 '13 at 18:45
  • @DanielDiPaolo - how do I find out if I can connect to a mail server with telnet ? – Steam Dec 30 '13 at 18:46
  • 1
    @DanielDiPaolo if it were a port problem, then something like *connection refused* would return. – L.B Dec 30 '13 at 18:47
  • trying this now - http://www.port25.com/how-to-check-an-smtp-connection-with-a-manual-telnet-session-2/ – Steam Dec 30 '13 at 18:50
  • Still not fixed. Continued here - http://social.msdn.microsoft.com/Forums/sqlserver/en-US/2e5d0f7a-5647-4b33-ac59-0d6eb55764a9/exception-in-sending-mail-with-c-task-instead-of-ssis-mail-task?forum=sqlintegrationservices#2e5d0f7a-5647-4b33-ac59-0d6eb55764a9 – Steam Dec 30 '13 at 20:36
  • and here - http://www.sqlservercentral.com/Forums/Topic1526579-364-1.aspx – Steam Dec 30 '13 at 20:37

1 Answers1

1

That IP address is obviously not correct 123.456.789:587.
Try ping smtp.gmail.com in a command prompt to see if it is resolving to that address. If so, you have that overriden somewhere, perhaps in your hosts file (C:\Windows\System32\drivers\etc\hosts).

After running your snippet ran but then I immediately got a "Sign-in attempt prevented" from Google since this method of authentication apparently does not meet their current standards.

Itsik
  • 3,920
  • 1
  • 25
  • 37
  • 1
    I can't believe I didn't realize this question is 2 years old. Somebody resurrected it into the most recent questions – Itsik Apr 10 '16 at 05:28