Alexandra Ramona
Alexandra Ramona

Reputation: 21

Gmail SMTP failure using c# SmtpClient

Our application has the possibility to send emails via SmtpClient. One of our customer is trying to do so by using his gmail account and it fails resulting in a timeout. However, from our testing lab it works just fine (with another gmail account).

this is the trace we got from our customer

In the trace i can see that the server is not answering with Certificate, Server Key Exchange, Server Hello Done. And im wondering what can be the cause for this?

I also noticed in the traces, the customer is using TLSv1 so I tried to replicate the error on a Windows7 system but still it works for me.

    oSmtp = new SmtpClient(this.host, this.port);
                oSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                oSmtp.EnableSsl = ssl;
                NetworkCredential oCredential = new NetworkCredential(this.userName, this.password);
                oSmtp.Credentials = oCredential;
                oSmtp.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
                string userState = null;
                oSmtp.SendAsync(oMsg, userState);

As far as the code goes, enableSsl is true, the port is 587 and we also instructed our customer to check his gmail account and allow less secure applications.

We will ask the customer for more specific details and try to put more traces in our application, but i would like to know if there is anything that can prevent the server to answer with Certificate,...

Inspecting the traces revealed no significant difference between customers Client Hello and our test Client Hello.

Thanks!

Upvotes: 2

Views: 2935

Answers (1)

gaby awad
gaby awad

Reputation: 1128

This is a working sample i used few days ago:

            string fromAddress = "[email protected]";
            var toAddress = new MailAddress("[email protected]", "To person");
            const string fromPassword = "pass";
            const string subject = "test";
            const string body = "Hey now!!";

            using (MailMessage mail = new MailMessage())
            {
                mail.From = new MailAddress(fromAddress);
                mail.To.Add(toAddress);
                mail.Subject = subject;
                mail.Body = body;
                mail.IsBodyHtml = true;
                //mail.Attachments.Add(new Attachment("C:\\file.zip"));
               
                using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
                {
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
                    smtp.EnableSsl = true;
                    smtp.Send(mail);
                }
            }

also make sure to enable Less secure app access for the gmail you are using to send data:

Upvotes: 3

Related Questions