javamail接收的邮件,如果附件过大,则报错无法接收

用javamail接收邮件,当附件过大时(5M以上),则会报如下错误信息:
javax.mail.MessagingException: No inputstream from datasource
at javax.mail.internet.MimeMultipart.parsebm(MimeMultipart.java:646)
at javax.mail.internet.MimeMultipart.parse(MimeMultipart.java:383)
at javax.mail.internet.MimeMultipart.getCount(MimeMultipart.java:229)
at com.sipai.mail.PraseMimeMessag.getMailContent(PraseMimeMessag.java:662)
at com.sipai.mail.Email_Autherticatorin.collectmail(Email_Autherticatorin.java:720)

经查看,提示在counts = multipart.getCount()处错误,我现将该块的相关代码贴在后面,这块是用来解析邮件正文和附件的代码。

public void getMailContent(Part part,String emlurltruetmp,String userid) throws Exception {
    // 以下为将eml文件存放到指定位置
    String emlurl = ".eml"; // 得到eml的名字
    String emlname1 = createfilename(userid); // 得到文件目录名字,通过绝对路径取得
    String emltruename = emlurltruetmp + emlname1 + emlurl;// 得到将要存放的文件绝对路径全名称,用户将邮件打包成eml格式
    emltruename=emltruename.replace("\\", "\\\\");
    emlname=emltruename;
    //savemailemlurl(emltruename);//暂时不需要生成eml文件
    // 以上为结束

    String contenttype = part.getContentType();
    try{
        int nameindex = contenttype.indexOf("name");
        boolean conname = false;
        if (nameindex != -1) {
            conname = true;
        }
        if (part.isMimeType("text/plain") && !conname) {

// System.out.println("text/plain");
try{
bodytext.append(todecodeText((String) part.getContent()));
}catch(UnsupportedEncodingException uex){
InputStream is = part.getInputStream();
bodytext.append(is);
}
} else if (part.isMimeType("text/html") && !conname) {
// System.out.println("text/html");
if(bodytext==null || bodytext.toString().trim().equals("")){
try{
bodytext.append(todecodeText((String) part.getContent()));
}catch(UnsupportedEncodingException uex){
InputStream is = part.getInputStream();
bodytext.append(is);
}

            }else{

                bodytext.delete(0, bodytext.length()-1);
                try{
                    bodytext.append(todecodeText((String) part.getContent()));
                }catch(UnsupportedEncodingException uex){
                    InputStream is = part.getInputStream();
                    bodytext.append(is);
                }
            }
        } else if (part.isMimeType("multipart/*")) {
            //System.out.println("multipart/*");
            Multipart multipart = (Multipart) part.getContent();
            int counts=0;
            if (multipart instanceof Multipart){
                try{
                    counts = multipart.getCount();  
                }catch(MessagingException e){
                    e.printStackTrace();
                    //attachflag=false;
                    counts=1;
                }
                for (int i = 0; i < counts; i++) {
                    getMailContent(multipart.getBodyPart(i),emlurltruetmp,userid);
                }   
            }
        } else if (part.isMimeType("message/rfc822")) {
            //System.out.println("message/rfc822");
            getMailContent((Part) part.getContent(),emlurltruetmp,userid);
        } else {

        }
    }catch(Exception e){
        e.printStackTrace();
    }
}
查看全部
yahhuop
yahhuop
2010/03/26 09:03
  • 企业应用
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

0个回复