Click here to Skip to main content
15,347,000 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been struggling with this for weeks and can't get the result I'm looking for. The email can easily send one file attachment but will not send multiple files. I need to be able to send three files. The only part that doesn't work is the multipart. All the rest works fine. Below is the code I'm using:


Java
package costfolder;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import java.time.format.DateTimeFormatter;  
import java.time.LocalDateTime;   
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message.RecipientType;
import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;

public class eMAIL_FORM extends javax.swing.JFrame {
public String emailTO;
public String emailCC;
public String emailLOGONID;
public String emailPSWD;
public String user;
public String emailDATE;
public String emailFROM;
public String emailBCC;
public static String filename2;
public static String filename3;
public class CurrentDateTimeExample1 {    
     
   DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");  
   LocalDateTime now = LocalDateTime.now();  
  
}    
    /**
     * Creates new form eMAIL_FORM
     */
    public eMAIL_FORM() {
        initComponents();
        setDefaultCloseOperation(eMAIL_FORM.DO_NOTHING_ON_CLOSE);  
    }
    public void sendEMAIL() throws UnsupportedEncodingException, IOException{
        
      
        
    user = jtxtfld_USER.getText();
    emailPSWD = new String(this.jtxtfld_PASSWD1.getPassword());
    emailTO = jtxtfld_TO.getText();
    emailCC = jtxtfld_CC.getText();
    Date date = new Date();
    emailDATE = date.toString();
    emailFROM = jtxtfld_FROM.getText();
    emailBCC = "     ";
    
    
    
   String attachFile1 = "C:\\Users\\wmelendez\\Documents\\NetBeansProjects\\PlumbPRO\\Plumb-13052021.docx";
   String attachFile2 = "C:\\Users\\wmelendez\\Documents\\NetBeansProjects\\PlumbPRO\\Plumb-13052021.xlsx";
    Properties props = new Properties();  
     Session session = Session.getInstance(props,new javax.mail.Authenticator() {  
      @Override
      protected PasswordAuthentication getPasswordAuthentication() {  
          
    return new PasswordAuthentication(user,emailPSWD);  
      }  
    });     
     String Host = "smtp.gmail.com";
         
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.host", Host);//gmail host
      props.put("mail.smtp.port", "465");//587,993
      props.put("mail.smtp.ssl.enable", "true"); 
      props.setProperty("mail.user", user);
      props.setProperty("mail.password", emailPSWD);
      props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");  
    

session.setDebug(true);

try {
    String txtMSG = jtxtarea_MESSAGE.getText();
   
    //Setup the Message Header and Session
    Message message = new MimeMessage(session);
     Multipart multipart = new MimeMultipart();
 
    message.setFrom(new InternetAddress (user));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailTO));
    message.addRecipient(RecipientType.CC, new InternetAddress(emailCC));
    message.setSubject("Report of Projects & Costs");
    message.setSentDate(new Date());
    message.setText(txtMSG);
    
    //*************************
    
     //create MimeBodyPart object and set your message content    
    
    
     
     MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setContent(message, "text/html");
    messageBodyPart.setText("This is message body");
    multipart.addBodyPart(messageBodyPart);
    
    MimeBodyPart attachPart1 = new MimeBodyPart();
    MimeBodyPart attachPart2 = new MimeBodyPart();
    MimeBodyPart attachPart3 = new MimeBodyPart();
    
    //create new MimeBodyPart object and set DataHandler object to this object    
    
    //create new MimeBodyPart object and set DataHandler object to this object    
    MimeBodyPart messageBodyPart2 = new MimeBodyPart();
    MimeBodyPart messageBodyPart3 = new MimeBodyPart();
    
    // create Multipart object and add MimeBodyPart objects to this object    
    
     multipart.addBodyPart(messageBodyPart);
    multipart.addBodyPart(attachPart3);
    multipart.addBodyPart(attachPart3);
    
    multipart.addBodyPart(messageBodyPart2);
    attachPart1.attachFile(attachFile1);
    multipart.addBodyPart(attachPart1);
    
    multipart.addBodyPart(messageBodyPart3);
    attachPart2.attachFile(attachFile2);
    multipart.addBodyPart(attachPart2);
    
   
    // set the multiplart object to the message object
    message.setContent(multipart );
   
     //Log onto GMAIL and Send the Message to recipients 
    
    Transport transport = session.getTransport("smtps");
    transport.connect(Host,465, user, emailPSWD);
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
    
    System.out.println("Sent Ok");
} catch (MessagingException e) {throw new RuntimeException(e);
    //System.out.println("Something went wrong");
   // e.printStackTrace();
}


    }


    
}


What I have tried:

I have gone through every possible tutorial and forum dealing with sending attachments with JavaMail. None work within my IDE environment. Setup for initial message is pretty easy and straight forward. The multipart somehow eludes me. Any help would be appreciated.
Posted
Updated 21-May-21 22:11pm
v2

1 solution

I have never used Javamail, but I think the problem may be in the following code:
Java
// create Multipart object and add MimeBodyPart objects to this object

 multipart.addBodyPart(messageBodyPart); // this was already added a few lines above
multipart.addBodyPart(attachPart3); // are you sure you want this twice?
multipart.addBodyPart(attachPart3);

multipart.addBodyPart(messageBodyPart2); // messageBodyPart2 is an empty MimeBodyPart
attachPart1.attachFile(attachFile1);
multipart.addBodyPart(attachPart1);

multipart.addBodyPart(messageBodyPart3); // messageBodyPart3 is an empty MimeBodyPart
attachPart2.attachFile(attachFile2);
multipart.addBodyPart(attachPart2);

I suggest you review the above to check that it is actually doing what you think it is (or should).
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900