Abhay Dhar
Abhay Dhar

Reputation: 133

Weird Issue in dotnet core website using MailKit to send email

I just inherited a website from a vendor and going through the bugs and source code for the website.

The application is built on dotnet core 1.1 and uses Mailkit to send emails via SMTP thorugh our corporate proxy.

The issue is that the mail send feature behaves erratically in sending emails. Most of the times I debug the send method it errors out. We use the Authentication Required Flag and pass the userId and Password to MailKit Authenticate Method

StackTrace message

AuthenticationInvalidCredentials: 5.7.3 Authentication unsuccessful

at MailKit.Net.Smtp.SmtpClient.Authenticate(Encoding encoding, ICredentials credentials, CancellationToken cancellationToken) at MailKit.MailService.Authenticate(String userName, String password, CancellationToken cancellationToken) at Rules.Emailer.SendNotification.Send(String to, String from, String subject, String body) in C:\Workspace\G\Rules\Emailer\SendNotification.cs:line 94 at G.Rules.Emailer.UserNotifications.ResetPassword(String

Code snippet

using (var client = new SmtpClient())
{
    client.Connect(EmailConfiguration.SmtpServer, EmailConfiguration.SmtpPort, EmailConfiguration.UseSsl);

    if (EmailConfiguration.RequiresAuthentication)
    {
        client.Authenticate(EmailConfiguration.Username, EmailConfiguration.Password);
    }

    //TODO: Only Send if PROD
    client.Send(message);
    client.Disconnect(true);
}

return true;
}

Upvotes: 0

Views: 1440

Answers (2)

To NTLM connection, you can try that:

await client.ConnectAsync(SmtpServer.SenderServer, SmtpServer.Port).ConfigureAwait(false);

if (client.AuthenticationMechanisms.Contains("NTLM"))
{
    var ntlm = new SaslMechanismNtlm(SmtpServer.UserName, SmtpServer.Password);
    await client.AuthenticateAsync(ntlm).ConfigureAwait(false);
}
else
{
    await client.AuthenticateAsync(SmtpServer.UserName, SmtpServer.Password).ConfigureAwait(false);
}

Upvotes: 1

Abhay Dhar
Abhay Dhar

Reputation: 133

Connected to smtp://<IP>:25/?starttls=when-available
S: 220 <Server_REDACTED>.<DOMAIN_REDACTED>.COM Microsoft ESMTP MAIL Service ready at Tue, 27 Mar 2018 03:22:42 -0400
C: EHLO [10.207.8.74]
S: 250-<Server_REDACTED>.<DOMAIN_REDACTED>.COM Hello [10.207.8.74]
S: 250-SIZE
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-STARTTLS
S: 250-X-ANONYMOUSTLS
S: 250-AUTH NTLM
S: 250-X-EXPS GSSAPI NTLM
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250-XEXCH50
S: 250-XRDST
S: 250 XSHADOW
C: STARTTLS
S: 220 2.0.0 SMTP server ready
C: EHLO [10.207.8.74]
S: 250-<Server_REDACTED>.<DOMAIN_REDACTED>.COM Hello [10.207.8.74]
S: 250-SIZE
S: 250-PIPELINING
S: 250-DSN
S: 250-ENHANCEDSTATUSCODES
S: 250-AUTH NTLM LOGIN
S: 250-X-EXPS GSSAPI NTLM
S: 250-8BITMIME
S: 250-BINARYMIME
S: 250-CHUNKING
S: 250-XEXCH50
S: 250-XRDST
S: 250 XSHADOW
C: AUTH LOGIN
S: 334 VXNlcm5hbWU6
C: <EMAIL_REDACTED>  
S: 334 UGFzc3dvcmQ6
C: <PSWD_REDACTED> 
S: 535 5.7.3 Authentication unsuccessful

Upvotes: 0

Related Questions