The SMTP server requires a secure connection or th

2019-01-03 02:48发布

I want to send an email from my application and i have written following code for sending mail

    MailMessage msg = new MailMessage();

    msg.From = new MailAddress("mymailid");
    msg.To.Add("receipientid");
    msg.Subject = "test";
    msg.Body = "Test Content";
    msg.Priority = MailPriority.High;

    SmtpClient client = new SmtpClient();

    client.Credentials = new NetworkCredential("mymailid", "mypassword", "smtp.gmail.com");
    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.EnableSsl = true;
    client.UseDefaultCredentials = true;

    client.Send(msg);

I am running it on localhost so what mistake i am doing to send it.

When i send button it gives an error like

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

Code in Web.config file

 <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />   
    <add key="smtpServer" value="smtp.gmail.com" />
    <add key="EnableSsl" value = "true"/>
    <add key="smtpPort" value="587" />
    <add key="smtpUser" value="sender@gmail.com" />
    <add key="smtpPass" value="mypassword" />
    <add key="adminEmail" value="sender@gmail.com" />
  </appSettings>
  <system.net>
    <mailSettings>
      <smtp from="sender@gmail.com">
        <network host="smtp.gmail.com" password="mypassword" port="587" userName="sender@gmail.com"  enableSsl="true"/>
      </smtp>
    </mailSettings>
  </system.net>

what should i do to solve this error and send mail??

标签: c# smtp
15条回答
闹够了就滚
2楼-- · 2019-01-03 03:15

try to enable allow less secure app access.

Here, you can enable less secure app after login with your Gmail.

https://myaccount.google.com/lesssecureapps

Thanks.

查看更多
欢心
3楼-- · 2019-01-03 03:16

I've searched and tried different things for hours.. To summarize, I had to take into consideration the following points:

  1. Use smtp.gmail.com instead of smtp.google.com
  2. Use port 587
  3. Set client.UseDefaultCredentials = false; before setting credentials
  4. Turn on the Access for less secure apps
  5. Set client.EnableSsl = true;

If these steps didn't help, check this answer.
Perhaps, you can find something useful on this System.Net.Mail FAQ too.

查看更多
相关推荐>>
4楼-- · 2019-01-03 03:16

Try to login in your gmail account. it gets locked if you send emails by using gmail SMTP. I don't know the limit of emails you can send before it gets locked but if you login one time then it works again from code. make sure your webconfig setting are good.

查看更多
成全新的幸福
5楼-- · 2019-01-03 03:18

I have the same problem.

I have found this solution:

Google may block sign in attempts from some apps or devices that do not use modern security standards. Since these apps and devices are easier to break into, blocking them helps keep your account safer.

Some examples of apps that do not support the latest security standards include:

  • The Mail app on your iPhone or iPad with iOS 6 or below
  • The Mail app on your Windows phone preceding the 8.1 release
  • Some Desktop mail clients like Microsoft Outlook and Mozilla Thunderbird

Therefore, you have to enable Less Secure Sign-In in your google account.

After sign into google account, go to:

https://www.google.com/settings/security/lesssecureapps

In C#, you can use the following code:

using (MailMessage mail = new MailMessage())
{
    mail.From = new MailAddress("email@gmail.com");
    mail.To.Add("somebody@domain.com");
    mail.Subject = "Hello World";
    mail.Body = "<h1>Hello</h1>";
    mail.IsBodyHtml = true;
    mail.Attachments.Add(new Attachment("C:\\file.zip"));

    using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
    {
        smtp.Credentials = new NetworkCredential("email@gmail.com", "password");
        smtp.EnableSsl = true;
        smtp.Send(mail);
    }
}

-------------------
Info shared by Michael Freidgeim in below comments area:
Similar answer with screenshots https://stackoverflow.com/a/32457468/52277

查看更多
做自己的国王
6楼-- · 2019-01-03 03:20

some smtp servers (secure ones) requires you to supply both username and email, if its gmail then most chances its the 'Less Secure Sign-In' issue you need to address, otherwise you can try:

public static void SendEmail(string address, string subject, 
    string message, string email, string username, string password, 
    string smtp, int port)
{
    var loginInfo = new NetworkCredential(username, password);
    var msg = new MailMessage();
    var smtpClient = new SmtpClient(smtp, port);

    msg.From = new MailAddress(email);
    msg.To.Add(new MailAddress(address));
    msg.Subject = subject;
    msg.Body = message;
    msg.IsBodyHtml = true;

    smtpClient.EnableSsl = true;
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Credentials = loginInfo;
    smtpClient.Send(msg);
}

notice that the email from and the username are different unlike some implementation that refers to them as the same.

calling this method can be done like so:

SendEmail("to-mail@gmail.com", "test", "Hi it worked!!", 
   "from-mail", "from-username", "from-password", "smtp", 587);
查看更多
爷的心禁止访问
7楼-- · 2019-01-03 03:25

Turn on less secure app from this link and boom...

查看更多
登录 后发表回答