【发布时间】:2018-05-29 07:56:13
【问题描述】:
我正在发送带有内嵌图像的邮件,但问题是它给了我一个错误 FileNotFound 我正在使用正确的路径但仍然抛出错误任何人都可以告诉我我错在哪里?
代码
public void forgotPasswordMail(String email,String token)
{
String to = email;
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setSubject("Reset Password");
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
MimeMultipart multipart = new MimeMultipart("related");
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = //"<img src=\"cid:image\"> " +
"<html><head><style>h1 {background-color: #FFF100;padding: 15px; text-indent: 40px;} " +
"p {text-indent: 60px;}</style></head><body><h1>Forgot password request</h1> " +
"<p> Please click on the following link to set new password</p>" +
"<p>" + frontendUrl+"resetForgottonPassword/"+token+"</p></div></body></html>";
messageBodyPart.setContent(htmlText, "text/html");
// add it
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource(
"/home/tahir/sportsacademy-backend/assets/Logo.png");
messageBodyPart.setFileName("logo.png");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<image>");}
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
而且文件也有读写权限
文件位置
【问题讨论】:
-
FileNotFoundException有什么消息?可能是您的应用程序用户无权访问它。 -
这是否适用于网络上下文环境?
标签: java linux spring-boot jakarta-mail filenotfoundexception