0

I am searching a file and attaching to an email, using the code below:

try {
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse(to));
    message.setSubject("ready to dispatch");
    BodyPart messageBodyPart = new MimeBodyPart();
    String msg = "Dear Customer is ready to dispatch";
    msg += "Please use the particulars  etc. \n\n";
    msg += "Regards \n";

    messageBodyPart.setText(msg);

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    messageBodyPart = new MimeBodyPart();

    String filename = a;

    DataSource source = new FileDataSource(filename);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(filename);
    multipart.addBodyPart(messageBodyPart);


    message.setContent(multipart);
    // Send message
    Transport.send(message);
    System.out.println("Sent message successfully....");

} catch (MessagingException e) {
    throw new RuntimeException(e);
}


public static class MyFileNameFilter implements FilenameFilter {
    private final String dealer;

    MyFileNameFilter(String dealer) {
        this.dealer = dealer;
    }

    @Override
    public boolean accept(File dir, String name) {
        return name.toLowerCase().endsWith(".pdf") || name.toLowerCase().endsWith(".xls");
    }
}

I am able to attach the file 'a.pdf' and I can send the mail successfully, but I have another file 'a.xls' file also in target folder. How can attach both files in a single mail? I tried, but I was only able to send them using two different mails. I would appreciate any assistance please. Thanks in advance.

Craig
  • 2,286
  • 3
  • 24
  • 37
Kiran
  • 5
  • 3
  • I am using the same method, but from filename filter i am getting a.pdf for first iteration, next time i am gettting a.xls file – Kiran Sep 14 '15 at 06:09
  • complete your iteration and have the path for all your files you want to attach. – Garry Sep 14 '15 at 07:26

0 Answers0