+1 vote
by (430 points)
edited

hello,

After calling login method, with the Rebex.Net.SmtpAuthentication.Auto option set: Is there a way to know which authentication method has been used by the component to log in? (without analyzing the ESMTP dialog ?

thanks and best regards, fritz

Applies to: Rebex Secure Mail

2 Answers

0 votes
by (147k points)
edited
 
Best answer

This is not possible at the moment, although adding this feature to one of the future releases sounds like a good idea!

However, analyzing the SMTP communication to detect this is not very hard. You can use the following class for this purpose:

C#:

public class AuthenticationMethodDetector
{
    public static SmtpAuthentication Login(Smtp smtp, string userName, string password, SmtpAuthentication method)
    {
        AuthenticationMethodDetector detector = new AuthenticationMethodDetector();
        SmtpCommandSentEventHandler commandSent = new SmtpCommandSentEventHandler(detector.smtp_CommandSent);
        smtp.CommandSent += commandSent;
        try
        {
            smtp.Login(userName, password, method);
            return detector._method;
        }
        finally
        {
            smtp.CommandSent -= commandSent;
        }
    }

    private SmtpAuthentication _method;

    private void smtp_CommandSent(object sender, SmtpCommandSentEventArgs e)
    {
        string command = e.Command;
        if (command.StartsWith("AUTH "))
        {
            int n = command.IndexOf(' ', 5);
            if (n < 0)
                n = command.Length;
            string method = command.Substring(5, n - 5);
            switch (method)
            {
                case "CRAM-MD5":
                    _method = SmtpAuthentication.CramMD5;
                    break;
                case "DIGEST-MD5":
                    _method = SmtpAuthentication.DigestMD5;
                    break;
                case "PLAIN":
                    _method = SmtpAuthentication.Plain;
                    break;
                case "LOGIN":
                    _method = SmtpAuthentication.Login;
                    break;
                case "NTLM":
                    _method = SmtpAuthentication.Ntlm;
                    break;
                case "GSSAPI":
                    _method = SmtpAuthentication.GssApi;
                    break;
                default:
                    throw new InvalidOperationException("Unexpected authentication method.");
            }
        }
    }
}

Then, instead of calling smtp.Login(userName, password, method), call AuthenticationMethodDetector.Login - it returns the selected authentication method.

If you prefer VB.NET, please let me know!

by (430 points)
thanks Lukas. - fritz
0 votes
by (140 points)
edited

i prefer, in vb.net

please

Ivan Monroy

...