1

I was stuck to a problem exactly same as this and with the solution posted i was able to solve my issue. But now the problem is, when the attachment is received, there is no name for it. In my method, i have asked for receiver's email id, subject , content, filename and byte[] for the File. There is no problem in format of the file i am passing but problem is with the name. The recipient gets "noname" as file name. How do we specify the filename of our choice. The filename which i am passing as parameter doesnt gets reflected. Please suggest.

The code which i am using is

File file = new File("D:/my docs/Jetty.pdf");
int len1 = (int)(file.length());
FileInputStream fis1 = null;
try {
    fis1 = new FileInputStream(file);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
byte buf1[] = new byte[len1];
try {
    fis1.read(buf1);
    EmailServiceClient.sendEmailWithAttachment("[email protected]", "[email protected]", "Hi", "PFA", "Jetty.pdf", buf1);

    System.out.println("SENT");
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

My email service Implementation for this goes here

public void sendEmailWithAttachment(String emailIdTo, String emailIdFrom, String subject, String content,
    final String fileName, byte[] file) {
MimeMessage message = mailSender.createMimeMessage();
try {
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setTo(emailIdTo);
    helper.setFrom(emailIdFrom);
    helper.setSubject(subject);
    helper.setText(content, true);
    helper.addInline("attachment", new ByteArrayResource(file) {
        @Override
        public String getFilename() {
            return fileName;
        }
    });
    mailSender.send(message);
} catch (MessagingException e) {
    throw new MailParseException(e);
}}

Please someone help in figuring this out

1 Answer 1

1

From the Spring Documentation I can say that the inlined elements don't have special names besides the contentId. Maybe you want to add an attachment instead using the addAttachment method? Then you can have a name for your file.

http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/mail/javamail/MimeMessageHelper.html

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply @toomasr but my previous problem was the link which i have referred. My problem was to add attachments in the email without creating physical file and the solution there did the trick. but now the email attachment received does not have any name and I dnt know how to fix that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.