This discussion is archived
5 Replies Latest reply: Mar 7, 2013 4:07 AM by PhHein RSS

Some PDF attachments are corrupted when emailed using javamail

843834 Newbie
Currently Being Moderated
I have a java class that takes a file name, 'To' email, and 'From' email as parameters, and then emails the named file to the specified address as a Multipart email.

The problem that I am facing is that sometimes the PDF cannot be opened when it arrives because it is corrupt. I know that prior to sending, the PDF opens without issue, so the problem is in the code somewhere.

There are a couple of oddities here:
1) PDFs that get corrupted and PDFs that do not get corrupted are generated from the same source (Oracle Report), so I believe that rules out a problem with malformed emails?
2) If I compare the PDF before it gets sent to the PDF that arrives in my inbox using a hex editor (XVI32), the files are identical except that an extra character is getting added to the corrupt PDFs at the end of each line - hex '0A', which corresponds to ASCII linefeed. This indicates to me that maybe there is a problem with the way that the file is getting encoded when it is read. If I remove all of the 0A characters, the file opens correctly.

I believe that the code I am using is set up correctly, as I have looked at countless examples of this online to try to find my problem. Hopefully someone can point me in the right direction. Code is included below.

Thanks,
Nick
import java.util.*;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;

public class test {

    public static void main(String args[]) {
        System.out.println("entered TEST email class");
        if (args.length < 3) {
            System.out.println("Invalid number of arguments. Syntax is 'java test <fileName> <fromEmail> <toEmail>'");
        } else {

            System.out.println("Correct number of arguments.");
            send(args[0], args[1], args[2]);
        }
    }

    public static void send(String fileName, String fromEmail, String toEmail) {
        System.out.println("Made it to SEND method");
        try {
            Properties props = new Properties();
            props.put("mail.smtp.host", "email.host");
            Session session = Session.getDefaultInstance(props, null);
            MimeMessage msg = new MimeMessage(session);
            //set from/to email addresses
            System.out.println("Setting email addresses...");
            msg.setFrom(new InternetAddress(fromEmail));
            msg.setRecipients(javax.mail.Message.RecipientType.TO, toEmail);
            //set email subject
            System.out.println("Setting subject...");
            msg.setSubject("email from TEST class");
            //define a body part for the email text and add text to it
            System.out.println("Setting body...");
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent("This is the email body. Here is some more body.", "text/html");
            //add code for attachment here!!
            System.out.println("Setting attachment...");
            MimeBodyPart attachFilePart = new MimeBodyPart();
            FileDataSource fds =
                    new FileDataSource(fileName);
            attachFilePart.setDataHandler(new DataHandler(fds));
            attachFilePart.setFileName(fds.getName());
            //define a multipart, and add the text body part and the attachment body part to it
            System.out.println("Building message...");
            Multipart mp = new MimeMultipart();
            mp.addBodyPart(messageBodyPart);
            mp.addBodyPart(attachFilePart);
            //add the multipart to the message
            msg.setContent(mp);
            //send the email
            System.out.println("Sending message...");
            Transport.send(msg);
            System.out.println("Email has been sent!");
        } catch (Exception e) {
            System.out.println("Error in test.send method: " + e.getMessage());
        }
    }
}
Edited by: nbacon on Sep 2, 2010 12:12 PM