Search code examples
c#emailsystem.net.mail

c# and issues with email


I'm using the following code, and get the error, "The SMTP server requires a secure connection or the client was not authenticated. The server response was: Authentication required'"


//*****************Send Email*********************

// Send email notification
string recipientEmail = "***********@mail.com";
string subject = "New BIOS version found";
string body = $"A new BIOS version ({newbiosversion}) is available for {motherboard}.";

//await smtpClient.SendMailAsync(message);
await SendEmailAsync(recipientEmail, subject, body);

static async Task SendEmailAsync(string recipientEmail, string subject, string body)
{
    // configure sending email address and password
    string email = "********@mail.com";
    string password = "********";

    // Create email message
    MailMessage message = new MailMessage();
    message.From = new MailAddress("********@mail.com");
    message.To.Add(recipientEmail);
    message.Subject = subject;
    message.Body = body;
    
    // Configure SMTP client
    SmtpClient smtpClient = new SmtpClient("smtp.mail.com", 587);
    smtpClient.UseDefaultCredentials = true;
    smtpClient.Credentials = new NetworkCredential(email,password);
    smtpClient.EnableSsl = false;

    await smtpClient.SendMailAsync(message);
}

I've created an Application-specific password on mail.com and added that to my code, I've tried setting smtpClient.EnableSsl = false; to true and false.

would be grateful for any help.


Solution

  • They no longer recommend using the System.Net.Mail.SmtpClient for new development, see the remarks on learn.microsoft.com for more info.

    If you install the MailKit package into your project, then here's a port of your code to work with it...

    using MailKit.Net.Smtp;
    using MailKit.Security;
    using MimeKit;
    using MimeKit.Text;
    
    string recipientEmail = "***********@mail.com";
    string subject = "New BIOS version found";
    string body = $"A new BIOS version ({newbiosversion}) is available for {motherboard}.";
    
    await SendEmailAsync(recipientEmail, subject, body);
    
    static async Task SendEmailAsync(string recipientEmail, string subject, string body)
    { 
        string email = "********@mail.com";
        string password = "********";
        
        var message = new MimeMessage();
        message.From.Add(MailboxAddress.Parse("********@mail.com"));
        message.To.Add(MailboxAddress.Parse(recipientEmail));
        message.Subject = subject;
        message.Body = new TextPart(TextFormat.Plain) { Text = body };
    
        using var smtp = new SmtpClient();
        smtp.Connect("smtp.mail.com", 587, SecureSocketOptions.StartTls);
        smtp.Authenticate(email, password);
        await smtp.SendAsync(message);
        smtp.Disconnect(true);
    }
    

    I don't have a mail.com account to be able to test this successfully, but if I run the above I already get a more meaningful error message...

    MailKit.Security.AuthenticationException: 535: Authentication credentials invalid