【问题标题】:IOException while sending message - File Not Found发送消息时出现 IOException - 找不到文件
【发布时间】: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


【解决方案1】:

有多种原因可能导致这种情况,但我猜问题是您正在文件系统位置中查找文件,并且您的程序在根文件夹不同的 Web 上下文中运行。如果您正在制作 Web 应用程序,即使您在 /home 文件夹中进行开发,它也会在 servlet 容器提供的 Web 上下文中运行,或者使用嵌入的 servlet 容器从您的 IDE 中运行)。实际的文件夹结构有点不同。这是因为在共享的 Web 应用程序环境中存在封装,并且每个应用程序只能访问属于它的文件(否则您可能能够修改不属于您的文件)。

因此,为了检查这一点,如果您在 Web 环境中运行,您可以通过在您的 servlet 上下文中调用 getRealPath("/") 方法来检查实际文件夹,该方法会向您显示实际的文件系统路径。但基本上你需要在 web 应用程序上下文中使用它们的相对路径访问资源,而不是在文件系统中。

【讨论】:

    【解决方案2】:

    可能是文件路径不正确,因为您使用的是主文件夹。你检查过这个帖子:Accessing "~" (user home) from Java in Linux 吗?

    还有一件事;你结束 try 块,然后添加一些代码,然后开始 catch 块。这不起作用,因为 catch 必须在 try 之后立即开始(但可能是复制错误)。

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 2021-08-31
      • 2015-01-27
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 2020-12-05
      • 2012-10-12
      • 2018-01-17
      相关资源
      最近更新 更多