Question

when run, the following code throws this exception: "Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. "

        public static void VerzendMail(Mail opgesteldeMail)
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
        mail.From = new MailAddress(opgesteldeMail.Verzender);
        mail.To.Add(opgesteldeMail.Ontvanger.Email);
        mail.Subject = opgesteldeMail.Onderwerp;
        mail.Body = opgesteldeMail.Data;

        SmtpServer.Port = 587;
        SmtpServer.Credentials = new NetworkCredential("myEmailAddress", "myPass");
        SmtpServer.EnableSsl = true;
        SmtpServer.UseDefaultCredentials = false;

        SmtpServer.Send(mail);
    }

(myEmailAddress & myPass aren't in code variables, i just wouldn't give the account away)

I've searched for a while now, since it seems a lot of people can get this error but in all their cases it's easily solved (like adding credentials or stuff) but this doesn't seem the case in my code.

The weird thing is and this is why i just don't get it, 2 months ago it worked... We were able to send emails regarding new passwords that had been generated and such. I haven't looked at this piece of code in the past 2 months, so idk, maybe someone deleted 1 line and i just can't figure out which one?

When debugging the code, i've checked all variables and they fill up good, with the correct values and such.

We also tried making a new dummy account for testing, put it in myEmailAddress & myPass and it wouldn't work aswell :.

I know a lot of people ask & have asked this question before but i just can't seem to find it and it's bugging me beyond believable measures...

Thanks for looking into this!

Jep.

Was it helpful?

Solution

try modifying this segment of code:

SmtpServer.Credentials = new NetworkCredential("myEmailAddress", "myPass");
SmtpServer.EnableSsl = true;
SmtpServer.UseDefaultCredentials = false;

to this:

SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new NetworkCredential("myEmailAddress", "myPass");
SmtpServer.EnableSsl = true;

In other words, try disabling UseDefaultCredentials before setting the credentials to use.

The order that the properties are set may make a difference.

I'm pretty sure I've seen a similar question and reordering things worked. I could be misremembering, though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top