2010-04-08 117 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

相关问题