1

Closed

SMTP Mail Send not working with port 587 (SSL)

description

I was trying to use Gmail's smtp to send mail but it fails with the following exception:

System.IO.IOException was unhandled
Message=The handshake failed due to an unexpected packet format.
Source=System
StackTrace:
   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Net.Security.SslStream.AuthenticateAsClient(String targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, Boolean checkCertificateRevocation)
   at System.Net.Security.SslStream.AuthenticateAsClient(String targetHost)
   at Higuchi.Net.Smtp.SmtpClient.Open() in C:\Documents and Settings\Binit\My Documents\Downloads\csharpmail-60313\Mail\Mail\Smtp\SmtpClient.cs:line 321
   at Higuchi.Net.Smtp.SmtpClient.EnsureOpen() in C:\Documents and Settings\Binit\My Documents\Downloads\csharpmail-60313\Mail\Mail\Smtp\SmtpClient.cs:line 355
   at Higuchi.Net.Smtp.SmtpClient.SendMail(String from, List`1 rcptTo, String text) in C:\Documents and Settings\Binit\My Documents\Downloads\csharpmail-60313\Mail\Mail\Smtp\SmtpClient.cs:line 916
   at Higuchi.Net.Smtp.SmtpClient.SendMail(String from, String to, String cc, String bcc, String text) in C:\Documents and Settings\Binit\My Documents\Downloads\csharpmail-60313\Mail\Mail\Smtp\SmtpClient.cs:line 885
   at ConsoleTest.Program.Main(String[] args) in C:\Documents and Settings\Binit\My Documents\Downloads\csharpmail-60313\Mail\ConsoleTest\Program.cs:line 34
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

InnerException:

 

Test code is as follows:

        SmtpClient Gmailsc = new SmtpClient()
        {
            ServerName = "smtp.gmail.com",
            Port = 587,
            UserName = "xxxx@gmail.com",
            Password = "yyyyy",
            Ssl = true,
            ReceiveTimeout = 50000,
        };
 

SendMailResult res = Gmailsc.SendMail("xxxx@gmail.com", "yyyyy@gmail.com", "", "", "hellow orld");

 
I cannot use .net's inbuilt mail sender because it cannot send mails when using implicit SSL at port 465. C#Mail is nice because it can send mails using implicit SSL at port 465.
In short, .net mail can send with explicit SSL (587) and C#Mail can send with implicit SSL (465) but not vice versa.
 
If C# Mail could also send to 587 port, it would be great!
 
Thanks,
Binit
Closed Dec 12, 2011 at 3:36 AM by higty

comments

higty wrote Jul 26, 2011 at 9:11 AM

Now I start what wrong in this case.
Please wait few days.

higty wrote Jul 26, 2011 at 9:27 AM

I can send mail with below code.
1.Set Tls property true and set Ssl property false.

2.Change value of from parameter of SendMail method xxx@gmail.com

SmtpClient Gmailsc = new SmtpClient()
{
ServerName = "smtp.gmail.com",
Port = 587,
UserName = "xxxx@gmail.com", Password = "yyyyy",
Tls = true,
Ssl = false,
ReceiveTimeout = 50000,
};

SendMailResult res = Gmailsc.SendMail("xxxx@gmail.com", "yyyyy@gmail.com", "", "", "hellow orld");

regard.

binit wrote Jul 26, 2011 at 8:24 PM

I tried the changes that you have mentioned, Tls=true, Ssl=false and from parameter enclosed by < >.
But still its not working.
It is not giving exception, but res.SendSuccessful is false, and mail is also not going.

Thanks,
Binit.

higty wrote Jul 29, 2011 at 2:51 AM

OK.
I continue investigation.
Please wait.

higty wrote Jul 29, 2011 at 3:54 AM

I found and fixed bug.
Please check out latest version.

wrote Dec 12, 2011 at 3:36 AM

wrote Feb 14, 2013 at 6:38 PM

wrote May 16, 2013 at 8:35 AM