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.