---
title: "Email not being sent from MVC 3 with C# using gmail a/c"  
description: "Email not being sent from MVC 3 with C# using gmail a/c"  
author: "Jayden Bell"  
published: 2013-03-02  
updated: 2013-03-02  
canonical: https://www.mindstick.com/forum/618/email-not-being-sent-from-mvc-3-with-c-sharp-using-gmail-a-c  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 1 minute  

---

# Email not being sent from MVC 3 with C# using gmail a/c

Hi [Expert](https://www.mindstick.com/articles/13120/an-expert-financial-advice-will-improve-your-finances)!

The email is not being sent from [C# code](https://www.mindstick.com/forum/403/how-to-export-data-in-excel-file-from-sql-server-using-c-sharp-code) using the gmail smtp settings. It is giving an error of "[Server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over) requires a [Secure connection](https://www.mindstick.com/forum/159459/sslhandshakeexception-when-establishing-a-secure-connection-in-a-mern-application) or the [Client](https://www.mindstick.com/articles/23198/3-steps-to-ensure-that-your-client-portal-is-impeccable) was not authenticated."

My code

MailMessage mail = new MailMessage("<from>","<to>");

[SmtpClient](https://www.mindstick.com/interview/34185/how-do-you-send-an-email-using-smtpclient-in-c-sharp) client = new SmtpClient("smtp.gmail.com", 587);\
client.Credentials = new NetworkCredential("<email>", "<[password](https://www.mindstick.com/blog/118/retriving-user-password-by-using-stored-procedure-in-asp-dot-net)>");\
client.EnableSsl = true;\
client.DeliveryMethod = SmtpDeliveryMethod.Network;\
client.Timeout = 20000;\
client.UseDefaultCredentials = false;

mail.Body = "Test email from C# Code";\
mail.Subject = "Test Email";

Console.WriteLine("Attempting to Send Email");

try {\
client.Send(mail);\
Console.WriteLine("Email sent... ");\
}\
catch (System.Net.Mail.SmtpFailedRecipientException ex) {\
Console.WriteLine("Could not send email to the mentioned recipient" + ex.Message);\
}\
catch (System.Net.Mail.SmtpException ex) {\
Console.WriteLine("Could not send Email..\n" + ex.Message + "\n" + ex.StackTrace); \
}\
Console.ReadLine();

Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)\

## Replies

### Reply by AVADHESH PATEL

Hi Jayden!

You have to swap the following lines.

client.Credentials = new NetworkCredential("<email>", "<password>");\
client.UseDefaultCredentials = false;

You have to set the UseDedaultCredentials property at first.

client.UseDefaultCredentials = false;

client.Credentials = new NetworkCredential("<email>", "<password>");

That's the way, it should work.\


---

Original Source: https://www.mindstick.com/forum/618/email-not-being-sent-from-mvc-3-with-c-sharp-using-gmail-a-c

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
