2012-04-13 14 views
0

BBでメールで複数の画像を添付したい。これどうやってするの?どんな体にもアイデアはありますか?助けてください。私は電子メールで1つの画像だけを送信するときにうまく動作する私のコードです。どのような変更を私のコードで複数の画像を添付するために行う必要があります。Blackberryで複数の画像を電子メールで添付するにはどうすればよいですか?

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。

答えて

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;   

}

私はこのコードを使用しています。それはうまく動作します。

+0

forx再生のために私にチェックさせてください.. – Hasmukh

+0

http://chat.stackoverflow.com/rooms/4014/knowledge-sharing-center-for-blackberry-android-and-java – Hasmukh

+0

あなたはreadstreamメソッドplzを置くことができます.. – Hasmukh

1

画像ごとに新しいSupportedAttachmentPartを作成し、addBodyPartメソッドを使用してメッセージに追加するだけです。

ボディパートと添付部品でマルチパートを作成したら、msg.setContent(mp)を呼び出します。

+0

返信ありがとうございました... – Hasmukh

+0

私はそれを試しています、それは大丈夫ですか?以下のために(; iは2 <; I = 0 int型私は++) \t \t \t \t {\t \t \t \t \t \t \t \t \t \t PNGEncodedImage IMG = PNGEncodedImage.encode(写真1)。 \t \t \t \t \t \t SupportedAttachmentPart PT =新しいSupportedAttachmentPart(MP、img.getMIMEType()、 "Weed.png"、img.getData())。 \t \t \t \t \t mp.addBodyPart(pt); \t \t \t \t} – Hasmukh

+0

私は2回で1枚の画像を添付しようとしましたが、画像では添付しませんでした。あなたは私が間違っていることを教えていただけますか? – Hasmukh

関連する問題