Finding the exact cause of the exception - System.Net.Sockets.SocketException

I tried to send mail to the SMTP mail server using the code below. But, I get an error message -

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 indicates to me a possible list of reasons - the connection attempt failed because the connected party did not respond properly after a period of time or the established connection failed because the connected host did not respond 123.456.789: 587

But that doesn’t tell me exactly what the reason is - port number, wrong username, password or something else. How to 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); } 
0
c # email
Dec 30 '14 at 18:42
source share
1 answer

This IP address is clearly not valid 123.456.789:587 .
Try ping smtp.gmail.com at the command line to see if it allows this address. If so, you have something, somewhere, perhaps in your hosts (C: \ Windows \ System32 \ drivers \ etc \ hosts).

After running your snippet, but then I immediately got the “Google Login Error” from Google, as this authentication method does not seem to meet their current standards.

0
Apr 10 '16 at 5:20
source share



All Articles