我目前正在用Java开发一个邮件系统应用程序。我正在使用Java.Mail阅读和发送电子邮件。我创建了由以下人员提供的电子邮件帐户类:
public EmailAccount(String emailId, String password)
{
super();
this.emailId = emailId;
this.password = password;
this.properties = new Properties();
this.properties.put("incomiongHost", "imap.gmx.com");
this.properties.put("mail.store.protocol", "imap");
this.properties.put("mail.transport.protocol", "smtp");
this.properties.put("mail.smtp.host", "mail.gmx.com");
this.properties.put("mail.smtp.port", "587");
this.properties.put("mail.smtp.auth", "true");
this.properties.put("mail.smtp.user", emailId);
this.properties.put("mail.smtp.password", password);
this.properties.put("mail.smtp.starttls.enable", "true");
}以下方法在登录时检查不同的错误,然后登录到电子邮件:
public LoginResult login()
{
Authenticator authenticator = new Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(emailAccount.getEmailId(), emailAccount.getPassword());
}
};
try
{
Session session = Session.getInstance(emailAccount.getProperties(), authenticator);
Store store = session.getStore("imap");
store.connect(emailAccount.getProperties().getProperty("incomingHost"), emailAccount.getEmailId(),
emailAccount.getPassword());
emailAccount.setMailStore(store); // use to get mails
}
catch(NoSuchProviderException e)
{
e.printStackTrace();
return LoginResult.FAILED_BY_NETWORK;
}
catch(AuthenticationFailedException e)
{
e.printStackTrace();
return LoginResult.FAILED_BY_CREDENTIALS;
}
catch(MessagingException e)
{
e.printStackTrace();
return LoginResult.FAILED_BY_UNEXPECTED_ERROR;
}
catch(Exception e)
{
e.printStackTrace();
return LoginResult.FAILED_BY_UNEXPECTED_ERROR;
}
return LoginResult.SUCCESS;
}我正在讨论的问题是,我的代码总是显示AuthenticationFailedException。我在GMX中打开了“允许POP3和IMAP发送和接收邮件”选项,但问题仍然发生在正确的凭据上。什么地方可能出了问题?
发布于 2022-06-03 17:48:58
@Max的评论解决了这个问题。主要问题是我在将"incomingHost“添加到属性时拼写错误。EmailAccount类的正确代码是:
public EmailAccount(String emailId, String password)
{
super();
this.emailId = emailId;
this.password = password;
this.properties = new Properties();
this.properties.put("incomingHost", "imap.gmx.com");
this.properties.put("mail.store.protocol", "imap");
this.properties.put("mail.transport.protocol", "smtp");
this.properties.put("mail.smtp.host", "mail.gmx.com");
this.properties.put("mail.smtp.port", "587");
this.properties.put("mail.smtp.auth", "true");
this.properties.put("mail.smtp.user", emailId);
this.properties.put("mail.smtp.password", password);
this.properties.put("mail.smtp.starttls.enable", "true");
}https://stackoverflow.com/questions/72489564
复制相似问题