Javamail attachment unintentionally in email body

(For the quick problem and solution, skip to the bottom)

In a recent project I wanted to send pdf reports I generated to clients using javamail.  I had never done so, so I began working on it, getting it working incrementally.  First I connected to gmail.. no problem.  Then I sent a simple text only email.. simple.   Next I sent an email with an arbitrary attachment.. again a sinch!

My next step was to being customizing the email to meet our needs.  I stored the email subject and message in XML  After generating the report, I added the subject, message, and attachment to the email and sent it!  Success!

Wait… the email attachment wasn’t showing up as an attachment, but as hex code within the body of the text.  First I thought maybe there was some problem in the encoding of the attachment.  I checked the file saved locally.. no prob.

After lots of searching the internet with no success, I realized my problem!!  The subject to my email, stored in xml, included a new line.  The new line seemed to shift everything in the email, making the subject AND the attachment part of the body.

Fixed my code to remove whitespace from the subject and check for other newlines, and tadaaa!!  Success in sending attachments!!!

 

-Steve

Leave a Reply

Your email address will not be published. Required fields are marked *