Click here to Skip to main content
16,011,508 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting the following error while sending email:

C#
An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code

Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at


What I have tried:

C#
[HttpPost]
        public ActionResult Index(Class1 objModelMail, HttpPostedFileBase fileUploader)
        {
            if (ModelState.IsValid)
            {
             
                string from = "<email>@gmail.com";
                using (MailMessage mail = new MailMessage(from, objModelMail.To))
                {
                    mail.Subject = objModelMail.Subject;
                    mail.Body = objModelMail.Body;
                    if (fileUploader != null)
                    {
                        string fileName = Path.GetFileName(fileUploader.FileName);
                        mail.Attachments.Add(new Attachment(fileUploader.InputStream, fileName));
                    }
                    mail.IsBodyHtml = false;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.EnableSsl = true;
                 
                    NetworkCredential networkCredential = new NetworkCredential(from, "<password>");
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = networkCredential;
                    smtp.Port = 587;

                    smtp.Send(mail);
                    ViewBag.Message = "Sent";
                    return View("Index", objModelMail);
                }
            }
            else
            {
                return View();
            }

        }
Posted
Updated 29-Feb-16 9:57am
v5
Comments
Afzaal Ahmad Zeeshan 29-Feb-16 15:51pm    
Never share your email and password details on Internet. Do not share your email address to stay safe of spammers. Do not share your password to stay safe of any potential user accessing your account.

Since your code looks good and the problem is with authentication. I think your password and/or email address are not valid or correct combination. If not that, then I guess you created a new account on Gmail and that is why, Google is not currently allow you to use API and SMTP calls to use the account. Google does that; I also had this trouble with a new account that I created.

Also, remember to always call Dispose function on your objects that can be released. A good way to do that is using using block. So, just as a template, your code would look like this:

C#
// You should use a using statement
using (SmtpClient client = new SmtpClient("<smtp-server-address>", 25))
{
   // Configure the client
   client.EnableSsl = true;
   client.Credentials = new NetworkCredential("<username>", "<password>");
   // client.UseDefaultCredentials = true;

   // A client has been created, now you need to create a MailMessage object
   MailMessage message = new MailMessage(
                            "from@example.com", // From field
                            "to@example.com", // Recipient field
                            "Hello", // Subject of the email message
                            "World!" // Email message body
                         );

   // Send the message
   client.Send(message);

   /* 
    * Since I was using Console app, that is why I am able to use the Console
    * object, your framework would have different ones. 
    * There is actually no need for these following lines, you can ignore them
    * if you want to. SMTP protocol would still send the email of yours. */
    
   // Print a notification message
   Console.WriteLine("Email has been sent.");
   // Just for the sake of pausing the application
   Console.Read();
}


What I use is:

1. Pass the values in the constructor; why waste another statement just to fill the values that can be filled in the constructor.
2. Use 25, which is the default SMTP port on TCP.
3. Do not tell the SMTP client to not use the default credentials. It will know if you pass the network credentials object.

Always make sure the username and password combination is correct and that your account is configured to be used with SMTP protocols. You can check those settings in the Settings tab of Gmail. To ensure everything, try your actual account and not testing account.

For more, please read my article, it shares much about SMTP protocol support in .NET and how to tackle a few problems: Sending emails over .NET framework, and general problems – using C# code[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900