无法发送带附件的邮件

时间:2011-03-31 08:17:12

标签: java blackberry sendmail

我编写的代码用于将带有附件的邮件从我的BlackBerry设备发送到我的Gmail帐户。

邮件发送时没有错误。但问题是附件部分不起作用。该邮件根本不包含我的附件!

请帮我解决这个问题。

Multipart mp = new Multipart();             byte [] data = readFile(strFileName);             String fileData =“只是一个简单的测试”;             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 个答案:

答案 0 :(得分:2)

这是发送带附件的电子邮件的完整代码。您可以在一条消息中发送多个附件,只需将所有部分添加到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("from@address.com")) {
                    //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("to@address.com", "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 :(得分:1)

您也可以点这个链接: j2me/BlackBerry - How to send Email with Attachment from Application?

我在发送附件时也遇到了这个问题 我在这里犯了一个错误:msg.setContent(multipart);

所以请检查代码并与y0rk或链接

中指定的其他代码进行比较
相关问题