缺少开始边界使用附件文件读取消息时异常 [英] Missing start boundary Exception when reading messages with an attachment file

查看:155
本文介绍了缺少开始边界使用附件文件读取消息时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么在从邮件服务器中读取附件文件的邮件时,我会收到以下异常:

I don't know why I'm getting the following exception when reading a mail with an attachment file from mail server:

Exception in thread "main" javax.mail.MessagingException: Missing start boundary

        at javax.mail.internet.MimeMultipart.parsebm<MimeMultipart.java:872)
        at javax.mail.internet.MimeMultipart.parse<MimeMultipart.java:493)
        at javax.mail.internet.MimeMultipart.getCount<MimeMultipart.java:240)
        at GetParts.handleMultipart(GetParts.java:57)
        at GetParts.main(GetParts.java:42)

我正在使用的文件阅读这些消息是:

The file which I'm using to read those messages is:

import java.io.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class GetParts {
  public static void main (String args[]) 
      throws Exception {
    String host = args[0];
    String username = args[1];
    String password = args[2];

    // Get session
    Properties props=new Properties();
    props.put("mail.mime.multipart.ignoremissingboundaryparamete",true);
    Session session = Session.getInstance(
      props, null);
ContentType ct=new ContentType();
    // Get the store
    Store store = session.getStore("pop3");
    store.connect(host, username, password);

    // Get folder
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_ONLY);

    BufferedReader reader = new BufferedReader (
      new InputStreamReader(System.in));

    // Get directory
    Message message[] = folder.getMessages();
    for (int i=0, n=message.length; i<n; i++) {
       System.out.println(i + ": "
         + message[i].getFrom()[0] 
         + "\t" + message[i].getSubject());
          //message[i].setHeader("Content-Type","multipart/mixed");
      System.out.println("Do you want to get the content? [YES to read/QUIT to end]");
      String line = reader.readLine();
      if ("YES".equals(line)) {
        Object content = message[i].getContent();
        if (content instanceof Multipart) {
          handleMultipart((Multipart)content);
        } else {
          handlePart(message[i]);
        }
      } else if ("QUIT".equals(line)) {
        break;
      }
    }

    // Close connection 
    folder.close(false);
    store.close();
  }
  public static void handleMultipart(Multipart multipart) 
      throws MessagingException, IOException {
      System.out.println(multipart.getCount());
    for (int i=0, n=multipart.getCount(); i<n; i++) {
      handlePart(multipart.getBodyPart(i));
    }
  }
  public static void handlePart(Part part) 
      throws MessagingException, IOException {
    String disposition = part.getDisposition();
    System.out.println("Disposition "+disposition);
    String contentType = part.getContentType();
    System.out.println("contentType "+contentType);
    if (disposition == null) { // When just body
      System.out.println("Null: "  + contentType);
      // Check if plain
      if ((contentType.length() >= 10) && 
          (contentType.toLowerCase().substring(
           0, 10).equals("text/plain"))) {
        part.writeTo(System.out);
      } else { // Don't think this will happen
        System.out.println("Other body: " + contentType);
        part.writeTo(System.out);
      }
    } else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
      System.out.println("Attachment: " + part.getFileName() + 
        " : " + contentType);
      saveFile(part.getFileName(), part.getInputStream());
    } else if (disposition.equalsIgnoreCase(Part.INLINE)) {
      System.out.println("Inline: " + 
        part.getFileName() + 
        " : " + contentType);
      saveFile(part.getFileName(), part.getInputStream());
    } else {  // Should never happen
      System.out.println("Other: " + disposition);
    }
  }
  public static void saveFile(String filename,
      InputStream input) throws IOException {
    if (filename == null) {
      filename = File.createTempFile("xx", ".out").getName();
    }
    // Do no overwrite existing file
    File file = new File(filename);
    for (int i=0; file.exists(); i++) {
      file = new File(filename+i);
    }
    FileOutputStream fos = new FileOutputStream(file);
    BufferedOutputStream bos = new BufferedOutputStream(fos);

    BufferedInputStream bis = new BufferedInputStream(input);
    int aByte;
    while ((aByte = bis.read()) != -1) {
      bos.write(aByte);
    }
    bos.flush();
    bos.close();
    bis.close();
  }
}


推荐答案

我们也有同样的问题。边界是在Multipart Content-Type中指定的。您可以在此来源中找到更多信息。您还可以使用 getContentType()函数来观看当前的消息。在我的情况下,我获得了这个结果:

I've just had the same problem. The boundary is specified within the Multipart Content-Type. You can find further information in this source. You can also watch the one of your current Message using the getContentType() function. In my case I obtained this result:

multipart/mixed; boundary=--boundary_25_2d74d02b-d0d6-4f28-a311-4d1b7d107417

所以 getCount()函数使用这个边界来分离组成多部分的所有部分。看起来可能会有这种边界被破坏的情况。

So the getCount() function uses this boundary to separate all the parts that compose the multiple part. Looks like there could be cases in which this boundary is corrupted.


mail.mime.multipart.ignoreexistingboundaryparameter系统属性可能设置为true,导致任何边界被忽略,而是搜索边界消息中的一行与mail.mime.multipart.ignoremissingboundary参数一样。

The mail.mime.multipart.ignoreexistingboundaryparameter System property may be set to true to cause any boundary to be ignored and instead search for a boundary line in the message as with mail.mime.multipart.ignoremissingboundaryparameter.

我遵循这些说明,一切正常。我添加了以下代码:

I followed this instructions and everything works all right. I added the code below:

System.setProperty("mail.mime.multipart.ignoreexistingboundaryparameter", "true");

希望它有帮助!

这篇关于缺少开始边界使用附件文件读取消息时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