Some PDF attachments are corrupted when emailed using javamail

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

Normally pdf files are text, with any binary data represented as encoded text. If your pdf files
include raw binary data, JavaMail can be confused. JavaMail will try to guess what encoding
is appropriate for your data. If the data is mostly text, JavaMail will use quoted-printable encoding,
and will canonicalize the text lines to end with CRLF (0x0D, 0x0A). A 0x0D in the binary will look
like an end of line and will turn into 0x0D, 0x0A.
You can force JavaMail to use base64 encoding for your data, which will preserve the binary
data exactly, by using
attachFilePart.setHeader("Content-Transfer-Encoding", "base64");
after setting the content for the part.
Alternatively, you could figure out how to generate pdf files that don't include raw binary data.

Similar Messages

  • PDF attachments are changed when emailed

    I have a user that whenever she creates or prints a PDF (Adobe 8 Professional) and then tries to email it within Adobe (going to file -> attach to an email), it converts to either a winmail.dat, .eml or a .mht file when the person receives it. It only does this when she sends to someone not using outlook. If she attaches the pdf outside of Adobe, then it works. Rich-text is turned off in Outlook and Adobe is patched and up to date. Any suggestions???? This is driving me crazy!!!!!!!!!

    Have you tried changing the message format in Outlook to HTML?

  • PDF attachment are corrupted via email.

    When I open a PDF attachment via my emails it's corrupted with different colors over the page and I can't read it. Opening the same document from my laptop has no problem. I have also had the attachment resent. My iPad never had this problem until recently.

    Skydiver119 makes a very good point. The built in PDF reader and the iBooks app will not read fillable form fields in a PDF, so you need a third party app for those types of PDF files. I use Adobe Reader for PDf files. It is free, it allows for annotation, you can create folders, highlighting, form fields, adding a signature, renaming, etc. It's a great app and certainly worth a look.

  • Why are PDF attachments are showing up on the body of emails, instead of as actual attachments?

    PDF attachments are showing up in the body of my emails rather than as attachments. I need to be able to open them in other programs, eg notability, mad can't do that if they're not real attachments. On my other devices they're showing up at regular attachments.

    When a PDF that has only one page is attached to an email the Mail app will display the conte tents of the PDF in the body of the email. But the PDF is still an attachment. Tap on the PDF to open in iBooks or forward it to your computer to open it there. Again, the one page PDF will display the contents of the single page but the PDF is still an attachment and can be used like any other attachment.
    When a PDF that has more than one page is attached to an email the Mail app only displays the icon for an attachment.

  • Adobe Acrobat V9 Professional crashes or causes PDFs to be corrupted when using Dragon software

    Adobe Acrobat V9 Professional crashes or causes PDFs to be corrupted when using Dragon software. I use Nuance Dragon NaturallySpeaking 11.5 to assist me when I add comments to a PDF. I also use it when making additions or changes to text.  When I use the two software programs together, Acrobat constantly crashes.  If it doesn't crash, an error occurs trying to save the file.  I entered 988 comments into a 158-pg PDF, only to find the file was corrupted and couldn't be printed. I also could not see the list of comments (click Show Comments). 
    These problems do not occur if I don't use both products simultaneously.

    Never overwrite a clip that is used in Pr.  Give the
    modified/corrected/updated clip a new name and import that into your Pr
    project.  Then Alt+Drag the new clip on top of the old clip in the
    timeline to replace the clip instance.
    If import-then-drag isn't your style, then right-clicking the original
    clip in the Project panel and selecting Replace Footage will accomplish
    the same thing.
    This important rule applies in Encore as well.
    -Jeff

  • Lion Mail PDF attachments occasionally corrupting

    Since upgrading to Lion I've noticed that some PDF attachments won't open in Preview - I get the following:
    =====
    The file “1234.pdf” could not be opened.
    It may be damaged or use a file format that Preview doesn’t recognize
    =====
    This continues to happen even after I drag the attachment to the desktop or rebuild the IMAP inbox.
    If I go to the webmail for the email provider, open the mail and download the attchment it is fine - so something in Lion (never had this before in Snow Leopard) seems to be corrupting it.
    Anyone able to shed some light on that's going on here?
    Thanks
    Robert

    So far I'm seeing this with PDFs. In one email, I had two versions of the same PDF: one was the original, and the other had been highlighted and anotated (probably in Preview). The original opens, and the other gives me problems.
    I've used the highlighting feature heavily in Preview, but I've found that it occasionally corrupts the PDF, such that if you copy and paste text (not necessarily the highlighted text), it come out all garbled. I'm wondering if Lion does a better job identifying damaged PDF files (damaged by previous OS versions, perhaps). Haven't been able to look into this very much, but I'm eager to find out what the problem is.

  • ITunesSetup says some install files are corrupt, iPod wont be read by pc..

    My ipod and itune were working fine earlier today and before, but whenever i try to update to the latest verion the install says some install files are corrupt, and now my itunes suddenly no longer reads my ipod... This is like the 20th time this ipod not being read by pc problem has occurred and it's really making me never trust apple with pcs again.

    djszapi wrote:1. "On my home partition I only found that viminfo got corrupted twice" Sometimes, it occurs by me too, I delete this file then, but it's a rare situation
    2. "I ran the system rescue cd and fsck'ed both partitions" -> Did you get any direct filesystem error message from the system?
    3. What happens if you try to delete the corrupted files and restart ? Does it become corrupted too every time?
    4."X sometimes won't start, I get a black screen and I need to power down by holding the power button." -> Which driver/kernel/xorg version do you use ? I've experienced such a situation with the nvidia-185.18.31 driver.
    Thank you for your quick response.
    1. Is it safe to delete the corrupted files in /var/log? I already deleted viminfo after it got corrupted the first time, but it happened again.
    2. In the rescue cd, fsck quickly said "it's ok" without checking. Is there a way to force it to check? The man page doesn't say. The longer check I get sometimes when booting ("hasn't been checked in the last X mounts yadda yadda") shows only "fixed write time in the future" from time to time.
    3. The Xorg.0.log particularly gets corrupted after the hard restart that I issue when X fails to start and a black screen locks up the pc.
    4. xf86-video-ati is 6.12.2-2, kernel26 is 2.6.30.4-1. xorg-server: 1.6.3-2. The black screen issue will be (I hope) solved in the other thread I made (link in first post), but I have the suspicion that both problems are very closely related (one is the cause for the other, or some sort of vicious circle.)

  • I have to backup of Gmail with Thunderbird Portable, so I could free some space on server - but when I use IMAP (to keep labels) after deletin on server...

    I have to backup of Gmail with Thunderbird Portable, so I could free some space on server - but when I use IMAP (to keep labels) after deletin on server...
    ...my post also deletes on Thunderbird. Please make an option to prevent it, so deleting on a server doesn't effect mail in the Thunderbird. I would be so happy to have it...
    David

    First Thunderbird portable is supported by its author at portableapps.com.
    Secondly you are describing exactly how the IMAP email standard works and Thunderbird has no control over that.
    Thunderbird has Local Folders to move messages that you want to keep locally. Maybe the portable version does too.

  • Mac Mail - IMAP setup - Large attachments are corrupt

    Mac Mail - IMAP setup - Large attachments are corrupt
    Attachments open fine in webmail
    Mailbox rebuild does not help
    Appears files over 500k are affected
    Multiple users affected
    Backend is GroupWise 7
    So, I see several posts on this topic, with no solutions.... Is this a known issue according to Apple? If so, can some point me to the official release notes/ bug report??

    I have the same problem. When I look at the message with Entourage, I found out that while under IMAP the attachment is not fully downloaded from the exchange server. Entourage gives you the choice to download this file. When I select, the file comes down and opens OK. I heard that some clients have a max size download setting for IMAP but Mac Mail does not. Anyone know how to get around this?

  • Some installation files are corrupt. Please download a fresh copy and retry

    Hello,
    I have installed BusinessObjects XI 4 SP02 on server 2008 R2. BO Client tools with sp02 and Patch 8 is also installed.
    When tried to install BO Server patch 8 (ENTERPRISE02P_8-10009484) I get this error;
    "Some installation files are corrupted. Please download a fresh copy and retry the installation."
    while unzipping, WinRAR window throws below message in red;
    CRC failed in dunit
    \crystalreports.boe.clientactions.java.webpath_bundle-4.0-core-64\content.cab
    I have tried twice with fresh copy downloaded on same server and still can't pass this error.
    Loading latest patch such as patch 10 is last option since all client machines here are on patch 8.
    Has anyone seen this behavior?

    Hi,
    I'm installing an updated version of Dashboard Design (SP04 Patch 0 - includes FP3). However, during the extraction of the installation file, I get the following message :
    "Some installation files are corrupt.
    Please download a fresh copy and retry the installation."
    I tried to download the application again from SAP Service MarketPlace but the same message rises during installation.
    The downloaded file is XCELSIUSENT04_0-20006661.exe
    Did anyone manage to correclty install this version ?
    Thanks for your feedback.
    Hecham

  • CRVS2010 Beta- Some Installation files are corrupt

    Hello,
    I am trying to download the CRVS2010 beta.  It downloads and when I run the cr4vs2010.exe I get this error:
    "Some installation files are corrupt.
    Please download a fresh copy and retry the installation"
    I have tried redownloading multiple times with different browsers to no avail.  I need this downloaded ASAP as we have multiple crystal reports that were ported over from VS 2008.
    Please Help.

    Hi Burns,
    I just downloaded from the US link and ran the installer and it worked for me. It extracted all of the files and it appears the setup engine is almost the last file it extracts.
    I believe this is a permission issue. Does it get as far to create this folder:
    C:\Program Files (x86)\SAP BusinessObjects\InstallData\logs
    or any \SAP BusinessObjects folders?
    Try adding the EXE's to the trusted list in your Anti-virus software and/or firewall and or anywhere else Windows may be blocking executables.
    Thank you
    Don

  • I used to be able to send document as a PDF or Word Document via email using the toolbar.

    I used to be able to send Pages documents as a PDF or Word Document via email using the Pages toolbar (not the iWork feature).  Lately, it hasn't been working at all.  No email pops up; just nothing.  Is anyone else having this problem?  Any solutions?  I'm running Pages '09 version 4.1.

    This Apple Express Helper is a dumb one.
    File names with # and $ are perfectly treated by the share to Mail feature.
    What is rejected is the slash (/) and it's perfectly normal.
    Since the delivery of the first version of OS X, slash is an illegal character in file names.
    Apple thought that it would be fair to build a piece of code taking care of such chars because they were valid ones in the older operating systems.
    But they expected that users will drop these chars. Alas they didn't and users like you are continuing to play with matches.
    At last, OS X 10.7 removed the extraneous code and now, hosanna, iWork refuse to export files whose name contain the infamous slash.
    Yvan KOENIG (VALLAURIS, France) lundi 9 avril 2012
    iMac 21”5, i7, 2.8 GHz, 12 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.3
    My Box account  is : http://www.box.com/s/00qnssoyeq2xvc22ra4k

  • Some external Users are getting Duplicate Emails from our domain

    Some external Users are getting Duplicate Emails from our domain. I have Exchange 2013 and is properly configured. A user reported me that, I sent one email to the other domain's user, and it was delivered twice after 1 or 2 hours.  I checked the Message
    track log on exchange servers and email was resubmitted and sent from the edge server after 2 hours.
    I have no idea why this is happening? can you please help me on this issue.

    Hi Aleem,
    Maybe some hidden rules cause this, I suggest use MFCMapi tool to double check whether there is any hidden rules exist.
    Thanks
    Mavis
    Mavis Huang
    TechNet Community Support

  • CRVS2010 Beta -CRforNET40_x86 - I get 'Some installation files are corrupt'

    Hi,
    I just downloaded CRforNET40_x86 from the "Blogs" page and got the 'Some installation files are corrupt' error message on trying to run/extract the files.
    The message also asked me to download the file again but it would seem pointless as I didn't get any download errors so I'm assuming that the file may be corrupted. Would you please have it checked so that I can try downloading it again.
    Thanks,
         Michael
    Edited by: turbo56 on Aug 4, 2010 6:19 PM

    Sorry Ludek for not specifying the link.
    I downloaded the file from "/people/blair.wheadon/blog/2010/07/29/crystal-reports-for-visual-studio-2010-beta-2-and-runtimes-now-availableCystlR%2528SAPWeblogs%253ACrystal+Reports%2529".
    Anyhow, I decided to download it again and this time it did run properly/extract and install. Don't know why it didn't work/run the first time but the main thing is that it worked.
    Thanks for your prompt response.

  • "Some installation files are corrupt." Trouble Upgrading iTunes.

    Please help. I have been trying to upgrade iTunes from the apple site, but everytime I try I get this error message, "Some installation files are corrupt." If anyone has any advice, it would be greatly appreciated. Thank you.
      Windows XP  

    I also had this problem and solved it by deleting the downloaded file, turning off the firewall and Cybersitter (content filter) and downloading itunes setup again. It then worked perfectly. I therefore recommend you disable any firewall or filtering software you may have (temporarily of course) and try again. Good luck.

Maybe you are looking for

  • How to login into another BO from my BO

    Hi My requirement is to login into another BO from a link in my BO without asking user to enter uername and password. Can you pls help me to know how this can be done. Thanks Regards Amit

  • Oracle Recovery Error using Wizard

    Recovery Manager: Release 8.1.6.0.0 - Production RMAN-06005: connected to target database: VTST (DBID=4051728604) RMAN-06009: using target database controlfile instead of recovery catalog RMAN> run { 2> allocate channel DefaultChannel type disk forma

  • How to get a loop run for 1 instance every time a button is pressed

    I need to get a loop run only once everytime a button is pressed. Please help me.

  • Get current logged info

    I've created in my application a form that everyone authorized can use to post information on my web site. But i want to record in the DB the reference of the user logged who has posted information. What is the command to do that ? In addition to tha

  • How to stop the registration screen

    I have had to reinstall my copy of FCE-HD. Since I had already registered when I first installed it, I don't want to send a second registration for the same product in if I don't have to. Is there a way to stop the "reminder to register" screen when