【问题标题】:How can i attach multiple images with email in Blackberry?如何在 Blackberry 中通过电子邮件附加多个图像?
【发布时间】:2012-04-18 15:01:02
【问题描述】:

我想在 BB 中附加多个带有电子邮件的图像。我怎样才能做到这一点?有没有人有想法?请帮助我。下面是我的代码,当我只用电子邮件发送一张图片时,它可以正常工作。那么我应该在我的代码中进行哪些修改以附加多个图像。

  public static void SendMailAttachment(Bitmap screenshot)
            {            

              String htmlContent = "String" ;     
                  try 
                  {
                       Multipart mp = new Multipart();
                       Message msg = new Message();
                       Address[] addresses = {new Address("","")};

                   for (int i = 0; i<2 ; i++)
                     {
                            PNGEncodedImage img = PNGEncodedImage.encode(screenshot);
                            SupportedAttachmentPart pt = new SupportedAttachmentPart(mp, img.getMIMEType(),
                            "Weed.png", img.getData());
                            mp.addBodyPart(pt);

                      }
                            msg.setContent(mp);
                            msg.setContent(htmlContent);

                       msg.addRecipients(RecipientType.TO, addresses);
                       msg.setSubject("Subject");          
                       Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES, new MessageArguments(msg));

                  }
                  catch (AddressException ex) 
                  {
                      System.out.println("Exception -->"+ex.getMessage()); 
                  } 
                  catch (MessagingException ex) 
                  {
                      System.out.println("Exception -->"+ex.getMessage()); 
                  }

        }

提前感谢。

【问题讨论】:

    标签: email blackberry


    【解决方案1】:

    以下代码可用于附加多个图像或文件。

    public void upload()
        {     
            Multipart mp = new Multipart();
        String fileName = null;
    
    
    
        for (int i = 0; i<2 ; i++)
        {
    
    
            //          Dialog.alert(image.);
            byte[] stream = readStream("file:///SDCard/IMG00001-20110404-1119.JPEG");
            SupportedAttachmentPart sap = new SupportedAttachmentPart(mp, MIMETypeAssociations.getMIMEType("IMG00001-20110404-1119.JPEG"),"IMG00001-20110404-1119.JPEG", stream);
            mp.addBodyPart(sap);
    
        }
    
    
        TextBodyPart tbp = new TextBodyPart(mp,"test bodyString");
        mp.addBodyPart(tbp);
    
        Folder folders[] = Session.getDefaultInstance().getStore().list(Folder.SENT);
        Message message = new Message(folders[0]);
        Address[] toAdds = new Address[1];
    
        try {
            toAdds[0] = new Address("testmailid", null);
            message.addRecipients(Message.RecipientType.TO,toAdds);
            //          message.setFrom(new InternetAddress(_from)); 
    
            //          message.addRecipients(Message.RecipientType.FROM,toAdds);
            message.setContent(mp);
            message.setSubject("test subject");
            Transport.send(message);
    
            Dialog.alert("message send successfully.");
    
        } catch (AddressException e) {
            // TODO Auto-generated catch block
            //          e.printStackTrace();
            Dialog.alert(e.getMessage());
    
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            //          e.printStackTrace();
            Dialog.alert(e.getMessage());
        }
    }
    
    private byte[] readStream(String path) 
    {
    
    
    InputStream in = null;
        FileConnection fc = null;
    byte[] bytes = null;
    
    try
    {
        fc = (FileConnection) Connector.open(path);
        if (fc !=null && fc.exists()) 
        {
            in = fc.openInputStream();
            if (in !=null)
            {
                bytes = IOUtilities.streamToBytes(in);
            }
        }
    }
    catch(IOException e) 
    {
    
    }
    finally
    {
        try
        {
            if (in != null) 
            {
                in.close();
            }
        }
        catch(IOException e)
        {                
        }
        try
        {
            if (fc !=null)
            {
                fc.close();
            }
        }
        catch(IOException e)
        {                
        }
    
    }       
    return bytes;         
    

    }

    我已使用此代码。它工作正常。

    【解决方案2】:

    只需为每个图像创建一个新的SupportedAttachmentPart 并使用addBodyPart 方法将它们添加到消息中。

    用正文部分和附件部分填充多部分后,请致电msg.setContent(mp)

    【讨论】:

    • 我正在尝试这样,可以吗? for (int i = 0; i
    • 我曾尝试在 2 次中附加一张图片,但它没有附加图片,你能告诉我我在做什么错吗?
    • 如果我没记错的话,在旧版本中每个附件有 64kB 的限制。首先尝试使用小于 64kB 的哑字节数组而不是图像数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多