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:02

enter image description here Make sure that Access less secure app is allowed.

        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("xx@gmail.com");
        mail.Sender = new MailAddress("xx@gmail.com");
        mail.To.Add("external@emailaddress");
        mail.IsBodyHtml = true;
        mail.Subject = "Email Sent";
        mail.Body = "Body content from";

        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
        smtp.UseDefaultCredentials = false;

        smtp.Credentials = new System.Net.NetworkCredential("xx@gmail.com", "xx");
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.EnableSsl = true;

        smtp.Timeout = 30000;
        try
        {

            smtp.Send(mail);
        }
        catch (SmtpException e)
        {
            textBox1.Text= e.Message;
        }
查看更多
男人必须洒脱
3楼-- · 2019-01-03 03:04

First check for gmail's security related issues. You may have enabled double authentication in gmail. Also check your gmail inbox if you are getting any security alerts. In such cases check other answer of @mjb as below

Below is the very general thing that i always check first for such issues

client.UseDefaultCredentials = true;

set it to false.

Note @Joe King's answer - you must set client.UseDefaultCredentials before you set client.Credentials

查看更多
你好瞎i
4楼-- · 2019-01-03 03:06

Ensure you set SmtpClient.Credentials after calling SmtpClient.UseDefaultCredentials = false.

The order is important as setting SmtpClient.UseDefaultCredentials = false will reset SmtpClient.Credentials to null.

查看更多
看我几分像从前
5楼-- · 2019-01-03 03:06

Try it this way, I just made some light changes:

MailMessage msg = new MailMessage();

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


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

    client.Send(msg);
}

Also please show your app.config file, if you have mail settings there.

查看更多
爷的心禁止访问
6楼-- · 2019-01-03 03:10

After turning less secure option on and trying other solutions, if you are still facing the same problem try to use this overload:

client.Credentials = new NetworkCredential("mymailid", "mypassword");

instead of:

client.Credentials = new NetworkCredential("mymailid", "mypassword", "smtp.gmail.com");
查看更多
倾城 Initia
7楼-- · 2019-01-03 03:12

I encountered the same problem even I set "UseDefaultCredentials" to false. Later I found that the root cause is that I turned on "2-step Verification" in my account. After I turned it off, the problem is gone.

查看更多
登录 后发表回答