给定不正确的凭据时,JavaMail SMTP传输不会引发AuthenticationFailedException

时间:2018-12-28 17:58:13

标签: java email authentication smtp javamail

我正在尝试检查是否使用JavaMail库为电子邮件帐户正确输入了SMTP设置/凭据。我遇到的问题是无论有效凭证还是无效凭证,连接都成功。

正在对启用了第三部分应用访问权限的GMail帐户进行测试,该帐户可以通过JavaMail随附的IMAP和GIMAP提供程序进行连接。如果SMTP设置正确,那么它也可以发送邮件,我只是尝试添加一个层,以便在配置新帐户时测试SMTP凭据和设置以验证正确的配置。

这里的大局是该代码所属的项目将仅用于GMail帐户,它应支持任何IMAP / SMTP电子邮件服务。

我在创建会话和传输时尝试了多种变体,并且主要遵循相关问题中的示例答案:

Javamail transport getting success to authenticate with invalid credentials

Validate smtp server credentials using java without actually sending mail

这些答案似乎不适用于我,因为问题是传输正在使用无效的凭据成功建立连接,尽管使用不是AuthenticationFailedException实例的MessagingException,但尝试发送消息的确失败。 。这2个相关问题中的第二个,多条评论声称存在相似的问题,但未提供解决方案。

// For the purposes of this code snippet getSmtpUsername() and getSmtpPassword() return a constant string value representing the username and password to be used when logging into SMTP server.

public Authenticator getSMTPAuthenticator() {
    return new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication( getSmtpUsername(), getSmtpPassword() );
        }
    };
}

public boolean authenticateSMTP( SMTPConfiguration smtpConfiguration ) throws MessagingException {
    try {
        Properties properties = new Properties(  );
        properties.put( "mail.smtp.auth", true );
        properties.put( "mail.smtp.host", "smtp.gmail.com" );
        properties.put( "mail.smtp.port", 465 );
        properties.put( "mail.smtp.socketFactory.port", 465);
        properties.put( "mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory" );

        Transport transport = Session.getInstance( properties, getSMTPAuthenticator() ).getTransport("smtp"); //.getTransport() also has not solved this issue
        transport.connect( "smtp.gmail.com", 465, getSmtpUsername(), getSmtpPassword() );
        transport.close();
        return true;
    } catch ( AuthenticationFailedException e ) { //TODO: this exception just never happens even with wrong credentials...
        return false;
    }
}

我的预期结果是,如果getSmtpUsername()或getSmtpPassword()返回的字符串值与有效帐户不一致,则将抛出AuthenticationFailedException,或者采用其他方法确定凭据是否不正确

1 个答案:

答案 0 :(得分:0)

在进行了Bill Shannon的建议更改后,事实证明该错误实际上是一个逻辑错误,在某些情况下getSmtpPassword()返回IMAP密码(导致成功登录),尽管这些建议不能解决问题。 ,更新后的代码如下:

public boolean authenticateSMTP( SMTPConfiguration smtpConfiguration ) throws MessagingException {
    try {
        Properties properties = new Properties(  );
        properties.put( "mail.smtp.auth", true );
        properties.put( "mail.smtp.host", "smtp.gmail.com" );
        properties.put( "mail.smtp.port", 465 );
        properties.put( "mail.smtp.ssl.enable", true);

        Transport transport = Session.getInstance( properties ).getTransport();
        transport.connect( getSmtpUsername(), getSmtpPassword() );
        transport.close();
        return true;
    } catch ( AuthenticationFailedException e ) {
        return false;
    }
}