Nice programing

SMTP 서버에 보안 연결이 필요하거나 클라이언트가 인증되지 않았습니다.

nicepro 2020. 10. 15. 21:40
반응형

SMTP 서버에 보안 연결이 필요하거나 클라이언트가 인증되지 않았습니다. 서버 응답은 5.5.1 인증이 필요합니까?


신청서에서 이메일을 보내고 싶은데 메일 발송을 위해 다음 코드를 작성했습니다.

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

나는 그것을 localhost에서 실행하고 있으므로 그것을 보내기 위해 어떤 실수를하고 있는지.

버튼을 보내면 다음과 같은 오류가 발생합니다.

SMTP 서버에 보안 연결이 필요하거나 클라이언트가 인증되지 않았습니다. 서버 응답 : 5.5.1 인증 필요.

Web.config 파일의 코드

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

이 오류를 해결하고 메일을 보내려면 어떻게해야합니까 ??


먼저 Gmail의 보안 관련 문제를 확인하세요. Gmail에서 이중 인증을 사용하도록 설정했을 수 있습니다. 보안 알림을 받으면 Gmail받은 편지함도 확인하세요. 이 경우 아래 @mjb의 다른 답변을 확인하십시오.

다음은 이러한 문제에 대해 항상 먼저 확인하는 매우 일반적인 것입니다.

client.UseDefaultCredentials = true;

false로 설정하십시오.

@Joe King의 대답을 참고하십시오-client.UseDefaultCredentials 를 설정 하기 전에 client.UseDefaultCredentials를 설정 해야합니다.


나는 같은 문제를 가지고있다.

이 해결책을 찾았습니다.

Google은 최신 보안 표준을 사용하지 않는 일부 앱이나 기기의 로그인 시도를 차단할 수 있습니다. 이러한 앱과 기기는 침입하기가 더 쉽기 때문에 차단하면 계정을 더 안전하게 유지하는 데 도움이됩니다.

최신 보안 표준을 지원하지 않는 앱의 예는 다음과 같습니다.

  • iOS 6 이하가 설치된 iPhone 또는 iPad의 Mail 앱
  • 8.1 릴리스 이전의 Windows Phone의 메일 앱
  • Microsoft Outlook 및 Mozilla Thunderbird와 같은 일부 데스크톱 메일 클라이언트

따라서 Google 계정에서 보안 수준이 낮은 로그인 을 활성화 해야합니다.

Google 계정에 로그인 한 후 다음으로 이동하십시오.

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

C #에서는 다음 코드를 사용할 수 있습니다.

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);
    }
}

-------------------
아래 댓글 영역에서 Michael Freidgeim이 공유 한 정보 :
스크린 샷과 유사한 답변 https://stackoverflow.com/a/32457468/52277


SmtpClient.Credentials 호출 한 후 설정하십시오 SmtpClient.UseDefaultCredentials = false.

설정 SmtpClient.UseDefaultCredentials = falseSmtpClient.Credentialsnull로 재설정 되므로 순서가 중요합니다 .


몇 시간 동안 여러 가지를 검색하고 시도했습니다. 요약하면 다음 사항을 고려해야합니다.

  1. smtp.gmail.com대신 사용smtp.google.com
  2. 포트 587 사용
  3. client.UseDefaultCredentials = false;자격 증명을 설정 하기 전에 설정
  4. 보안 수준이 낮은 앱 에 대한 액세스 켜기
  5. 세트 client.EnableSsl = true;

이 단계가 도움이되지 않았다면이 답변을 확인하세요 .
System.Net.Mail FAQ 에서도 유용한 정보를 찾을 수 있습니다.


Gmail 계정에 로그인 해보십시오. Gmail SMTP를 사용하여 이메일을 보내면 잠 깁니다. 잠기기 전에 보낼 수있는 이메일의 한도를 모르겠지만 한 번 로그인하면 코드에서 다시 작동합니다. webconfig 설정이 올바른지 확인하십시오.


이 방법으로 시도해 보겠습니다.

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);
}

또한 메일 설정이있는 경우 app.config 파일을 표시하십시오.


"UseDefaultCredentials"를 false로 설정해도 동일한 문제가 발생했습니다. 나중에 근본 원인이 내 계정에서 "2 단계 인증"을 설정했기 때문이라는 것을 알게되었습니다. 전원을 끄면 문제가 사라졌습니다.


enter image description here 보안 수준이 낮은 앱 액세스가 허용되는지 확인합니다.

        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;
        }

Below is my code.I also had the same error but the problem was that i gave my password wrong.The below code will work perfectly..try it

            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");              
            mail.From = new MailAddress("fromaddress@gmail.com");
            mail.To.Add("toaddress1@gmail.com");
            mail.To.Add("toaddress2@gmail.com");
            mail.Subject = "Password Recovery ";
            mail.Body += " <html>";
            mail.Body += "<body>";
            mail.Body += "<table>";

            mail.Body += "<tr>";
            mail.Body += "<td>User Name : </td><td> HAi </td>";
            mail.Body += "</tr>";

            mail.Body += "<tr>";
            mail.Body += "<td>Password : </td><td>aaaaaaaaaa</td>";
            mail.Body += "</tr>";

            mail.Body += "</table>";
            mail.Body += "</body>";
            mail.Body += "</html>";

            mail.IsBodyHtml = true;
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("sendfrommailaddress.com", "password");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);

You can reffer it in Sending mail


If it's a new google account, you have to send an email (the first one) through the regular user interface. After that you can use your application/robot to send messages.


You should consider to specify SMTP configuration data in config file and do not overwrite them in a code - see SMTP configuration data at http://www.systemnetmail.com/faq/4.1.aspx

<system.net>
            <mailSettings>
                <smtp deliveryMethod="Network" from="admin@example.com">
                    <network defaultCredentials="false" host="smtp.example.com" port="25" userName="admin@example.com" password="password"/>
                </smtp>
            </mailSettings>
        </system.net>

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


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

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");

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.

참고URL : https://stackoverflow.com/questions/18503333/the-smtp-server-requires-a-secure-connection-or-the-client-was-not-authenticated

반응형