Java接收附件邮件错误

时间:2012-05-14 08:06:06

标签: java email character-encoding javamail mime

我有一个使用java邮件获取附件的java代码,我可以下载附件并插入特定文件夹。但问题是,当我发送带有Blackberry手机附件的电子邮件时,我会收到如下下载附件的错误。

ERROR [STDERR] java.io.FileNotFoundException: E:\CCjboss\server\sor
emicc\deploy\eis.ear\CC.war\attachment\=?utf-8?B?MS5qcGc= (The filename, directo
ry name, or volume label syntax is incorrect)
07:31:10,739 ERROR [STDERR]     at java.io.FileOutputStream.open(Native Method)
07:31:10,741 ERROR [STDERR]     at java.io.FileOutputStream.<init>(FileOutputStr
eam.java:179)
07:31:10,744 ERROR [STDERR]     at java.io.FileOutputStream.<init>(FileOutputStr
eam.java:131)
07:31:10,747 ERROR [STDERR]     at com.infosense.eis.webapp.util.Mobilekiran.sav
eFile(Mobilekiran.java:230)
07:31:10,750 ERROR [STDERR]     at com.infosense.eis.webapp.util.Mobilekiran.Mob
ileTimeCheckTask(Mobilekiran.java:179)
07:31:10,753 ERROR [STDERR]     at com.infosense.eis.webapp.util.Mobilekiran.run
(Mobilekiran.java:38)
07:31:10,756 ERROR [STDERR]     at java.util.TimerThread.mainLoop(Timer.java:432
)
07:31:10,759 ERROR [STDERR]     at java.util.TimerThread.run(Timer.java:382)

我认为它有一些编码和解码问题。附件被编码或其他东西

=?utf-8?B?MS5qcGc=

以下是接收带附件的电子邮件的代码

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
import java.util.*;
import java.lang.String;
import javax.mail.internet.*;
/**
 *
 * @author  Administrator
 */
public class Mobilekiran extends TimerTask{
     List emattach=new ArrayList();
  List emails = new ArrayList();
  List attachments=new ArrayList();
  String emailbody=null;
  String bodypart=null;
  String s1=null;
  String fromaddress=null;
  String subject=null;
  String body=null;
  String date1=null;
  String filename=null;
  int filesize=0;

    public void run() {
        MobileTimeCheckTask(); 
    }

    private void MobileTimeCheckTask(){

    String host = "mail.**********.com";
    String username = "*****@****.com";
    String password = "*******";
    String downloadDir = "E:/CCjboss/server/soremicc/deploy/eis.ear/CC.war/attachment";
    String provider = "pop3";
        try{

             // Create empty properties
  Properties props = new Properties();

  // Get the session
  Session session = Session.getInstance(props, null);

  // Get the store
  Store store = session.getStore("pop3");
  store.connect(host, username, password);

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


  Message messages[] = folder.getMessages();
   for (int i = 0; i < messages.length; i++) {


    // from 
       Address[] toaddress1=messages[i].getAllRecipients();
      String toaddress=toaddress1[0].toString();
       System.out.println("to address"+toaddress1[0].toString());

    emails.add(0,messages[i].getFrom()[0].toString());
    s1=messages[i].getFrom()[0].toString();
    InternetAddress address = new InternetAddress(s1);
     fromaddress=address.getAddress();

    // subject
    emails.add(1,messages[i].getSubject());
    subject=messages[i].getSubject();
    // received date
    if (messages[i].getReceivedDate() != null) {
     emails.add(2,messages[i].getReceivedDate().toString());
     date1=messages[i].getReceivedDate().toString();
      // emattach.add(0,emails);
    } else {
     emails.add(2,new Date());
     date1=(new Date()).toString();
      // emattach.add(0,emails);
    }

    // body and attachments
    //email.body = "";
    Object content = messages[i].getContent();
    if (content instanceof java.lang.String) {

     emails.add(3,(String) content);

    } else if (content instanceof Multipart) {

     Multipart mp = (Multipart) content;
     boolean d=true;
     for (int j = 0; j < mp.getCount(); j++) {

      Part part = mp.getBodyPart(j);
      BodyPart bp=mp.getBodyPart(j);
      Object o2 = bp.getContent();
      if (o2 instanceof String) {

                            System.out.println("**This is a String BodyPart**");
                            System.out.println("bodypart "+(String)o2);
                            if(d)

                            emails.add(3,(String)o2);
                            body=(String)o2;
                            d=false;

                        } 

      String disposition = part.getDisposition();

      if (disposition == null) {

       MimeBodyPart mbp = (MimeBodyPart) part;
       if (mbp.isMimeType("text/plain")) {
        // Plain

        emailbody += (String) mbp.getContent();
           emails.add(3,emailbody);

       } 

      } else if ((disposition != null) && (disposition.equals(Part.ATTACHMENT) || disposition .equals(Part.INLINE))) {

       // Check if plain
       MimeBodyPart mbp = (MimeBodyPart) part;
       if (mbp.isMimeType("text/plain")) {

           emailbody += (String) mbp.getContent();
           emails.add(3,emailbody);

       } else {
        //EmailAttachment attachment = new EmailAttachment();

           System.out.println("the filename  i required"+part.getFileName().toString());


        attachments.add(0,Mobilekiran.decodeName(part.getFileName()));
         filename=decodeName(part.getFileName());
        File savedir = new File(downloadDir);
        savedir.mkdirs();
        // File savefile = File.createTempFile( "emailattach", ".atch", savedir);
        File savefile = new File(downloadDir,attachments.get(0).toString());
        attachments.add(1,savefile.getAbsolutePath());
        attachments.add(2,new Integer(Mobilekiran.saveFile(savefile, part)));
        filesize=saveFile(savefile,part);
        emattach.add(0,attachments);
       }
      }
     } // end of multipart for loop 
    emattach.add(0,emails);
   System.out.println(emattach);
   emattach.clear();
   System.out.println(emattach);
    messages[i].setFlag(Flags.Flag.DELETED , true);

      } // end messages for loop



    // Finally delete the message from the server.

   }

   // Close connection
   folder.close(true); // true tells the mail server to expunge deleted messages
   store.close();
        }catch(Exception e){
            e.printStackTrace();
         }
    }  
    private static String decodeName(String name) throws Exception {
  if (name == null || name.length() == 0) {
   return "unknown";
  }
  String ret = java.net.URLDecoder.decode(name, "UTF-8");

  // also check for a few other things in the string:
  ret = ret.replaceAll("=\\?utf-8\\?q\\?", "");
  ret = ret.replaceAll("\\?=", "");
  ret = ret.replaceAll("=20", " ");

  return ret;
 }

 private static int saveFile(File saveFile, Part part) throws Exception {

  BufferedOutputStream bos = new BufferedOutputStream(
    new FileOutputStream(saveFile));

  byte[] buff = new byte[2048];
  InputStream is = part.getInputStream();
  int ret = 0, count = 0;
  while ((ret = is.read(buff)) > 0) {
   bos.write(buff, 0, ret);
   count += ret;
  }
  bos.close();
  is.close();
  return count;
 }
}

请帮助我解决上述问题。

1 个答案:

答案 0 :(得分:0)

看起来附件已编码。