JavaMail SMTP凭据验证,无需实际发送电子邮件

时间:2010-04-21 08:58:00

标签: java smtp javamail

是否可以在不发送电子邮件或连接到POP / IMAP的情况下检查用户SMTP服务器凭据。 我尝试编写的一些代码失败了。 你能找到那里缺少的东西。

不要担心电子邮件/密码。我知道它就在那里。

注意:如果您正在尝试代码。在提供正确的凭证时,案例1应该通过。 如果失败,则有人更改了密码。您应该使用其他一些电子邮件地址。



import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;

public class EmailTest {

 public static void main(String[] args) {
  EmailHelper eh = new EmailHelper();

  /* GMail Setting for SMTP using STARTTLS */
  String name = "AAA";
  String email = "mymasterpainter@gmail.com";
  String smtpHost = "smtp.gmail.com";
  String serverPort = "587";
  String requireAuth = "true";
  String dontuseAuth = "false";
  String userName = email; // same as username for GMAIL
  String password = "zaq12wsx";
  String incorrectPassword = "someRandomPassword";
  String enableSTARTTLS = "true";
  String dontenableSTARTTLS = "false";

  try {
   /* only valid case */
   eh.sendMail(name, email, smtpHost, serverPort, requireAuth,
     userName, password, enableSTARTTLS);
   System.out.println("Case 1 Passed");

   /* should fail since starttls is required for GMAIL. */
   eh.sendMail(name, email, smtpHost, serverPort, requireAuth,
     userName, password, dontenableSTARTTLS);
   System.out.println("Case 2 Passed");

   /* should fail since GMAIL requires authentication */
   eh.sendMail(name, email, smtpHost, serverPort, dontuseAuth, "", "",
     dontenableSTARTTLS);
   System.out.println("Case 3 Passed");

   /* should fail. password is incorrect and starttls is not enabled */
   eh.sendMail(name, email, smtpHost, serverPort, requireAuth,
     userName, incorrectPassword, dontenableSTARTTLS);
   System.out.println("Case 4 Passed");
  } catch (MessagingException e) {
   e.printStackTrace();
  }
 }

}

class EmailHelper {

 private Properties properties = null;
 private Authenticator authenticator = null;
 private Session session = null;

 public void sendMail(String name, String email, String smtpHost,
   String serverPort, String requireAuth, String userName,
   String password, String enableSTARTTLS) throws MessagingException {
  properties = System.getProperties();
  properties.put("mail.smtp.host", smtpHost);
  properties.put("mail.smtp.port", serverPort);
  properties.put("mail.smtp.starttls.enable", enableSTARTTLS);
  properties.put("mail.smtp.auth", requireAuth);
  properties.put("mail.smtp.timeout", 20000);

  authenticator = new SMTPAuthenticator(userName, password);

  session = Session.getInstance(properties, authenticator);

  // session.setDebug(true);

  Transport tr = session.getTransport("smtp");
  tr.connect();
  /*
   * do I need more than just connect? Since when i try to send email with
   * incorrect credentials it fails to do so. But I want to check
   * credentials without sending an email. Assume that POP3/IMAP username
   * is not same as the SMTP username, since that might be one of the
   * cases
   */
 }
}

class SMTPAuthenticator extends Authenticator {

 private String userName = null;
 private String password = null;

 public SMTPAuthenticator(String userName, String password) {
  this.userName = userName;
  this.password = password;

 }

 @Override
 public PasswordAuthentication getPasswordAuthentication() {
  return new PasswordAuthentication(userName, password);
 }
}

1 个答案:

答案 0 :(得分:2)

由于SMTP协议本身,您遇到此问题。身份验证在协议中不是必需的。当你调用方法" connect"你至少会做一个" EHLO"返回支持的扩展名的命令。身份验证是RFC中定义的扩展。 JavaMail将尝试做一个" AUTH"命令如果服务器说它支持,并且您提供了凭据。事情是身份验证扩展信息只有在您完成了" STARTTLS" (这也是一个扩展),因为在明确的通道上使用例如PLAIN认证是不安全的。这就是JavaMail做第二个EHLO"在" STARTTLS"更新支持的扩展程序。

所以对你来说一个简单的解决方案是始终把" mail.smtp.starttls.enable"为真。在JavaMail中(至少1.4.5),这意味着" STARTTLS"如果服务器说它支持,则将发送命令(否则不会),然后您将获得更新的扩展,JavaMail可以执行" AUTH"如果需要的话。