Skip to content

Instantly share code, notes, and snippets.

@kashua14
Created August 31, 2021 07:16
Show Gist options
  • Save kashua14/a5eaafd661b6416c7eba8fc9e088b685 to your computer and use it in GitHub Desktop.
Save kashua14/a5eaafd661b6416c7eba8fc9e088b685 to your computer and use it in GitHub Desktop.
How to login into an email using the Java Mail API.
public boolean checkEmailCrendentails(String hostName, String email, String password) {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", hostName);
props.put("mail.smtp.port", "25");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(email, password);
}
});
try {
Store store = session.getStore("pop3s");
store.connect(hostName, email, password);
return store.isConnected();
} catch (NoSuchProviderException e) {
e.printStackTrace();
return false;
}
catch (AuthenticationFailedException e) {
e.printStackTrace();
return false;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment