SORU
17 Kasım 2009, Salı


Java kullanarak Posta ekleri

Tüm iletileri indirilen ve onları saklamak istiyorum

Message[] temp;

Nasıl bu mesajları her biri için ek listesi alabilirim

List<File> attachments;

Not:hiç bir konuda, lütfen, sadece JavaMail libs.

CEVAP
17 Kasım 2009, Salı


İstisnasız işleme, ama burada gider:

List<File> attachments = new ArrayList<File>();
for (Message message : temp) {
    Multipart multipart = (Multipart) message.getContent();
    // System.out.println(multipart.getCount());

    for (int i = 0; i < multipart.getCount(); i  ) {
        BodyPart bodyPart = multipart.getBodyPart(i);
        if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()) &&
               !StringUtils.isNotBlank(bodyPart.getFileName())) {
            continue; // dealing with attachments only
        } 
        InputStream is = bodyPart.getInputStream();
        File f = new File("/tmp/"   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);
    }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Kyletiv7

    Kyletiv7

    28 Mayıs 2007
  • Microsoft Research

    Microsoft Re

    24 EKİM 2008
  • Slave Boy Films - Fandom from a Galaxy Far Far Away

    Slave Boy Fi

    12 HAZİRAN 2009