3

Right now I have emails stored in a database, and I need to be able to retrieve them and download attachments If they have them. Currently, I am stumped on a way to convert the byte[] which I have stored the email as into a MailMessage or MimeMessage. I believe I have gotten to the byte[] into the Mimebody part but I am not sure how I would parse through it and pull out the attachment.

final byte[] mailMessageString = resultSet.getBytes(mailMessageIndex);
File file = new File("C:\\Users\\khurt\\Downloads\\op.txt");
List<File> attachments = new ArrayList<File>();
@SuppressWarnings("deprecation")
String mimeType = file.toURL().openConnection().getContentType();
MimeBodyPart att = new MimeBodyPart(); 
ByteArrayDataSource efe = new ByteArrayDataSource(mailMessageString, mimeType); 
DataHandler dh = new DataHandler(efe);
att.setDataHandler(dh);
att.setFileName(bds.getName());
Multipart multipart = (Multipart) att.getContent();
multipart.addBodyPart(att);
for (int i = 0; i < multipart.getCount(); i++) 
{
    BodyPart bodyPart = multipart.getBodyPart(i);
    if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())
        && !StringUtils.isNotBlank(bodyPart.getFileName())) 
    {
        InputStream is = bodyPart.getInputStream();
        File f = new File("C:\\Users\\khurt\\Downloads\\"
            + bodyPart.getFileName());
        FileOutputStream fos = new FileOutputStream(f);
        byte[] buf = new byte[4096];
        int bytesRead;
        while ((bytesRead = is.read(buf)) != -1) 
        {
            fos.write(buf, 0, bytesRead);
        }
        fos.close();
        attachments.add(f);
    }
    else
    {
        System.out.println("there is nata");
    }
}   

There are no actual errors when it gets to the for - loop but I have not yet parsed the email to get any of the files. Is it possible to parse through a MimeBodyPart?

For Reference the emails will have the data about it then the email attachment starts with an empty line and then: (The email inst java, Stack Overflow wouldn't let me post it without formatting it, though. Also some emails don't have the header with the info about the attachment).

--_002_2733D716DEFD0D49BF462DE618263C07019302260BCVGEXCEMAIL01_
Content-Type: image/gif; name="image001.gif"
Content-Description: image001.gif
Content-Disposition: inline; filename="image001.gif"; size=1669;
    creation-date="Tue, 14 Jun 2011 14:42:12 GMT";
    modification-date="Tue, 14 Jun 2011 14:42:12 GMT"
Content-ID: <image001.gif@01CC2204.E828E6F0>
Content-Transfer-Encoding: base64

R0lGODlhiwA9ANUAAAAzZv/4+lvKwABVBzlstgY+q/wCBNebwwNN/l7H+ZsrHB0NuPkc/Z4kBm
jLEQFW+MqZ+yxQ8/b8wAANLb5NV/gu/y9eWwsl9/nzBZg8ZQU6+/z+/P0MFARPrv79BvcuCfobYg
JICZs+/39/Xf4NDZ47/M2ZGnvXCNqWGBoNaAgt6UlNBwcxFBcM7e5gAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAAHAP8ALAAAAACLAD0AAAb/wIBw
SCwaj8ikcslsOp/QqHRKrVqv2Kx2y+16v+CweEwum8/otHrNtnoMcFBlTq9c4O189s2oaDoDgYKD
hIIPHR8XHnqMSgYMHx0PhYIhHZeYl5SDGiSNjRwMBQiElgUqIHhPcCoak4Ign2sGFYCCHacGHF0X
tg+esmS0tiEaKgbAYwWBDMFgJAyuxQwGi2kekyrOXBcfliqKjIAG21ceIAUFDLvBkuVV5+vvASEF
8/dVHAPJ+P1OBSz8CWxC4oG1gQiPnErIsEjBgw0ZAiQIp2K1iHpIDAg4hMOjWh1IbRKEoIMdjGhA
BDo0csChTJlErryAsoxKQ7hUULvYxEOv/0D8aoLxyM4cqRCphAr0oCLEoA6xlOLjU2ASAqkCOUzC
KjBECK6y3oAcRCoq2DbnNLjswELFsUcsNJxFWwEBiwtB5zK6wKLoPRMKMCgQkMBCAoYkIM5rcODA
AiELMgAQoZdRgxYAShQhQLlyngMAUhixECFABAcOFEhwIEBIAgwOBCsg0GCIhRYOhIhIDcEBAQUQ
WnhWYgIAgNJJjAvBAACF7slCTJgYEgHzYQEWAhTPHaBzCgDDi4gwPj05+AACAHAf37lICwWhiWwn
user3772418
  • 49
  • 1
  • 2
  • 9
  • Iterate over the `BodyPart`s of the `MimeMultipart`. Use `MimeMultipart.getCount()` to retrieve the number of parts then `MimeMultipart.getBodyPart(index)` to retrieve the actual part – Bruno Grieder Jun 27 '14 at 15:05
  • Your code dosn't seem complete, you load the bytes into mailMessageString then create a ByteArrayDataSource called efe from it but then never use that. Also you use a variable bds but this is not defined or set anywhere. – dethorpe Jun 27 '14 at 15:44
  • That was my mistake on puttin the code in, I edited it a few hours ago after I figured out an earlier problem and forgot to change the varaible from bds to efe – user3772418 Jun 27 '14 at 17:38

1 Answers1

4

Use the MimeMessage constructor that takes an InputStream. Then access the attachments in the message in the normal fashion. See the msgshow.java sample program for example code.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • 1
    The problem is that I don't have a mail session, there is no constructor that just takes a InputStream – user3772418 Jun 30 '14 at 13:48
  • 4
    Just create a Session to use for this purpose. The Session just holds configuration information. If you don't need to change any defaults it's as easy as Session session = Session.getInstance(new Properties()); – Bill Shannon Jun 30 '14 at 20:56