0

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("something@gmail.com", "2-step-auth-Password");
MailAddress address = new MailAddress("something@gmail.com");

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("example@gmail.com", 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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Ed Landau
  • 966
  • 2
  • 11
  • 24
  • possible duplicate of [Can't auth to Gmail smtp via MailMessage & smtpClient](http://stackoverflow.com/questions/9104645/cant-auth-to-gmail-smtp-via-mailmessage-smtpclient) – gunwin Aug 07 '15 at 15:37
  • http://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not – Andrey Belykh Aug 07 '15 at 15:48
  • Don't your credentials need to be from a gmail account? – Jack Hughes Aug 07 '15 at 16:07
  • I do not believe it is a duplicate as the code above adheres to the solution provided: The 'UseDefaultCredentials' is set to false BEFORE I set the credentials and yet it still fails. This is why I am asking. – Ed Landau Aug 07 '15 at 16:10
  • the me@me.com are just placeolders so I do not post my credentials on this public forum. The credentials at "...@gmail.com". Edited my post to reflect. – Ed Landau Aug 07 '15 at 16:12
  • Do you have a firewall blocking the outward connection? – Jack Hughes Aug 07 '15 at 16:13
  • Did you try 25 port? – Nisarg Shah Aug 07 '15 at 16:37

1 Answers1

1

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)
{

}
  • This works. I compared it to my answer and it seems to be the same. My solution now works. I hate when that happens. It's like the toaster that only works with the person you want to show it is broken to is standing right there. THANKS FOR YOUR REPLY!! – Ed Landau Aug 07 '15 at 22:27