4

I'm trying to send a file base64-encoded via apache.commons.mail and I just can't seam to get the Content-Transfer-Encoding: base64 header where it's supposed to go.

// Create the email
MultiPartEmail email = new MultiPartEmail();
email.setSmtpPort(587);
email.setDebug(false);
email.setHostName("smtp.gmail.com");
email.setAuthentication("from@gmail.com", "password");
email.setTLS(true);

email.addTo("to@example.com");
email.setFrom("from@example.com");
email.setSubject("subject");

email.attach(new ByteArrayDataSource(
     Base64.encodeBase64(attachFull.getBytes()), "text/plain"), 
     "samplefile.txt", 
     "sample file desc", 
     EmailAttachment.ATTACHMENT
);

And this is what gets to the recipient.

------=_Part_0_614021571.1334210788719
Content-Type: text/plain; charset=Cp1252; name=texto.txt
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment; filename=samplefile.txt
Content-Description: sample file desc

How I can specify that the file is Base64 encoded?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Frankie
  • 24,627
  • 10
  • 79
  • 121

2 Answers2

5

The easiest solution would be to do something like this:

// create a multipart leg for a specific attach
MimeMultipart part = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler (new DataHandler(new ByteArrayDataSource(attachFull.getBytes(), "text/plain")));
messageBodyPart.removeHeader("Content-Transfer-Encoding");
messageBodyPart.addHeader("Content-Transfer-Encoding", "base64");
part.addBodyPart(messageBodyPart);
email.addPart(part);

And javax will automatically convert your file to base64.

Hope it helps.

Frankie
  • 24,627
  • 10
  • 79
  • 121
  • I used the answer to manipulate body transfer encoding, works as well. I only needed to drop `removeHeader` call. – Jarekczek Jul 25 '14 at 10:14
1

You might try overriding the attach method and set the Content-Transfer-Encoding header in there. By default the framework doesn't set it for you or expose the MIME bodyPart cleanly.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • That may do... I've had some success adding a `MimeMultipart()` (javax.mail) as an external part to commons.mail. But something's still not right. I may be messing the Base64 encoding. Let me dig a bit further... – Frankie Apr 12 '12 at 07:04
  • Accepted your answer as it put me on track. Posted mine as a reference. Thanks. – Frankie Apr 12 '12 at 07:19