如何修改现有的Java邮件MimeMessage正文部分?
我正在尝试修改现有的 MimeMessage 正文部分。我想过滤某些链接。你们中有人知道为什么即使正文部分内容接缝要更改,消息也会以旧内容发送吗?是否正在进行一些缓存?知道如何解决这个问题吗?
这是我的代码:
public void resend(InputStream data) throws Exception {
Session mailSession = createMailSession();
//mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession, data);
Object content = message.getContent();
if (content.getClass().isAssignableFrom(MimeMultipart.class)) {
MimeMultipart mimeMultipart = (MimeMultipart) content;
for (int i = 0; i < mimeMultipart.getCount(); i++) {
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
if (bodyPart.getContentType().startsWith("text/plain")) {
String cnt = updateContent((String) bodyPart.getContent());
System.out.println("ContentType = " + bodyPart.getContentType());
System.out.println("Content = " + cnt);
bodyPart.setContent(cnt, bodyPart.getContentType());
} else if (bodyPart.getContentType().startsWith("text/html")) {
String cnt = updateContent((String) bodyPart.getContent());
System.out.println("ContentType = " + bodyPart.getContentType());
System.out.println("Content = " + cnt);
bodyPart.setContent(cnt, bodyPart.getContentType());
}
}
} else {
String cnt = updateContent((String) message.getContent());
System.out.println("ContentType = " + message.getContentType());
System.out.println("Content = " + cnt);
message.setContent(cnt, message.getContentType());
}
transport.connect();
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
}
private String updateContent(String cnt) {
return cnt.replace("www.xyz.pl", "www.new-xyz.pl");
}
输入流“数据”包含原始消息。
有什么想法吗?
提前致谢....
I am trying to modify existing MimeMessage body part. I would like to filter certain links. Does any of you know why even though body part content seams to be changed message is sent with old content? Is there some caching going on? Any idea how to solve this?
Here is my code:
public void resend(InputStream data) throws Exception {
Session mailSession = createMailSession();
//mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession, data);
Object content = message.getContent();
if (content.getClass().isAssignableFrom(MimeMultipart.class)) {
MimeMultipart mimeMultipart = (MimeMultipart) content;
for (int i = 0; i < mimeMultipart.getCount(); i++) {
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
if (bodyPart.getContentType().startsWith("text/plain")) {
String cnt = updateContent((String) bodyPart.getContent());
System.out.println("ContentType = " + bodyPart.getContentType());
System.out.println("Content = " + cnt);
bodyPart.setContent(cnt, bodyPart.getContentType());
} else if (bodyPart.getContentType().startsWith("text/html")) {
String cnt = updateContent((String) bodyPart.getContent());
System.out.println("ContentType = " + bodyPart.getContentType());
System.out.println("Content = " + cnt);
bodyPart.setContent(cnt, bodyPart.getContentType());
}
}
} else {
String cnt = updateContent((String) message.getContent());
System.out.println("ContentType = " + message.getContentType());
System.out.println("Content = " + cnt);
message.setContent(cnt, message.getContentType());
}
transport.connect();
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
}
private String updateContent(String cnt) {
return cnt.replace("www.xyz.pl", "www.new-xyz.pl");
}
Input stream "data" contains raw message.
Any ideas?
Thanks in advance....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了更新 text/plain 和 text/html 部分,我使用了 Jsoup 提供的功能
To update both the text/plain and text/html sections, I used functionalities provided by Jsoup
您需要在 MimeMessage 上调用 saveChanges() (据我所知应该足够了),另请参阅: api-doc MimeMessage#saveChanges():
You need to call saveChanges() on the MimeMessage (which as far as I know should be sufficient), see also: api-doc MimeMessage#saveChanges():