Sending Email problem (.NET 2.0)  
Author Message
Bruno Ribeiro





PostPosted: .NET Framework Networking and Communication, Sending Email problem (.NET 2.0) Top

Hello folks!

I'm trying to send email using the new classes of .NET 2.0, with the following code:

MailMessage msg = new MailMessage();


msg.Subject = "Teste de envio de emails usando .NET 2.0";
msg.SubjectEncoding = Encoding.UTF8;
msg.Body = "Este e o corpo da mensagem.";
msg.BodyEncoding = Encoding.UTF8;
msg.IsBodyHtml = false;

SmtpClient client = new SmtpClient();

client.Port = 587;
client.Host = "smtp.certisign.com.br";
client.EnableSsl = true;

client.Send(msg);

Unfortunatelly I got a "Time out" exception. The System.Net tracing log is the following:

System.Net Information: 0 : [3260] Associating MailMessage#39785641 with Message#45523402
System.Net Verbose: 0 : [3260] SmtpClient::.ctor()
System.Net Information: 0 : [3260] Associating SmtpClient#35287174 with SmtpTransport#44419000
System.Net Verbose: 0 : [3260] Exiting SmtpClient::.ctor() -> SmtpClient#35287174
System.Net Verbose: 0 : [3260] SmtpClient#35287174::Send(MailMessage#39785641)
System.Net Information: 0 : [3260] SmtpClient#35287174::Send(DeliveryMethod=Network)
System.Net Information: 0 : [3260] Associating SmtpClient#35287174 with MailMessage#39785641
System.Net Information: 0 : [3260] Associating SmtpTransport#44419000 with SmtpConnection#52697953
System.Net Information: 0 : [3260] Associating SmtpConnection#52697953 with ServicePoint#22597652
System.Net Information: 0 : [3260] Associating SmtpConnection#52697953 with SmtpPooledStream#10261382
System.Net Error: 0 : [3260] Exception in the SmtpClient#35287174::Send - Unable to read data from the transport connection: Uma operacao de bloqueio foi interrompida por uma chamada a WSACancelBlockingCall.
System.Net Error: 0 : [3260] at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
at System.Net.Mail.SmtpReplyReader.ReadLine()
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message)
System.Net Verbose: 0 : [3260] Exiting SmtpClient#35287174::Send()

Any ideas

Thanks for help!

Bruno.


.NET Development22  
 
 
MarcD





PostPosted: .NET Framework Networking and Communication, Sending Email problem (.NET 2.0) Top

The obvious question would be. Are you able to connect to the same SMTP server
Tryt connecting via telnet onthe same computer
STart -> run -> cmd.exe
telnet smtp.certisign.com.br 25

And see if you can connect. Also if you're savvy then try to send an email message via telnet to see if it works.

Hope this helps.

 
 
Bruno Ribeiro





PostPosted: .NET Framework Networking and Communication, Sending Email problem (.NET 2.0) Top

Hello Marc!

Yes, I can connect via telnet, using the port 587.

By the way, I can connect using CDO classes, the problem just happens with this new .NET classes.

Thanks!

 
 
Mariya Atanasova - MSFT





PostPosted: .NET Framework Networking and Communication, Sending Email problem (.NET 2.0) Top

Did you try increasing the timeout The default value is 100,000 (100 seconds).

Mariya


 
 
Bruno Ribeiro





PostPosted: .NET Framework Networking and Communication, Sending Email problem (.NET 2.0) Top

Hello Mariya!

I tried to increase the timeout to 500000 (500 seconds). Same error :(

System.Net Information: 0 : [4188] Associating MailMessage#39785641 with Message#45523402
System.Net Verbose: 0 : [4188] SmtpClient::.ctor()
System.Net Information: 0 : [4188] Associating SmtpClient#35287174 with SmtpTransport#44419000
System.Net Verbose: 0 : [4188] Exiting SmtpClient::.ctor() -> SmtpClient#35287174
System.Net Verbose: 0 : [4188] SmtpClient#35287174::Send(MailMessage#39785641)
System.Net Information: 0 : [4188] SmtpClient#35287174::Send(DeliveryMethod=Network)
System.Net Information: 0 : [4188] Associating SmtpClient#35287174 with MailMessage#39785641
System.Net Information: 0 : [4188] Associating SmtpTransport#44419000 with SmtpConnection#52697953
System.Net Information: 0 : [4188] Associating SmtpConnection#52697953 with ServicePoint#22597652
System.Net Information: 0 : [4188] Associating SmtpConnection#52697953 with SmtpPooledStream#10261382
System.Net Error: 0 : [4188] Exception in the SmtpClient#35287174::Send - Unable to read data from the transport connection: Uma operacao de bloqueio foi interrompida por uma chamada a WSACancelBlockingCall.
System.Net Error: 0 : [4188] at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
at System.Net.Mail.SmtpReplyReader.ReadLine()
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message)
System.Net Verbose: 0 : [4188] Exiting SmtpClient#35287174::Send()

Thank you!

Bruno.

 
 
Mariya Atanasova - MSFT





