如何在Blackberry中使用电子邮件附加多个图像?

时间:2012-04-13 10:53:12

标签: email blackberry

我想在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()); 
                  }

        }

提前完成。

2 个答案:

答案 0 :(得分:2)

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

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;         

}

我使用过这段代码。它工作正常。

答案 1 :(得分:1)

只需为每个图片创建一个新的SupportedAttachmentPart,然后使用addBodyPart方法将其添加到邮件中。

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