gpt4 book ai didi

java - 为什么我使用javamail发送到outlook账户时回复邮件是以附件形式发送的?

转载 作者:行者123 更新时间:2023-12-02 10:08:56 24 4
gpt4 key购买 nike

我尝试了多种方式使用 Outlook 帐户和 javamail api 在同一线程中获取回复,但我无法在同一线程中获取回复,而是作为附件获取。我尝试复制整个内容并保存在当前消息中,即使我作为附件获取,也尝试将内容配置更改为内联,但仍然不起作用您可以在下面找到我尝试过的代码。

    Properties properties = new Properties();
Session emailSession = Session.getDefaultInstance(properties,null);
store = emailSession.getStore("imaps");
store.connect(host,mailbox_username, mailbox_password);
folder = store.getFolder("Inbox");
folder.open(Folder.READ_WRITE);
Message[] unreadMessages = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN),false));
if(unreadMessages.size()>0)
{

for (int i = 0; i < unreadMessages.length; i++)
{
log.info("retriving message "+(i+1))
Message message = unreadMessages[i]

Address[] froms = message.getFrom();
String senderEmailAddress =(froms[0]).getAddress();

if(senderEmailAddress.endsWith("@gmail.com"))
{
subject = message.getSubject()
log.info(message.getSubject())

}
else
{ //reply to same mail here we need to reply to the message
Message message2 = new MimeMessage(emailSession);
message2= (MimeMessage) message.reply(false);
message2.setSubject("RE: " + message.getSubject());
//message2.setFrom(new InternetAddress(from));
message2.setReplyTo(message.getReplyTo());
message2.addRecipient(Message.RecipientType.TO, new InternetAddress(senderEmailAddress));
BodyPart messageBodyPart = new MimeBodyPart();
content = "some reply message"
//multipart.addBodyPart(content);
messageBodyPart.setText(content);
Multipart multipart = new MimeMultipart("related");
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
//messageBodyPart.setDataHandler(message.getDataHandler());
//bodyPart.setDataHandler(new DataHandler(ds));
//messageBodyPart.setHeader("Content-Type", "image/jpeg; name=image.jpg");
//messageBodyPart.setHeader("Content-ID", "<image>");
//messageBodyPart.setHeader("Content-Disposition", "inline");
//messageBodyPart.addBodyPart(bodyPart);
//msg.setContent(content);
messageBodyPart.setDisposition(MimeBodyPart.INLINE);
messageBodyPart.setContent(message, "message/rfc822");
messageBodyPart.setDataHandler(message.getDataHandler());
// Add part to multi part
multipart.addBodyPart(messageBodyPart);
// Associate multi-part with message
message2.setContent(multipart);
Transport t = emailSession.getTransport("smtp");
try {
t.connect(mailbox_username, mailbox_password);
t.sendMessage(message2, message2.getAllRecipients());
} finally {
t.close();
}
}
}
}

最佳答案

“内联”与“附件”只是给邮件阅读者的建议。许多人忽略了设备,或者无法内联显示所有内容类型。

如果您希望原始消息的文本出现在回复消息的正文中(例如,用“>”缩进),您需要提取原始文本并适本地重新格式化,将其添加到回复消息的文本中回复,然后将该新字符串设置为回复消息的内容。

关于java - 为什么我使用javamail发送到outlook账户时回复邮件是以附件形式发送的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55135103/

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