Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

MimeBodyPart content-type does not keep the value.

3349471Nov 18 2016 — edited Nov 24 2016

Hi all,

I have an issue sending emails with .zip file attached. When I run the code with html or text files, everything works properly. However, when I send those files in a .zip file, the mail is not sent. I check the set up and I saw that the content-type field is not saved after the change. This is the code:

// A MimeMessage is created by defect.

MimeMessage message = new MimeMessage(session);

// The sender is added.

message.setFrom(new InternetAddress(from));

// The receiver/s is/are objects is/are created..       

String[] receiverString = to.split(",");

Address [] receivers = new Address [receiverString.length];

int j = 0;

while (j<receiverString.length) {

   

    receivers[j] = new InternetAddress(receiverString[j]);                  

    j++;

   

}

// The receiver/s is/are added.

message.addRecipients(Message.RecipientType.TO, receivers);

// The subject is added.

message.setSubject(subject);

// A new multiPart message is created joining both previous parts: text and attachment.

MimeMultipart multiPart = new MimeMultipart();

// A new message object is created. It will have two parts: the first one with the text message and the second one with the attachments.

MimeBodyPart messageText = new MimeBodyPart();

System.out.println("EmailSender - sendEmail: messageText content-type: " + messageText.getContentType());

messageText.setText(text);

multiPart.addBodyPart(messageText);

System.out.println("****************************************************************");

System.out.println("****************************************************************");

MimeBodyPart attachment = null;

System.out.println("EmailSender - sendEmail: multiPart.getCount() before: " + multiPart.getCount());

for (int co=0; co<outputs.length; co++) {

   

    if (outputs[co] != null) {

   

        attachment = new MimeBodyPart();

        System.out.println("EmailSender - sendEmail: attachment.getContentType() before: " + attachment.getContentType());

        attachment.setHeader("Content-Type", "application/x-zip-compressed");

        System.out.println("EmailSender - sendEmail: attachment.getContentType() after: " + attachment.getContentType());

        DataSource source = new FileDataSource(outputs[co].getAbsolutePath());

        attachment.setDataHandler(new DataHandler(source));

        attachment.setFileName(outputs[co].getName());           

        multiPart.addBodyPart(attachment);

   

    }

       

}

System.out.println("EmailSender - sendEmail: multiPart.getCount() after: " + multiPart.getCount());

System.out.println("****************************************************************");

System.out.println("****************************************************************");

for (int co=0; co<multiPart.getCount(); co++) {

   

    System.out.println("EmailSender - sendEmail: multiPart.getBodyPart("+co+").getContentType(): " + multiPart.getBodyPart(co).getContentType());

   

}

// The multipart message is inserted to the MimeMessage default message.

message.setContent(multiPart);           

message.saveChanges();

Output:

EmailSender - sendEmail: messageText content-type: text/plain

****************************************************************

****************************************************************

EmailSender - sendEmail: multiPart.getCount() before: 1

EmailSender - sendEmail: attachment.getContentType() before: text/plain

EmailSender - sendEmail: attachment.getContentType() after: application/x-zip-compressed

EmailSender - sendEmail: multiPart.getCount() after: 2

****************************************************************

****************************************************************

EmailSender - sendEmail: multiPart.getBodyPart(0).getContentType(): text/plain

EmailSender - sendEmail: multiPart.getBodyPart(1).getContentType(): text/plain

I would like to point that I tried to use application/zip and multipart/x-zip with the same results. I tried to find out any clue about it and I read that savedChanges() should be applied to MimeMessage object to keep the changes. According with the results, it seems that it does not work. I am wondering if it can be the reason of this problem. If anyone have any suggestion about how to solve these issues (mail with .zip file and content-type not saved), I will appreciate your help.

Thanks in advance.

This post has been answered by Bill Shannon-Oracle on Nov 18 2016
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 22 2016
Added on Nov 18 2016
2 comments
1,742 views