当尝试使用spring和angular发送邮件时出现此错误:
错误11175-[io-8090-exec-10] oaccC [。[。[/]。[dispatcherServlet]:路径为[]的上下文中servlet [dispatcherServlet]的Servlet.service()引发了异常[Request处理失败;嵌套的异常是java.lang.RuntimeException:javax.mail.AuthenticationFailedException],其根本原因是 javax.mail.AuthenticationFailedException:空
我已经在pom.xml文件中导入了javax.mail:
<dependency>
<groupId> javax.mail </groupId>
<artifactId> mail </artifactId>
<version> 1.4 </version>
</dependency>
这是我的代码:
public static boolean sendMail(MailMessageRequest messageRequest) {
final String username = "----";
final String password = "----";
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");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username, "TestingApp"));
message.setSender(new InternetAddress(username, "TestingApp"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(messageRequest.getSentTo()));
message.setSubject(messageRequest.getSubject());
message.setContent(messageRequest.getBody(), "text/html");
message.setReplyTo(messageRequest.getReplayTo());
Transport.send(message);
log.info("A mail has been sent to " + messageRequest.getSentTo() + " at --> " + new Date());
} catch (MessagingException | UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
return true;
}
所以我不明白是什么问题。