c# sending email 2022

Viewed 30

Lots of examples of how to send emails using System.Net.Mail; but I read that there has been recent upgrades to mail servers so this code no longer works for both gmail or hotmail:

void sendMail(string ip) {
    SmtpClient SmtpMail = new SmtpClient("smtp.gmail.com");

    MailMessage msg = new MailMessage();
    msg.Subject = "MailTest";
    msg.Body = "MailTest";
    msg.From = new MailAddress("email@gmail.com");
    msg.To.Add("email@gmail.com");
    msg.Priority = MailPriority.High;
    SmtpMail.EnableSsl = true;
    SmtpMail.UseDefaultCredentials = true;
    SmtpMail.Credentials = new System.Net.NetworkCredential("email@gmail.com", "password"); 

    try
    {
        SmtpMail.Send(msg);
        oldIP = newIP;
    }
    catch (Exception ec) 
    {
        int xxx = 1;
    }
    
}

Does anyone know any working code?

My question was closed due to duplicate of this: Check the 'Less secure apps' setting in your gmail account. support.google.com/accounts/answer/6010255?hl=en – Shameel 1 hour ago

But i looked in to link and google says " This setting is no longer available. "

Also I don't get authentication error, code throws exception of "Failure sending mail" and that's it.

0 Answers
Related