Hasmukh
Hasmukh

Reputation: 4670

How can i attach multiple images with email in Blackberry?

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.

Upvotes: 0

Views: 501

Answers (2)

Rajkiran
Rajkiran

Reputation: 259

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.

Upvotes: 2

Mister Smith
Mister Smith

Reputation: 28178

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).

Upvotes: 1

Related Questions