-1

I'm new to C# and resonantly started to study I tried sending a Google mail and it is throwing this error Please help me to finish my work

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

using (MailMessage mm = new MailMessage("sender@gmail.com", txtEmail.Text))
    {
        mm.Subject = "Account Activation";
        string body = "Hello " + txtUsername.Text.Trim() + ",";
        body += "<br /><br />Please click the following link to activate your account";
        body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>";
        body += "<br /><br />Thanks";
        mm.Body = body;
        mm.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>");
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mm);
    }
janagan
  • 15
  • 1
  • 7
  • AFAIK, setting the `EnableSsl` property to `true`, doesn't necessarily mean "send over SSL" - I *think* it just means that the request is coming from a HTTPS source. Try removing this, and then also try using `Port` `465`, instead :) Also, don't think you need the `UseDefaultCredentials = true` - might need to `false` it, to set the `Credentials` to `null` *before* setting the Credentials yourself – Geoff James Sep 01 '16 at 12:00
  • 4
    possible duplicate: http://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not – Radinator Sep 01 '16 at 12:01
  • smtp.UseDefaultCredentials = false; smtp.EnableSsl = true; – Hitesh Thakor Sep 01 '16 at 12:03
  • 1
    I remember once stumbling upon a similar problem. Gmail itself was blocking the mail from sending saying that it was coming from an untrusted application. I had to enable it from Gmail settings. – TKharaishvili Sep 01 '16 at 12:04
  • 1
    Did you even try to read what's under "read more at..."? – Camilo Terevinto Sep 01 '16 at 12:07
  • @Radinator while it is tempting to vote for that as a duplicate - it doesn't actually address the biggest error in the OP's code, which is that he has `UseDefaultCredentials` set to `true`. – user1666620 Sep 01 '16 at 13:55
  • @GeoffJames port 587 is the correct port to use for `smtp.google.com`. The `EnableSsl` property implicitly tells the SmtpClient to use TLS when set to `true`. – user1666620 Sep 01 '16 at 13:55
  • @user1666620 That's fair enough. I've seen a couple of similar issues to this before, and `465` sometimes solved the issue. Regarding `EnableSsl`, I've ready before (can't remember where), that it was something to do with the server the program is running on not using SSL. Maybe it was wrong or I remembered wrong. Thanks for clarifying! :) – Geoff James Sep 01 '16 at 14:06
  • I know this might seem a random question - but you're not using two-factor authentication, are you? – Geoff James Sep 01 '16 at 14:10
  • @GeoffJames sometime if the server doesn't support TLS you need to use the deprecated System.Web.Mail, which allows use of SSL. These things can be a pain in the ass. – user1666620 Sep 01 '16 at 14:12
  • @user1666620 Good to know, thanks! :) And you're telling me. Had my own headache on a project with SMTP issues and working on localhost vs server - turned out it was the hosting that was blocking outbound to other SMTP servers on 587 or 465! Such a PITA... >:o( – Geoff James Sep 01 '16 at 14:19

5 Answers5

0

With gmail is correct to set EnableSSL to true and set Port to 587, because SmtpClient class doesn't work with port 465. See this article http://www.codeproject.com/KB/IP/GmailSmtp.aspx?q=SmtpClient+ssl+yahoo I think the problem is smtp.UseDefaultCredentials = true. Try to remove this row.

Hi.

Andrea
  • 112
  • 1
  • 13
0

You should have got an email from google stating they have blocked the access of a less secure app.

you will need to set UseDefaultCredentials = false and need to allow access for less secure apps from https://www.google.com/settings/security/lesssecureapps?rfn=27&rfnc=1&asae=2&anexp=lbe3-R2_C

I have done this and my app is able to send emails using gmail.

Sujit Singh
  • 799
  • 6
  • 11
0
To Send email using Gmail server we need to set following thing.Use these namespaces in C#.


     1. using System.IO;
     2. using System.Net;
     3. using System.Net.Mail;

MailMessage Class Properties

Following are the required properties of the MailMessage class.

 - From – Sender’s email address 
 - To – Recipient(s) Email Address 
 - CC –Carbon Copies (if any) 
 - BCC – Blind Carbon Copies (if any) 
 - subject –Subject of the Email  
 - Body – Body of the Email 
 - IsBodyHtml – Specify whether body contains text or HTML tag.
 - Attachments – Attachments(if any)
 - ReplyTo – ReplyTo Email address.

 SMTP Class Properties

  Following are the properties of the SMTP class.
  - Host – SMTP Server URL (Gmail: smtp.gmail.com)
  - EnableSsl – Specify whether your host accepts SSL Connections (Gmail: True)
  - UseDefaultCredentials – Set to True in order to allow authentication based on the Credentials of the Account used to send emails
  - Credentials – Input valid username and password
  - Port – Assign port number for (Gmail: 587)

Finally here is your Code.  

       using (MailMessage mm = new MailMessage(from, to))
        {
            mm.Subject = "Account Activation";
            string body = "Hello " + your username.Trim() + ",";
            body += "<br /><br />Please click the following link to activate your account";
            body += "<br /><a href = '" + new Uri("http://www.google.com", true).AbsoluteUri + "<a>Click here to activate your account.</a>";
            body += "<br /><br />Thanks";
            mm.Body = body;
            mm.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            NetworkCredential NetworkCred = new NetworkCredential(username, password);
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
        }

If you are getting the error Like this:-

-The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

Then you Do the following steps:- Click on below link. - https://www.google.com/settings/security/lesssecureapps

Click on radio button Turn on.

It will work.

Code is tested. Thanks..

Ramdeo angh
  • 179
  • 9
0

You are defining the login credentials for your gmail account:

NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>");

But then you are overwriting these details by setting the UseDefaultCredentials property to true.

smtp.UseDefaultCredentials = true;

You need to remove that line of code as it is telling your application to attempt to log into gmail with your windows credentials. Alternatively, if that doesn't work, you can set it to false prior to providing the new NetworkCredentials:

smtp.UseDefaultCredentials = false;
NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>");

After that, if you keep getting the error, you will need to go into your gmail account security settings and allow less-secure applications.

user1666620
  • 4,800
  • 18
  • 27
0

Hello My friend I just come up with this error, it's just you didn't change anything the sender@gmail.com and the password, just put the gmail you created and the password, like you see in my modification below there's actually nothing wrong with the code just put the email you created then the password of that gmail , actually all of the answers above was correct but you must remove that "<>" to your password and you didn't change the sender@gmail.com to your respective gmail......

      NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>");


      NetworkCredential NetworkCred = new NetworkCredential("yourgmail.com", "yourpassword");
  • Hope my simple answers helps you, I just use this code also and the only problem when, I found out this code is that I didn't change anything... I see the error of your's for so many times. – pantera ureN Sep 06 '16 at 01:24