-1

I've been struggeling a lot with this now.

I try to send mail with my mvc application using my google apps account. But I keep getting errors. It doesn't matter which settings I use. I tried using both port 465 and 587 with ssl and authentication turned on. With 465 I get Operation Timed Out and with 587 I get this message:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

I tried turning of the firewall with no luck. I have also tried to turn off 2-step authentication but I figured out that it wasn't even turned on.

I hope that you can help me
Regards

Here is the code as requested:

public static void SendMail(MailAddress from, MailAddress to, string subject, string body) {
    MailMessage mail = new MailMessage();
    mail.From = from;
    mail.To.Add(to);
    mail.Subject = subject;
    mail.Body = body;


    SmtpClient client;
    if (Settings.Port != null)
        client = new SmtpClient(Settings.Host, Int32.Parse(Settings.Port));
    else
        client = new SmtpClient(Settings.Host);

    client.EnableSsl = Settings.UseSSL;

    client.Timeout = 50000;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;

    if (Settings.UseAuthentication) {
        client.Credentials = new NetworkCredential(Settings.Username, Settings.Password);
        client.UseDefaultCredentials = false;
    }

    client.Send(mail);
}

I have checked all obvius stuff like username and password (username is formed like user@domain.com). I've also stepped through the code to verify my settings class is working as it should

jorx
  • 481
  • 5
  • 7
  • 1
    Please show the code you are using to connect and send an e-mail via SMTP... without that you're likely to get downvoted... BUT, just glancing at the error should tell you what's wrong... you aren't authenticating (username / password). – Evan L Oct 17 '14 at 14:35
  • I'm going to guess you have an issue with `smtp.Credential = new NetworkCredential(username, password);`. Without code though we will never know. – Greg Oct 17 '14 at 14:40

1 Answers1

0

I found out what the problem was. client.UseDefaultCredentials = false; must go before the credentials is set. Now client.Sendmethod returns without exceptions.

jorx
  • 481
  • 5
  • 7
  • Would you like to re-write this post as an answer and then mark it as one? That way people can use this for the reference in the future :) – Sometowngeek Jul 06 '16 at 03:20