2012-05-14 49 views
0

我有一個java代碼來獲取使用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; 
} 
} 

請幫我在解決上述問題的代碼。

+0

字符串'=?utf-8?B?MS5qcGc ='是「1.jpg」的mime/base64編碼版本。我會想到,使用'getFileName'時,javamail會自動對此進行解碼。 –

+0

它沒有,這是爲什麼:(http://www.oracle.com/technetwork/java/javamail/faq/index.html#encodefilename) –

回答

0

看起來像附件是編碼。

+0

解碼我們正在使用私人靜態字符串decodeName(字符串名稱)拋出異常{name == null || name.length()== 0){ return「unknown」; } String ret = java.net.URLDecoder.decode(name,「UTF-8」); //還檢查字符串中的其他一些內容: ret = ret.replaceAll(「= \\?utf-8 \\?q \\?」,「」); ret = ret.replaceAll(「\\?=」,「」); ret = ret.replaceAll(「= 20」,「」); return ret; } – Kiran

+0

這是UTF-8解碼。該消息用您的問題中提到的公鑰進行編碼。沒有私鑰就無法讀取它們。據我所知,RIM在郵寄之前加密所有郵件。同樣的原因RIM的服務在印度停留了一段時間。 –