无法发送带附件的邮件

发布于 2024-10-28 15:21:54 字数 1261 浏览 5 评论 0原文

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

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

请帮我解决这个问题。

多部分 mp = new Multipart(); byte[] 数据 = readFile(strFileName); String fileData = "只是一个简单的测试"; String messageData = msgField.getText(); 支持的附件部分 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()); 
        }

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()); 
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咆哮 2024-11-04 15:21:54

这是发送带有附件的电子邮件的完整代码。您可以在一条消息中发送多个附件,只需将所有部分添加到 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();
    }

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();
    }
丢了幸福的猪 2024-11-04 15:21:54

您也可以点击此链接:
j2me/BlackBerry - 如何从应用程序发送带有附件的电子邮件?

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

因此,请检查代码并与 y0rk 给出的或链接中指定的其他代码进行比较

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文