gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:41:32 24 4
gpt4 key购买 nike

有没有一种方法可以在不发送电子邮件或连接到 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);
}
}

最佳答案

您遇到此问题是因为 SMTP 协议(protocol)本身。身份验证在协议(protocol)中不是强制性的。当您调用“connect”方法时,您至少会执行一个“EHLO”命令,该命令返回支持的扩展。身份验证是 RFC 中定义的扩展。 JavaMail 将尝试执行“AUTH”命令如果服务器说它支持并且您提供了凭据。事情是身份验证扩展信息只能在您完成“STARTTLS”(这也是一个扩展)之后发送,因为在清晰的 channel 上使用例如 PLAIN 身份验证是不安全的。这就是为什么 JavaMail 在“STARTTLS”之后执行第二个“EHLO”以更新支持的扩展。

因此,解决您问题的一个简单方法是始终将“mail.smtp.starttls.enable”设置为 true。在 JavaMail(至少 1.4.5)中,这意味着如果服务器说它支持(否则不支持),将发送“STARTTLS”命令,然后您将获得更新的扩展名,如果需要,JavaMail 可能会执行“AUTH”。

关于JavaMail SMTP 凭据验证,无需实际发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2681481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com