JavaMail SMTP Transport 在提供不正确的凭据时不会抛出 AuthenticationFailedException
JavaMail SMTP Transport does not throw AuthenticationFailedException when given incorrect credentials
我正在尝试检查是否为使用 JavaMail 库的电子邮件帐户正确输入了 SMTP 设置/凭据。我遇到的问题是无论凭据有效还是无效,连接都会成功。
这正在 GMail 帐户上进行测试,该帐户启用了第 3 部分应用程序访问权限,并且可以通过 JavaMail 中包含的 IMAP 和 GIMAP 提供商进行连接。如果 SMTP 设置正确,那么它也能够发送邮件,我只是想添加一个层,以便在您配置新帐户时,测试 SMTP 凭据和设置以验证配置是否正确。
这里的大局是这个代码所属的项目不会只用于 GMail 帐户,它应该支持任何 IMAP/SMTP 电子邮件服务。
我尝试了多种创建会话和传输的变体,主要遵循相关问题中的示例答案:
Validate smtp server credentials using java without actually sending mail
这些答案似乎对我不起作用,因为问题是传输正在与无效凭据建立成功连接,尝试发送消息确实失败,但 MessagingException 不是AuthenticationFailedException.. 这两个相关问题中的第二个,多个评论声称有类似的问题,但没有提供解决方案。
// 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() return 字符串值与有效帐户不一致,则将抛出 AuthenticationFailedException,或者实施其他一些方法来确定凭据是否不正确。
按照 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;
}
}
我正在尝试检查是否为使用 JavaMail 库的电子邮件帐户正确输入了 SMTP 设置/凭据。我遇到的问题是无论凭据有效还是无效,连接都会成功。
这正在 GMail 帐户上进行测试,该帐户启用了第 3 部分应用程序访问权限,并且可以通过 JavaMail 中包含的 IMAP 和 GIMAP 提供商进行连接。如果 SMTP 设置正确,那么它也能够发送邮件,我只是想添加一个层,以便在您配置新帐户时,测试 SMTP 凭据和设置以验证配置是否正确。
这里的大局是这个代码所属的项目不会只用于 GMail 帐户,它应该支持任何 IMAP/SMTP 电子邮件服务。
我尝试了多种创建会话和传输的变体,主要遵循相关问题中的示例答案:
Validate smtp server credentials using java without actually sending mail
这些答案似乎对我不起作用,因为问题是传输正在与无效凭据建立成功连接,尝试发送消息确实失败,但 MessagingException 不是AuthenticationFailedException.. 这两个相关问题中的第二个,多个评论声称有类似的问题,但没有提供解决方案。
// 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() return 字符串值与有效帐户不一致,则将抛出 AuthenticationFailedException,或者实施其他一些方法来确定凭据是否不正确。
按照 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;
}
}