0

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

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;
 }
}

请帮助我解决上述问题。

4

1 回答 1

0

看起来附件是编码的。

于 2012-05-14T08:08:28.360 回答