0 votes

I want to send mail using gmail smtp via my proxy. I'm getting an error- An unhandled exception of type 'System.IO.IOException' occurred in System.dll

Additional information: Unable to read data from the transport connection: 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.
I'm using the following code-

 MailBuilder builder = new MailBuilder();
 builder.From.Add(new MailBox("subhannitatesting@gmail.com", "Alice"));
 builder.To.Add(new MailBox("subhannitasarcar1994@gmail.com", "Bob"));
 builder.Subject = "subject";
 builder.Html = "testing";
 IMail email = builder.Create();

 ProxyFactory factory = new ProxyFactory();
 IProxyClient proxy = factory.CreateProxy(
      ProxyType.Http, 
      "10.3.100.207", 
      8080);

 Socket socket = proxy.Connect("smtp.gmail.com", Smtp.DefaultPort);   

 using (Smtp smtp = new Smtp())
 {
      smtp.Attach(socket);

      smtp.UseBestLogin("subhannitatesting@gmail.com", "password"); 

      ISendMessageResult result = smtp.SendMessage(email);

      if (result.Status == SendMessageStatus.Success)
           Console.WriteLine("Success!");
      else 
           Console.WriteLine("Failed!");

      smtp.Close();
 }

Thanks from advance!

by
At what exact line are you getting this exception?
Socket socket = proxy.Connect("smtp.gmail.com", Smtp.DefaultPort);
What is the stack trace?
>    ConsoleApplication2.exe!ConsoleApplication1.Program.Main(string[] args) Line 28    C#

Line 28 is Socket socket = proxy.Connect("smtp.gmail.com", Smtp.DefaultPort);
I'm asking about exception's StackTrace property.
Exception thrown: 'System.IO.IOException' in System.dll
The thread 0x2278 has exited with code 0 (0x0).
The thread 0x2d10 has exited with code 0 (0x0).
The program '[12756] ConsoleApplication2.vshost.exe' has exited with code 0 (0x0).
This is not the stack trace. Please catch the exception and examine its StackTrace property.

Also the exception you see in the output window (IOException) is unrelated to the connect issue. Most likely you are getting a SocketException thrown from Connect method, this means that either IP address or port are incorrect or proxy is not accepting your connection.

1 Answer

0 votes

The code looks correct. It seems your proxy is not working correctly. Are you sure this is HTTP proxy? Are you sure it is working at all?

PLease make sure the IP address and port are correct.

Additnally please try turning off your firewall and AV software.

by (297k points)
edited by
Yes it is HTTP proxy and yes it is working. This is the university proxy and I can access internet through it.
Well, the error you see is simple "I can't connect" error, so either IP or port are incorrect. Also this is a private IP so you won't be able to connect from outside.
...