Ed Landau
Ed Landau

Reputation: 998

SmtpClient is unable to connect to Gmail using SSL

I see many posts on SMTPClient but I'm still stuck. I am creating a C# Console application targeting the .NET 4.0 framework and running on a Windows 2008 R2 Datacenter (server).

I want send an email using SSL from a gmail account I just created.

If I use port 587, I get an exception:

The SMTP server requires a secure connection or the client was not authenticated

If I use port 465, I get another one:

Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCall.

I realize this question has been asked several times in the past. All answers I see relate to the order in which UseDefaultCredentials is set. It must be set to false BEFORE the real credentials are set. I have followed this advice (as per the code below) and yet I still get a failure.

Other answers include switching ports from 465 to 587... which i have tried and just results in a different error (as stated above).

Here's my code:

NetworkCredential credentials = new NetworkCredential("[email protected]", "2-step-auth-Password");
MailAddress address = new MailAddress("[email protected]");

SmtpClient client = new SmtpClient("smtp.gmail.com", 465); // or 587
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = credentials;
client.Timeout = 10000; // 10 seconds.
client.Host = "smtp.gmail.com";
//client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);

MailMessage message = new MailMessage("[email protected]", recipients);
message.Subject = "Hello";
message.Body = "Goodbye";
message.BodyEncoding = Encoding.UTF8;
try
{
    client.Send(message);
}
catch (Exception ex) 
{
    Console.WriteLine("Exception caught in CreateTestMessage2(): {0}", ex.ToString());  
}
message.Dispose();

Update 2: I see another answer which may be my issue: gmail requires an application-specific password (which is apparently not available with my current gmail account. not sure why yet). but this may be my issue.

Update 3: I have followed all instructions. Generated 2-step authentication with Gmail, and still fails.

Upvotes: 0

Views: 1106

Answers (1)

Nisarg Shah
Nisarg Shah

Reputation: 61

Below code works for me, you can try this:

SmtpClient smtp = new SmtpClient("smtp.office365.com");

//Your Port gets set to what you found in the "POP, IMAP, and SMTP access" section from Web Outlook
smtp.Port = 587;
//Set EnableSsl to true so you can connect via TLS
smtp.EnableSsl = true;

System.Net.NetworkCredential cred = new System.Net.NetworkCredential("Username", "Password");

smtp.Credentials = cred;

MailMessage mail = new MailMessage();

mail.From = new MailAddress("Sender Email Address", "Sender Name");

StringBuilder mailBody = new StringBuilder();

mail.To.Add("ToEmailAddress");

mail.Subject = "Subject";
mail.IsBodyHtml = true;
mail.Body = "Body Text";

try
{
    smtp.Send(mail);
}
catch (Exception ex)
{

}

Upvotes: 1

Related Questions