0
votes

I write code for sending mail with attachment from my BlackBerry device to my gmail account.

The mail is sent without an error. But the problem is the attachment part is not working. The message simply doesn't contain my attachment!

Please help me to solve the issue.

Multipart mp = new Multipart(); byte[] data = readFile(strFileName); String fileData = "just a simple test"; String messageData = msgField.getText(); SupportedAttachmentPart sap= null;

        try{
            sap = new SupportedAttachmentPart(mp,"application/x-example",strFileName, data);
        }catch (Exception e) {
            Dialog.inform(e.toString());
        }
        TextBodyPart tbp = new TextBodyPart(mp,messageData); 

        mp.addBodyPart(tbp); 
        mp.addBodyPart(sap); 

        Folder[] folders = Session.getDefaultInstance().getStore().list(Folder.SENT); 

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

        try{ 
            Address toAdd = new Address(toField.getText(), toField.getText()); 
            Address[] toAdds = new Address[1]; 
            toAdds[0] = toAdd; 
            message.addRecipients(Message.RecipientType.TO,toAdds);
            message.setSubject(subjectField.getText());
            message.setContent(mp); 
            Transport.send(message); 
        }catch (Exception e){ 
            Dialog.inform(e.toString()); 
        }
2

2 Answers

2
votes

it's a complete code for send email with attachments. You can send multiple attachments a single message, just add all parts to Multipart.

 try {
        // create a multipart
        StringBuffer sbFileBody = new StringBuffer();
        Multipart mp = new Multipart();
        TextBodyPart tbp = new TextBodyPart(mp, "your message body");
        SupportedAttachmentPart sap = new SupportedAttachmentPart(mp, "text/plain", "info.txt", sbFileBody.toString().getBytes("UTF-8"));
        mp.addBodyPart(tbp);
        mp.addBodyPart(sap);

        ServiceConfiguration sc = null;
        ServiceRecord[] records = ServiceBook.getSB().getRecords();
        for (int i = 0; i < records.length; i++) {
            if (records[i].getCid().equalsIgnoreCase("CMIME") && !records[i].isDisabled() && records[i].isValid()) {
                ServiceConfiguration sct = new ServiceConfiguration(records[i]);
                String mailAddress = sct.getEmailAddress().toLowerCase();
                if (mailAddress.equals("[email protected]")) {
                    //use sc;
                    sc = sct;
                    break;
                }
            }
        }
        if (sc != null) {

            Session session = Session.getInstance(sc);
            Store store = session.getStore();
            Folder[] folders = store.list(Folder.SENT);
            Folder sentfolder = folders[0];

            if (sentfolder != null) {
                Message message = new Message(sentfolder);
                Address toAdress = new Address("[email protected]", "to address");
                message.setFrom(new Address(sc.getEmailAddress(), sc.getName()));
                message.addRecipients(Message.RecipientType.TO, new Address[] { toAdress });
                message.setSubject("Your mail subject");
                message.setContent(mp);
                message.addMessageListener(new MessageListener() {
                    public void changed(MessageEvent e) {
                        if (e.getMessage().getStatus() == Message.Status.TX_SENT) {
                            try {
                                e.getMessage().removeMessageListener(this);
                                System.out.println("Send complete");
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                });
                Transport.send(message);

            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
1
votes

you can follow this link also: j2me/BlackBerry - How to send Email with Attachment from Application?

i also got this issue while i was sending attachment i made a mistake here: msg.setContent(multipart);

so please check the code and compare with other codes as given by y0rk or as specified in the link