我一直在寻找和尝试这么久我需要你的帮助。 我有一个获取HTML电子邮件的服务器。在那之后,我想得到它的部分(标题,主题和身体部分)。 获取标题和主题没有问题,但内容是。
所以这里是我获得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("\\<+([a-zA-Z0-9@.-]*)\\>+");
matcher = fromMail.matcher(from);
if(matcher.find()){
from = matcher.group(1);
LOG.info(from);
}else{
throw new RuntimeException("Ilegal From Address: " + from);
}
现在我自己获取内容:
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();
现在我有一个看起来像这样的字符串: javax.mail.internet.MimeMultipart@4edfc9bb
但是当我这样写出来的时候:
((Multipart) message).writeTo(System.out);
我在这里可以看到我想要的内容:
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 ...
但它不可能是空的吗? 我试图让它得到这样的内容:
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();
}
}
}
我真的需要完成这项工作,并且我已经在互联网上搜索了很长时间..
答案 0 :(得分:7)
使用commons-email,您可以获得如下内容
MimeMessageParser parser = new MimeMessageParser(yourMimeMessage);
parser.parse();
String htmlContent = parser.getHtmlContent();
// or
String plainContent = parser.getPlainContent();
答案 1 :(得分:1)
您可以使用Simple Java Mail(开源)将MimeMessage转换为电子邮件对象,然后访问所有内容,包括附件和嵌入图片,收件人,HTML,文本和标题:
<!DOCTYPE html>
<html lang="es">
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="styles/main.css">
<meta charset="utf-8">
<title>Apple - iPhone</title>
</head>
<body>
<ul>
<img src="images/logo.png">
<li><a href="#">iPhone</a></li>
<li><a href="#">Mac</a></li>
<li><a href="#">iPad</a></li>
<li><a href="#">iPod</a></li>
</ul>
<div id="ad">
<h1>Think different</h1>
</div>
<div id="product-container">
<div id="product" class="product2">
</div>
<div id="product" class="product1">
</div>
</div>
<div id="product-container2">
</div>
</body>
</html>
从这里,您可以轻松查看电子邮件中的所有项目。