2013-02-07 113 views
4

我发送带附件的电子邮件!这是好的,但有一个问题。我添加到电子邮件正文中的文件有“完整路径”的名称。发送像...Android发送带附件的邮件

 class SendEmailAsyncTask extends AsyncTask <Void, Void, Boolean> { 
    @Override 
    protected Boolean doInBackground(Void... params) { 
     { 
      Context context = getApplicationContext(); 
      Mail m = new Mail("****[email protected]", "te****mail"); 
      String[] toArr = {"te****[email protected]****"}; 
      m.setTo(toArr); 
      m.setFrom("t****[email protected]"); 
      m.setSubject("TESTING");   
      m.setBody("Принят test"); 
      try { 
       String path = "/mnt/sdcard/qwe"; 
       File fileDir = new File(path); 
       String[] _files = fileDir.list(); 
       for (int i = 0 ; i < _files.length ; i++ ) 
       { 
        _files[i] = path + "/"+ _files[i]; 
       //  _files[i] = path + "/"+ _files[i].substring(_files[i].lastIndexOf("/")); 
         m.addAttachment(_files[i]); 
         Log.v("list sending", _files[i]); 
         Log.v("list sending", _files[i].substring(_files[i].lastIndexOf("/"))); 
       } 

       if(m.send()) { 

        for (int i = 0 ; i < _files.length ; i++ ) 
        { 
         Log.v("test mail result", "success"); 
        } 
       } else { 
        Log.v("test mail result", "fail"); 
       } 
      } catch(Exception e) { 
       Log.v("test mail result", "error while sending"); 
      }   
     } 
     return null; 
    } 
} 

日志发送的是....

 02-07 11:18:15.542: V/list sending(18255): /mnt/sdcard/qwe/1233.txt 
    02-07 11:18:15.542: V/list sending(18255): /1233.txt 
    02-07 11:18:15.542: V/list sending(18255): /mnt/sdcard/qwe/123.txt 
    02-07 11:18:15.542: V/list sending(18255): /123.txt 
    02-07 11:18:15.552: V/list sending(18255): /mnt/sdcard/qwe/12333.txt 
    02-07 11:18:15.552: V/list sending(18255): /12333.txt 
    02-07 11:18:15.722: D/dalvikvm(18255): GC_FOR_MALLOC freed 3564 objects/349496 bytes in 33ms 
    02-07 11:18:15.722: I/global(18255): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required. 
    02-07 11:18:16.222: I/SSLSocketFactory(18255): Using factory [email protected]006ed60 
    02-07 11:18:17.084: D/NativeCrypto(18255): SSL_OP_NO_SSLv3 is set 
    02-07 11:18:18.562: I/global(18255): Default buffer size used in BufferedOutputStream constructor. It would be better to be explicit if an 8k buffer is required. 
    02-07 11:18:18.562: I/global(18255): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required. 
    02-07 11:18:19.152: I/global(18255): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required. 
    02-07 11:18:27.687: V/test mail result(18255): success 

我怎样才能只在我的附件文件名?

回答

2

这里是我的addAttachment是什么样子:

public static void addAttachment(String filename) throws Exception { 
    BodyPart messageBodyPart = new MimeBodyPart(); 
    DataSource source = new FileDataSource(filename);   
    messageBodyPart.setDataHandler(new DataHandler(source)); 

    // Truncating the full file path to just filename 
    Pattern p = Pattern.compile("[^/]*$"); 
    Matcher m = p.matcher(filename); 
    if (m.find()) 
     messageBodyPart.setFileName(m.group()); 
    else 
     messageBodyPart.setFileName(filename); 

    multipart.addBodyPart(messageBodyPart); 
    } 

希望这有助于!

+0

非常感谢。想知道如何解决这个问题。我花了一段时间才知道在哪里找到addAttachment方法 - 它在Mail1.java文件中。此外,我不得不从公共静态无效的静态删除,以摆脱一个错误。 – gbotha

1

我不知道“Mail”类是什么,但它看起来像是Mail.addAttachment方法中的错误。

+0

我已经决定了我的问题... public void addAttachment(String filename)throws Exception { BodyPart messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename.substring(filename.lastIndexOf(“/”)) ); _multipart.addBodyPart(messageBodyPart); } – timonvlad

+0

谢谢任何​​方式... – timonvlad

+0

或者只是使用MimeBodyPart.attachFile方法。 –

1

试试这个:

messageBodyPart.setFileName(filename.substring(filename.lastIndexOf("/") + 1));