如何在黑莓中发送带图像附件的邮件

时间:2011-12-28 13:00:42

标签: blackberry

我正在开发使用Mail功能的BlackBerry应用程序。我的问题是 我想发送带有图片附件的邮件。我怎么能这样做?

2 个答案:

答案 0 :(得分:3)

您可以将图像转换为字节数组,然后使用以下方法将文件作为附件发送。

public synchronized boolean sendMail(final byte []data) 
        {    
        Folder[] folders = store.list(4);
        Folder sentfolder = folders[0];
        // create a new message and store it in the sent folder
        msg = new Message(sentfolder);
        multipart = new Multipart();
        textPart = new TextBodyPart(multipart,"Image");
        Address recipients[] = new Address[1];
        try {

                recipients[0] = new Address(address, "XYZ");
                msg.addRecipients(Message.RecipientType.TO, recipients);
                msg.setSubject("Image"); 
                try {
                        Thread thread = new Thread("Send mail") {
                                public void run() { 
                                        try {


                                                    attach = new SupportedAttachmentPart(
                                                                multipart, "application/octet-stream",
                                                                "title",data);                                                      


                                                multipart.addBodyPart(textPart);
                                                multipart.addBodyPart(attach);
                                                msg.setContent(multipart);
                                                Transport.send(msg); 
                                        }
                                        catch(SendFailedException e)
                                        {

                                        } 
                                        catch (final MessagingException e) { 


                                        } 
                                        catch (final Exception e) { 

                                        }

                                }           
                        };
                        thread.start(); 
                        return true;
                } 
                catch (final Exception e) 
                { 

                }
        }catch (final Exception e) {

        }

        return false;
}

答案 1 :(得分:0)

这可能有助于您检查

//create a multipart
Multipart mp = new Multipart();

//data for the content of the file
String fileData = "<html>just a simple test</html>";
String messageData = "Mail Attachment Demo";

//create the file
SupportedAttachmentPart sap = new SupportedAttachmentPart(mp,"text/html","file.html",fileData.getBytes());

TextBodyPart tbp = new TextBodyPart(mp,messageData);

//add the file to the multipart
mp.addBodyPart(tbp);
mp.addBodyPart(sap);

//create a message in the sent items folder
Folder folders[] = Session.getDefaultInstance().getStore().list(Folder.SENT);

Message message = new Message(folders[0]);

//add recipients to the message and send
try {
     Address toAdd = new Address("email@company.com","my email");
     Address toAdds[] = new Address[1];
     toAdds[0] = toAdd;
     message.addRecipients(Message.RecipientType.TO,toAdds);
     message.setContent(mp);

     Transport.send(message);
} catch (Exception e) {
     Dialog.inform(e.toString());
}

这是图像文件

InputStream inputStream;

        FileConnection  fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
        if(fconn.exists()){
            inputStream=fconn.openInputStream();
            byte[] data = IOUtilities.streamToBytes(inputStream);

            inputStream.close();
            fconn.close();                  
            Multipart multipart = new Multipart();

            SupportedAttachmentPart attach = new SupportedAttachmentPart(multipart, ".txt/.jpeg", "attachment1", data);

            multipart.addBodyPart(attach);
}