【问题标题】:j2me/BlackBerry - How to send Email with Attachment from Application?j2me/BlackBerry - 如何从应用程序发送带有附件的电子邮件?
【发布时间】:2010-04-08 12:05:27
【问题描述】:

嘿,我正在构建一个应用程序,用户可以在其中向某人发送电子邮件。 用户在编辑字段中输入要向其发送电子邮件的人的电子邮件 ID,然后按发送按钮,电子邮件必须带有附件。

我该怎么做??????

谷歌搜索后我真的很困惑。 谁能告诉我具体的方法

另外,如果我的 cod 文件未签名,我不能从模拟器发送电子邮件

提前致谢

【问题讨论】:

标签: email blackberry java-me attachment


【解决方案1】:

试试这个。

     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;
}

【讨论】:

  • 嘿 vivart 我之前也尝试过这种方法,但是当我的应用程序未签名时,这种方法是否会从模拟器发送邮件????所有行都毫无例外地执行,但仍然没有发送邮件。
  • 要从模拟器发送电子邮件和向模拟器发送电子邮件,您必须使用 ESS(电子邮件服务模拟器)。我已经在真实设备上测试了此代码及其工作。
  • 嘿,谢谢我也在设备上测试了这个代码,但附件不能发送....文件名表示什么----我使用了文件的路径(它存储在存储卡中)但仍然附件不能发!!!如果我不提供文件的位置/路径,那么它将获取默认位置???
  • 我使用了这个路径“file:///SDCard/BlackBerry/documents/myfolder/myfile.txt”
  • 我完全按照这段代码而不是文件路径工作,我给出了附件的名称,但仍然没有成功!!!!!!!!!!!!!!!
【解决方案2】:

这是一个创建新电子邮件并在从我的项目 BBSSH 发送之前将其提交审核的工作示例。您不需要并且可以删除的对话框/弹出窗口。在此示例中,我们将位图作为参数,并将其转换为我们附加到电子邮件的 PNG。不同的内容类型将被类似地附加。

如果代码未签名,您应该可以在模拟器中执行任何操作;但是我认为实际上不会发送电子邮件,因为模拟器本身没有连接到实际的邮件服务器。

/** * 发送反馈,可选择将提供的位图作为附件。 * * 调用者有责任确保调用它 *以适当同步的方式。 * * @param screenshot - 如果不为null,则此函数提示 * 用户将屏幕截图作为附件包含在内。 */ 公共静态无效发送反馈(位图截图){ ResourceBundle b = ResourceBundle.getBundle(BBSHRResource.BUNDLE_ID, BSSHRResource.BUNDLE_NAME); 尝试 { 多部分 mp = new Multipart(); 消息消息 = 新消息(); 地址[] 地址 = {new Address("recipient@example.com", "Recipient Name")}; if (screenshot == null || Dialog.ask(Dialog.D_YES_NO, b.getString(BBSHRResource.MSG_FEEDBACK_INCLUDE_SCREENSHOT), Dialog.YES) == Dialog.NO) { } 别的 { 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, 地址); msg.setSubject("BBSSH 反馈"); Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, new MessageArguments(msg)); } 捕捉(地址异常前){ Logger.getLogger().log(10, "无法发送反馈:" + ex.getMessage()); } 捕捉(MessagingException ex){ Logger.getLogger().log(10, "无法发送反馈:" + ex.getMessage()); } }

如果您想发送消息而不是将其提交审查而不是 Invoke.invokeApplication 您将使用 Transport.send(msg);

【讨论】:

猜你喜欢
  • 2016-10-08
  • 1970-01-01
  • 1970-01-01
  • 2016-09-08
  • 2021-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多