SMTP client: authentication method which has been used

+1 vote
asked Jun 22, 2010 by Fritz (430 points)
edited Jul 28, 2010

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
answered Jun 22, 2010 by Lukas Pokorny (104,230 points)
edited Jun 22, 2010
 
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!

commented Jun 23, 2010 by Fritz (430 points)
thanks Lukas. - fritz
0 votes
answered Jul 28, 2010 by Ivan Monroy (140 points)
edited Jul 28, 2010

i prefer, in vb.net

please

Ivan Monroy

...