How can i attach multiple images with email in Blackberry?

孤街醉人 提交于 2020-01-10 05:55:10

问题


I want to attach multiple images with email in BB. How can I do this? Does any body have an idea? please help me.Below is my code which works fine when i send only one image with email. so what modification should I make in my code for attaching multiple images.

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

        }

Thanx in advance.


回答1:


following code can be used to attach multiple images or files.

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;         

}

i have used this code. it works fine.




回答2:


Just create a new SupportedAttachmentPart for each image and add them to the message with the addBodyPart method.

Once the multipart is populated with the body part and the attachment parts, call msg.setContent(mp).



来源:https://stackoverflow.com/questions/10139482/how-can-i-attach-multiple-images-with-email-in-blackberry

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!