我是 Java Mail 的新手。当我执行下面的代码时,我遇到了一个异常
javax.mail.MessagingException:无法向 SMTP 主机发送命令
public void sendMessage(EmailMessage emailMessage) throws MessagingException {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
System.out.println(emailMessage.getFromAddress());
Optional<EmailId> emailIdOptional = emailIdRepository.findByEmailId(emailMessage.getFromAddress());
System.out.println(emailIdOptional.get().getEmailId());
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(emailIdOptional.get().getEmailId(), emailIdOptional.get().getPassword());
}
});
javax.mail.Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(emailMessage.getFromAddress(), false));
msg.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(emailMessage.getToAddress()));
msg.setSubject(emailMessage.getSubject());
msg.setContent(emailMessage.getMessage(), "text/html");
msg.setSentDate(new Date());
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(emailMessage.getMessage(), "text/html");
Transport.send(msg);
}
任何人都请帮我解决这个问题。
杨魅力
牛魔王的故事
相关分类