public static void main(String[] args) throws GeneralSecurityException {
String emailFrom = from;
Authenticator authenticator = new Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("邮箱", "密码");
}
};
Properties properties = new Properties();
properties.setProperty("mail.smtp.host", "smtp.office365.com");
properties.setProperty("mail.smtp.port", "587");
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.debug", "true");
properties.setProperty("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance( properties, authenticator );
try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailFrom));
InternetAddress[] to = {new InternetAddress("邮箱")};
message.setRecipients(Message.RecipientType.TO, to);
message.setSubject("PLACE_SUBJECT_HERE");
message.setText("YOUR_MESSAGE_HERE");
Transport.send(message);
}
catch (MessagingException exception)
{
exception.printStackTrace();
}
}
报错:
javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful
账号和密码又是对的。求解