2013-07-10 223 views
1

我有一個郵件發送應用程序,它會隨附件一起發送電子郵件。除了附件外,每個工作都很好。我正在使用這個code。但是我不知道這段代碼中缺少什麼或者什麼是錯的。請幫忙。這裏是附件部分。Android電子郵件發送失敗,發生錯誤發送失敗:發送郵件時出現IOException

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

在我的主要活動,我使用

public final static String APP_PATH_SD_CARD = "/Images_Book/"; 
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + APP_PATH_SD_CARD; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    String mailBody = "Email is send along with attachment"; 
    Mail m = new Mail("FTP backup failed", mailBody); 
    try { 
     m.addAttachment(path); 
     m.send(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 
+0

請張貼堆棧跟蹤。 – thegrinner

+0

這裏是堆棧跟蹤 07-10 21:03:29.312:W/Trace(3972):來自nativeGetEnabledTags的意外值:0 07-10 21:03:29.332:D/gralloc_goldfish(3972):未檢測到GPU仿真的仿真器。 07-10 21:03:29.382 W/Trace(3972):來自nativeGetEnabledTags的意外值:0 07-10 21:03:29.433:W/Trace(3972):來自nativeGetEnabledTags的意外值:0 07-10 21:03:31.173:D/dalvikvm(3972):GC_CONCURRENT釋放289K,14%空閒2708K/3120K,暫停72ms + 5ms,總計108ms 07-10 21:03:32.722:E/MAIL發送:發送消息時發生IOException –

回答

1

嘗試

public final static String APP_PATH_SD_CARD = "/Images_Book/filetobeattached.ext"; 

而不是

public final static String APP_PATH_SD_CARD = "/Images_Book/";