mrovnanik
mrovnanik

Reputation: 123

Extracting (and uploading) attachment from an incoming mail message

Do you have any idea on how to extract attachment from the incoming message? I have a PDF attached to the message. This is what I see in the console:

['contentType':'application/pdf; name="TransakciaMK.pdf"', 'filename':'TransakciaMK.pdf', 'disposition':'attachment', 'contentBytes':'[B@d2d9e15']

I wanted to use the code used for uploading files

org.apache.commons.fileupload.FileItem

to a service, which saves the file.

  1. Is this a correct way of doing it?
  2. If yes, how can I render the attachment into a FileItem?

Added:

OK. I must be doing something wrong. I extract the byte[] using bodyPartList[2].contentBytes.getBytes(). Into variable called contentFileByte.

Then:

ByteArrayInputStream fileStream = new ByteArrayInputStream(contentFileByte)
try { docRr.putStream(fileStream) } finally { fileStream.close() }

A file is created, but it contains only:

[B@d2d9e15

There must be an error somewhere on the way of converting the byte array to a file stream.

Upvotes: 0

Views: 47

Answers (1)

Ronan Keane
Ronan Keane

Reputation: 189

I don't know if this will be helpful, you may already have seen it - but in the end of EmailEcaRule.groovy, you can see how the bodyPartList is created from the email, drawing on the JavaMail API. I think you then need to iterate through bodyPartList to check MIME type of each part in order to know if you have to treat it as text or an attachment. - Currently the existing service that your emeca rule refers to just handles it very simply, taking only the body text from bodyPartList[0].contentText. I have not tried to extract attachments yet myself, but from a previous task done, that is where I would start from to try and do this.

Upvotes: 1

Related Questions