java 以字符串形式获取 MimeMessage 内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33781094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get MimeMessage Content as String
提问by Sundaze
I have been searching and trying this so long i need your help. I have a server that gets HTML email. After that, i want to get the parts of it (header, subject and bodypart). Getting the header and subject is no problem, but the content is.
我一直在寻找和尝试这个,我需要你的帮助。我有一个接收 HTML 电子邮件的服务器。之后,我想获取它的部分(标题、主题和正文部分)。获取标题和主题没有问题,但内容是。
So here's the code where i'm getting a SmtpMessage:
所以这是我收到 SmtpMessage 的代码:
private SmsItem getEmailData(SmtpMessage msg) throws MessagingException, IOException {
String to = msg.getHeaderValue("To");
String from = msg.getHeaderValue("From");
String content = null;
LOG.info("Mail from: " + from + " to: " + to);
String toNr;
Pattern mobileNr = Pattern.compile("(\+?[0-9]+)@*");
Matcher matcher = mobileNr.matcher(to);
if (matcher.find()) {
toNr = Utils.processAddress(matcher.group(1));
} else {
throw new RuntimeException("Illegal address to send SMS from mail: " + to);
}
Pattern fromMail = Pattern.compile("\<+([[email protected]]*)\>+");
matcher = fromMail.matcher(from);
if(matcher.find()){
from = matcher.group(1);
LOG.info(from);
}else{
throw new RuntimeException("Ilegal From Address: " + from);
}
Now i'm getting the content itself:
现在我正在获取内容本身:
Session s = Session.getDefaultInstance(new Properties());
String ip = msg.toString();
ip = MimeUtility.decodeText(ip);
InputStream is = new ByteArrayInputStream(ip.getBytes("ISO-8859-1"));
MimeMessage mi = new MimeMessage(s, is);
Object message = mi.getContent();
String test = mi.getContent().toString();
test.toString();
Now I have a String that looks like this: javax.mail.internet.MimeMultipart@4edfc9bb
现在我有一个看起来像这样的字符串:javax.mail.internet.MimeMultipart@4edfc9bb
But when I write it out like this:
但是当我像这样写出来时:
((Multipart) message).writeTo(System.out);
I get this where I can see the content i want:
我得到这个,我可以看到我想要的内容:
This is a multi-part message in MIME format.--------------010600010509000401060604Content-Type: text/plain; charset=utf-8; format=flowedContent-Transfer-Encoding: 7bit
*asdasd ad asd asddfgdfgsdfsd:-)*
asdsad
javax.mail.MessagingException: Empty multipart: multipart/alternative;
at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:548)
at ...
But it can't be empty right? I'm trying to get it the content like that:
但不能为空吧?我正在尝试获取这样的内容:
if (message instanceof String)
{
message = (String)message;
LOG.info("String");
}
else if (message instanceof Multipart)
{
Multipart mp = (Multipart)message;
LOG.info("Count: " + mp.getCount() + mp.getParent() + test.toString());
for (int i = 0; i < mp.getCount(); i++){
LOG.info("Multipart");
LOG.info(i + ". " + mp.getBodyPart(i).toString());
BodyPart bp = mp.getBodyPart(i);
String dp = bp.getDisposition();
if(dp != null){
LOG.error("Something's wrong");
}else if(dp.equalsIgnoreCase("ATTACHMENT")){
LOG.error("Attachment dürfen nicht mitgegeben werden!");
}else{
content = bp.getContent().toString();
}
}
}
I really need to get this done and I'm searching on the internet for ages..
我真的需要完成这件事,而且我在互联网上搜索了很长时间..
回答by Daniel
Using commons-emailyou can get the content as follows
使用commons-email可以得到如下内容
MimeMessageParser parser = new MimeMessageParser(yourMimeMessage);
parser.parse();
String htmlContent = parser.getHtmlContent();
// or
String plainContent = parser.getPlainContent();
回答by Benny Bottema
You can use Simple Java Mail(Open Source) to convert a MimeMessage to an Email object and then access everything, including attachments and embedded images, recipients, html, text and headers:
您可以使用Simple Java Mail(开源)将 MimeMessage 转换为 Email 对象,然后访问所有内容,包括附件和嵌入图像、收件人、html、文本和标题:
Email email = EmailConverter.mimeMessageToEmail(mimeMessage);
From here you can easily get to all the various items of an email.
从这里您可以轻松访问电子邮件的所有不同项目。