gpt4 book ai didi

java - 无法发送带附件的邮件

转载 作者:行者123 更新时间:2023-12-02 00:44:00 25 4
gpt4 key购买 nike

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

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

请帮我解决这个问题。

Multipart mp = new Multipart(); byte[] 数据 = 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());
}

最佳答案

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

关于java - 无法发送带附件的邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5496980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com