Java Codes To Send The Email Attachments

I have a web application where people can upload files. The filenames are put into a list. Then I loop through the list and for each file, I attach it to an email and send the email. All my print statements seem to indicate that it is processing and attaching the correct files, but when the email is received, only one of the attachments is included, but multiple times. 

For instance, if I upload file1.txt and file2.txt, when the email is received, it comes in with two file attachments (like it should) but both of them are file2.txt. 

Any ideas are welcome! 

Code:

mailProps.put("mail.smtp.host", properties.getProperty("smtpmail.hostname")); 
Session mailSession = Session.getDefaultInstance(mailProps); 
Message simpleMessage = new MimeMessage(mailSession); 

InternetAddress fromAddress = null; 
InternetAddress toAddress = null; 

fromAddress = new InternetAddress(inquiry.getContactemail()); 
toAddress = new InternetAddress(sendEmailTo); 

simpleMessage.setFrom(fromAddress); 
simpleMessage.setRecipient(RecipientType.TO, toAddress); 
simpleMessage.setSubject("my subject"); 
MimeBodyPart messagePart = new MimeBodyPart(); 
String bodyText = composeEmailBody(inquiry, request); 

messagePart.setText(bodyText); 
Multipart multipart = new MimeMultipart(); 

if (!attachmentList.isEmpty()) { 
    // Set the email attachment file 
    MimeBodyPart attachmentPart = new MimeBodyPart(); 
    for (int i = 0; i < attachmentList.size(); i++) { 
        File target = new File(properties.getProperty("file.upload.location") + attachmentList.get(i)); 
        System.out.println("target.getAbsolutePath(): " + target.getAbsolutePath()); 
        if (target.exists()) { 
            FileDataSource fileDataSource = new FileDataSource( 
                    properties.getProperty("file.upload.location") + attachmentList.get(i)) { 
                public String getContentType() { 
                    return "application/octet-stream"; 
                } 
            }; 
            System.out.println("fileDataSource.getName(): " + fileDataSource.getName()); 
            attachmentPart.setDataHandler(new DataHandler(fileDataSource)); 
            attachmentPart.setFileName(attachmentList.get(i)); 
            System.out.println("attachmentPart.getFileName(): " + attachmentPart.getFileName()); 
            multipart.addBodyPart(attachmentPart); 
        } 
    } 

simpleMessage.setContent(multipart); 
multipart.addBodyPart(messagePart); 
Transport.send(simpleMessage); 


mailProps.put("mail.smtp.host", properties.getProperty("smtpmail.hostname"));
Session mailSession = Session.getDefaultInstance(mailProps);
Message simpleMessage = new MimeMessage(mailSession);

InternetAddress fromAddress = null;
InternetAddress toAddress = null;

fromAddress = new InternetAddress(inquiry.getContactemail());
toAddress = new InternetAddress(sendEmailTo);

simpleMessage.setFrom(fromAddress);
simpleMessage.setRecipient(RecipientType.TO, toAddress);
simpleMessage.setSubject("my subject");
MimeBodyPart messagePart = new MimeBodyPart();
String bodyText = composeEmailBody(inquiry, request);

messagePart.setText(bodyText);
Multipart multipart = new MimeMultipart();

if (!attachmentList.isEmpty()) { 
 // Set the email attachment file
 MimeBodyPart attachmentPart = new MimeBodyPart();
 for (int i = 0; i < attachmentList.size(); i++) {
  File target = new File(properties.getProperty("file.upload.location") + attachmentList.get(i));
  System.out.println("target.getAbsolutePath(): " + target.getAbsolutePath());
  if (target.exists()) {
   FileDataSource fileDataSource = new FileDataSource(
     properties.getProperty("file.upload.location") + attachmentList.get(i)) {
    public String getContentType() {
     return "application/octet-stream";
    } 
   };
   System.out.println("fileDataSource.getName(): " + fileDataSource.getName());
   attachmentPart.setDataHandler(new DataHandler(fileDataSource));
   attachmentPart.setFileName(attachmentList.get(i));
   System.out.println("attachmentPart.getFileName(): " + attachmentPart.getFileName());
   multipart.addBodyPart(attachmentPart);
  }
 } 
}
simpleMessage.setContent(multipart); 
multipart.addBodyPart(messagePart);
Transport.send(simpleMessage);

}

Solutions:

You only create one MimeBodyPart but then you use it for all of the attachments. You need one MimeBodyPart per attachment. 

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.