Search code examples
javapathclasspathemail-attachmentsjakarta-mail

Javax Mail File Path for Attachment


I understand this may be an amateur question:

What's the path of any locatable folder for attaching a file as an attachment in a MimeMultiPart message?

I've followed an example to produce this:

MimeBodyPart imagePart = new MimeBodyPart();
imagePart.attachFile("logo.jpg");
imagePart.setContentID("<" + cid + ">");
imagePart.setDisposition(MimeBodyPart.INLINE);
content.addBodyPart(imagePart);

But when calling Transport.send I get file not found exception.

Basically, where can I put 'logo.png' in my project structure so it's accessible to this static method?


Solution

  • Your current code is looking relative to the working directory. If you just want to move your file then you need to place it the working directory. You can determine that by getting the canonical path.

    new File("logo.jpg").getCanonicalPath()
    

    This code returns the path where you need to place the logo.jpg.