C# SmtpClient class not able to send email using gmail

You won’t believe what fixed my problem. The Credentials property ss.Credentials = new NetworkCredential(“username”, “pass”); must be declared after ss.UseDefaultCredentials = false; So the final working code listing is SmtpClient ss = new SmtpClient(“smtp.gmail.com”, 587); ss.EnableSsl = true; ss.Timeout = 10000; ss.DeliveryMethod = SmtpDeliveryMethod.Network; ss.UseDefaultCredentials = false; ss.Credentials = new NetworkCredential(“username”, “pass”); MailMessage mm = … Read more

How to send email using simple SMTP commands via Gmail?

to send over gmail, you need to use an encrypted connection. this is not possible with telnet alone, but you can use tools like openssl either connect using the starttls option in openssl to convert the plain connection to encrypted… openssl s_client -starttls smtp -connect smtp.gmail.com:587 -crlf -ign_eof or connect to a ssl sockect directly… … Read more

Sending email fails when two factor authentication is on for Gmail

Create a custom app in you Gmail security settings. Log-in into Gmail with your account Navigate to https://security.google.com/settings/security/apppasswords In ‘select app’ choose ‘custom’, give it an arbitrary name and press generate It will give you 16 chars token. Use the token as password in combination with your full Gmail account and two factor authentication will … Read more

What are best practices for using SmtpClient, SendAsync and Dispose under .NET 4.0

The original question was asked for .NET 4, but if it helps as of .NET 4.5 SmtpClient implements async awaitable method SendMailAsync. As a result, to send email asynchronously is as the following: public async Task SendEmail(string toEmailAddress, string emailSubject, string emailMessage) { using (var message = new MailMessage()) { message.To.Add(toEmailAddress); message.Subject = emailSubject; message.Body … Read more

Why do I get “‘property cannot be assigned” when sending an SMTP email?

mail.To and mail.From are readonly. Move them to the constructor. using System.Net.Mail; … MailMessage mail = new MailMessage(“you@yourcompany.example”, “user@hotmail.com”); SmtpClient client = new SmtpClient(); client.Port = 25; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Host = “smtp.gmail.com”; mail.Subject = “this is a test email.”; mail.Body = “this is my test email body”; client.Send(mail);