Wpis z mikrobloga

Mirasy, mam problem z moją klasą dotyczącą weryfikacji emaila, w skrócie przy używaniu metody SendVerificationCode(string email) wywala mi errora:

System.Net.Mail.SmtpException: 'The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required. Learn more at'


Czytałem że to przez to że używam do tego gmaila i trzeba włączyć w ustawieniach Dostęp mniej bezpiecznych aplikacji, jednak widzę że google wyłączył tą opcje co robić?

using Aplikacja.Core;
using Aplikacja.Pages;
using System;
using System.Net.Mail;
using System.Threading;

public class EmailVerification
{
private static string _code;

public static void SendVerificationCode(string email)
{
// Generate a random 6-digit code
Random random = new Random();
_code = random.Next(100000, 999999).ToString();

// Create a new email message

MailMessage message = new MailMessage();
message.To.Add(SignUpPage.NewUser.Email);
message.From = new MailAddress("email@gmail.com");
message.Subject = "Aplication Verification Code";
message.Body = $"Your verification code is {_code}. This code will expire in 60 seconds.";

// Send the email message

SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new System.Net.NetworkCredential("email@gmail.com", "hasło do maila");
smtpClient.EnableSsl = true;
smtpClient.Send(message); **//TA LINIJKA WYRZUCA ERROR O KTÓRYM MOWA**

// Wait for 60 seconds before clearing the code

Thread.Sleep(60000);
_code = null;
}

#csharp #wpf #programowanie #programista15iq
  • 5