2

I have implemented java mail with org.springframework.mail.javamail but while downloading from gmail it not downloading with file extension

below is my java code


private JavaMailSender mailSender;

MimeMessagePreparator preparator = new MimeMessagePreparator() {
                    public void prepare(MimeMessage mimeMessage) throws Exception {

                        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
                        message.setTo("abc@gmail.com");
                        message.setFrom("sdf@gmail.com", "hello");
                        message.setText("text", true);
                        message.setSubject("subject");
                        message.addAttachment("name", file);
                    }
                };
                try {
                    mailSender.send(preparator);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

as we can see in first image it showing as excel file but when we download it, its downloading as file please see the second image please help me if any thing missing or suggest any changes required

Edited

I am Creating file as File outputFl = new File("filepath/finaleName.xls");

var a
  • 39
  • 9

1 Answers1

0

You are missing the correct procedure to attach a file to your email body (I guess).

You should create email attachment like below:

MimeBodyPart attachPart = new MimeBodyPart();
String attachFile = "filepath/finaleName.xls";
attachPart.attachFile(attachFile);
multipart.addBodyPart(attachPart);

For a full working example, you can search on Google and will find a lot of working examples relevant to your requirement. Here are few examples. Hope it helps.

Ajay Kumar
  • 2,906
  • 3
  • 23
  • 46