将MimeMessage内容作为字符串获取

问题描述 投票:1回答:2

我一直在寻找和尝试这么久我需要你的帮助。我有一个获取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("\\<+([[email protected]]*)\\>+");
        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();
                }
            }
        }

我真的需要完成这项工作,而且我在互联网上搜索了很多年。

java email smtp multipart mime-message
2个回答
7
投票

使用commons-email,您可以获得如下内容

MimeMessageParser parser = new MimeMessageParser(yourMimeMessage);
parser.parse();

String htmlContent = parser.getHtmlContent();
// or
String plainContent = parser.getPlainContent();

1
投票

您可以使用Simple Java Mail(开源)将MimeMessage转换为电子邮件对象,然后访问所有内容,包括附件和嵌入图像,收件人,html,文本和标题:

Email email = EmailConverter.mimeMessageToEmail(mimeMessage);

从这里,您可以轻松地查看电子邮件中的所有项目。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.