2010-04-08 118 views
2

嘿,我正在建立一個應用程序,用戶可以發送電子郵件給一個人。 用戶在編輯字段中輸入要發送電子郵件的人員的電子郵件ID,然後按發送按鈕,該電子郵件必須與附件一起發送。j2me /黑莓 - 如何發送電子郵件與附件從應用程序?

我該怎麼辦??????

我在谷歌搜索後真的很困惑。 有人可以告訴我具體的方式

也不能筆者從模擬器發送電子郵件,如果我的COD文件是無符號

在此先感謝

+0

如果你有這個問題,請你幫忙http://stackoverflow.com/questions/10139482/how-can-i-attach-multiple-images-with-email-in-blackberry – Hasmukh 2012-04-17 14:22:52

回答

2

試試這個。

 Address[] address = new Address[1]; 
        try { 
         address[0] = new Address(email,name); 
        } catch (AddressException e1) { 
         // TODO Auto-generated catch block 
         e1.printStackTrace(); 
        } 
        byte[] data = readFile(); 
        Multipart multipart = new Multipart(); 
        SupportedAttachmentPart attach = new SupportedAttachmentPart(multipart, 
          "application/x-example", "test.txt", data); 
        multipart.addBodyPart(attach); 
        Message msg = new Message(); 
        // add the recipient list to the message 
        try { 
         msg.addRecipients(Message.RecipientType.TO, address); 
         // set a subject for the message 
         msg.setSubject("Mail from mobile"); 
         msg.setContent(multipart); 
        } catch (MessagingException e1) { 
         // TODO Auto-generated catch block 
         e1.printStackTrace(); 
        } 


        try { 
         Transport.send(msg); 
        } catch (MessagingException e) { 
         System.out.println(e.getMessage()); 
        } 
private static byte[] readFile() { 
    String fName ="file:///store/home/user/test.txt"; 
    byte[] data = null; 
    FileConnection fconn = null; 
    DataInputStream is = null; 
    try { 
      fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE); 
      is = fconn.openDataInputStream();    
      data = IOUtilities.streamToBytes(is); 
    } catch (IOException e) { 
      System.out.println(e.getMessage()); 
    } finally { 
      try { 
        if (null != is) 

          is.close(); 
        if (null != fconn) 
          fconn.close(); 
      } catch (IOException e) { 
        System.out.println(e.getMessage()); 
      } 
    } 
    return data; 
} 
+0

嘿vivart我早些時候也嘗試過這種方法,但是當我的應用程序未簽名時,此方法是否會從模擬器發送郵件? 所有行執行沒有任何異常,但仍然沒有郵件發送。 – Swati 2010-04-09 04:03:45

+0

從模擬器發送電子郵件,你必須使用ESS(電子郵件服務模擬器)。我已經在真實設備及其工作上測試了這些代碼。 – Vivart 2010-04-09 04:45:16

+0

嘿謝謝我也在設備上測試了這個代碼,但附件無法發送.... 什麼是文件名指示----我使用了文件路徑(它被存儲在存儲卡中),但仍然不能發送! ! 如果我不提供文件的位置/路徑,那麼它會獲取默認位置? – Swati 2010-04-09 10:17:47

0

下面是一個創建新電子郵件並在從我的項目BBSSH發送它之前審查的實用示例。您不需要的對話框/彈出窗口,可以刪除。在這個例子中,我們將位圖作爲參數,並將其轉換爲我們附加到電子郵件中的PNG。不同的內容類型將被類似地附加。

如果代碼是無符號的,你應該可以在模擬器中做任何事情;但是我認爲電子郵件實際上不會被髮送,因爲模擬器本身沒有連接到實際的郵件服務器。

 
/** 
    * Sends feedback, optionally including the provided bitmap as an attachement. 
    * 
    * it is the caller's responsibility to ensure that this is invoked 
    * in a properly synchronized manner. 
    * 
    * @param screenshot - if not null, this function prompts 
    * the user to include the screenshot as an attachment. 
    */ 
public static void sendFeedback(Bitmap screenshot) { 
    ResourceBundle b = ResourceBundle.getBundle(BBSSHRResource.BUNDLE_ID, 
    BBSSHRResource.BUNDLE_NAME); 
    try { 
    Multipart mp = new Multipart(); 
    Message msg = new Message(); 
    Address[] addresses = {new Address("[email protected]", "Recipient Name")}; 
    if (screenshot == null || Dialog.ask(Dialog.D_YES_NO, 
    b.getString(BBSSHRResource.MSG_FEEDBACK_INCLUDE_SCREENSHOT), Dialog.YES) == Dialog.NO) { 
    } else { 
    PNGEncodedImage img = PNGEncodedImage.encode(screenshot); 
    SupportedAttachmentPart pt = new SupportedAttachmentPart(mp, img.getMIMEType(), 
     "bbssh-screen.png", img.getData()); 
    mp.addBodyPart(pt); 
    msg.setContent(mp); 
    } 
    msg.addRecipients(RecipientType.TO, addresses); 
    msg.setSubject("BBSSH Feedback"); 
    Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, new MessageArguments(msg)); 
    } catch (AddressException ex) { 
    Logger.getLogger().log(10, "Unable to send feedback: " + ex.getMessage()); 
    } catch (MessagingException ex) { 
    Logger.getLogger().log(10, "Unable to send feedback: " + ex.getMessage()); 
    } 

}

如果你想發送消息,而不是把它進行審查,而不是Invoke.invokeApplication你會使用Transport.send(MSG)的,

+0

你能幫助我,請http://stackoverflow.com/questions/10139482/how-can-i-attach-multiple-images-with-email-in-blackberry – Hasmukh 2012-04-17 14:24:25

相關問題