PostPosted: .NET Framework Networking and Communication, Sending Email problem (.NET 2.0) Top

I'll investigate if there is a problem with the international chars and let you know later today. Other than that I am not able to telnet to your SMTP server at all.

Mariya


 
 
Bruno Ribeiro





PostPosted: .NET Framework Networking and Communication, Sending Email problem (.NET 2.0) Top

Mariya.

The address "smtp.certisign.com.br" is only reachable from inside the company network.

If there is any test I can do using this address, please let me know.

Thanks!

 
 
Mariya Atanasova - MSFT





PostPosted: .NET Framework Networking and Communication, Sending Email problem (.NET 2.0) Top

Do you have the RTM version of the framework We had a bug with SMTP in Beta2. Here is the thread on this:

http://forums.microsoft.com/msdn/showpost.aspx postid=7575&siteid=1

Mariya


 
 
Bruno Ribeiro





PostPosted: .NET Framework Networking and Communication, Sending Email problem (.NET 2.0) Top

I'm using Visual Studio 2005 Version 8.0.50727.42 (RTM.050727-4200) and Microsoft.NET Framework Version 2.0.50727.

Thanks!

 
 
Mariya Atanasova - MSFT





PostPosted: .NET Framework Networking and Communication, Sending Email problem (.NET 2.0) Top

Any firewalls, antivirus/antispyware blocking the port

Another thing: is this the complete log If not see the SSL certificates If not then you haven't been able to establish the SSL connection successfully. You'll need to use the CertificateValidationCallback and check for errors

Mariya


 
 
Bruno Ribeiro





PostPosted: .NET Framework Networking and Communication, Sending Email problem (.NET 2.0) Top

Hello Mariya.

I changed my code this way:

static private void SendMailWithDotNet20()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteServerCertificateValidationCallback);

SmtpClient client = new SmtpClient("smtp.certisign.com.br", 587);
client.EnableSsl = true;



MailMessage message = new MailMessage(from, to);

message.Body = "This is a test e-mail message.";
message.Subject = "Test email with SSL and Credentials";



client.Credentials = myCreds;

try
{
client.Send(message);
}

catch (Exception ex)
{
Console.WriteLine("Exception is:" + ex.ToString());
}

Console.WriteLine("Goodbye.");
Console.ReadLine();
}

public static bool RemoteServerCertificateValidationCallback(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
Console.WriteLine("Remote Server Cert validation callback called");
Console.WriteLine(sslPolicyErrors);
return sslPolicyErrors == SslPolicyErrors.None;
}
}

But the call back is never called. And the log remains the same.

I have antivirus and firewalls here, but I supose this is opened, cause my Outlook works well. I also can send emails using CDO classes.

Thanks!

 
 
Mariya Atanasova - MSFT





PostPosted: .NET Framework Networking and Communication, Sending Email problem (.NET 2.0) Top

This is an example of what should be in the log if the negotiation succeeds.

[Subject]
CN=smtp.gmail.com, O=Google Inc, L=Mountain View, S=California, C=US
Simple Name: smtp.gmail.com
DNS Name: smtp.gmail.com

[Issuer]
, CN=Thawte Premium Server CA, OU=Certification Services Division, O=Thawte Consulting cc, L=Cape Town, S=Western Cape, C=ZA
Simple Name: Thawte Premium Server CA

DNS Name: Thawte Premium Server CA

If you don't have the server certificates the negotiation hasn't succeeded and you have nothing to validate in the callback. Try this:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

Mariya


 
 
Bruno Ribeiro





PostPosted: .NET Framework Networking and Communication, Sending Email problem (.NET 2.0) Top

I tried "SecurityProtocolType.Ssl3" but nothing has change.

I was testing using Etheral software, for inspect the network packages. It is strange, cause nothing goes out from my IP address to the SMTP server.

 
 
MarcD





PostPosted: .NET Framework Networking and Communication, Sending Email problem (.NET 2.0) Top

There should at least be some RST or ACP commands that are sent out.

 
 
Bruno Ribeiro





PostPosted: .NET Framework Networking and Communication, Sending Email problem (.NET 2.0) Top

Sorry, I had a VPN software running on my workstation. I have turn off this, and got the following Ethereal log:

Source / Destination / Protocol / Info

192.168.11.241 / 192.168.5.30 / DNS / Standard query A smtp.certisign.com.br
192.168.5.30 / 192.168.11.241 / DNS / Standard query response A 200.225.81.134
192.168.11.241 / 200.225.81.134 / TCP / 1150 > 587 [SYN] Seq=0 Ack=0 Win=16384 Len=0 MSS=1460
200.225.81.134 / 192.168.11.241 / TCP / 587 > 1150 {SYN, ACK] Seq=0 Ack=1 Win=5840 Len=0 MSS=1460
192.168.11.241 / 200.225.81.134 / TCP / 1150 > 587 [ACK] Seq=1 Ack=1 Win=17520 [CHECKSUM INCORRECT] Len=0

192.168.11.241 ==> my workstation

192.168.5.30 ==> DNS server

200.225.81.134 ==> smtp.certisign.com.br (my SMTP server).

Thanks!