确认邮箱账号和密码是否正确。
确认邮箱服务商是否开启了IMAP/POP3服务。
检查代码中是否正确配置了SMTP/IMAP/POP3的相关参数。
参考代码:
使用JavaMail发送邮件:
String host = "smtp.gmail.com";
String port = "587";
String username = "[email protected]";
String password = "your-email-password";
Properties properties = new Properties();
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");
Session session = Session.getInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse("[email protected]")
);
message.setSubject("Testing JavaMail API");
message.setText("Dear Recipient," +
"\n\nThis is a test email sent using JavaMail API. " +
"Please do not reply to this email." +
"\n\nThank you." +
"\n\nBest regards," +
"\nYour Name");
Transport.send(message);
System.out.println("Email sent successfully.");
} catch (MessagingException e) {
e.printStackTrace();
}
使用JavaMail接收邮件:
String host = "imap.gmail.com";
String port = "993";
String username = "[email protected]";
String password = "your-email-password";
Properties properties = new Properties();
properties.put("mail.imap.host", host);
properties.put("mail.imap.port", port);
properties.put("mail.imap.starttls.enable", "true");
properties.put("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Session session = Session.getInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Store store =