简体   繁体   中英

Getting email text from ImageHtmlEmail

We are using apache commons mail, specifically the ImageHtmlEmail. We really would like to log every email sent - exactly as it will be sent - in the perfect world it would be something you could paste into sendmail - with all headers and other information included.

This is primarily to troubleshoot some problems we've been having with it turning up as text/plain rather than text/html - but also because it would be nice to have a record of exactly what the system sent out stored in our logs.

So essentially - the dream is a function that would take an ImageHtmlEmail and return a string - as it will be sent . I know I could render it into a string myself, but then I'm bypassing whatever is being done in the library function, which is what we really want to capture. I tried BuildMimeMessage and then getMimeMessage, which I think is probably the correct first step - but that just leaves me with the question of how to turn a mimemessage into a string.

I have a sort of solution - but would love a better one:

/**
 * add content of this type
 *
 * @param builder
 * @param content
 */
private static void addContent(final StringBuilder builder, final Object content)
{
    try
    {
        if (content instanceof MimeMultipart)
        {
            final MimeMultipart multi = (MimeMultipart) content;
            for (int i = 0; i < multi.getCount(); i++)
            {
                addContent(builder, ((MimeMultipart) content).getBodyPart(i));
            }
        }
        else if (content instanceof MimeBodyPart)
        {

            final MimeBodyPart message = (MimeBodyPart) content;
            final Enumeration<?> headers = message.getAllHeaderLines();
            while (headers.hasMoreElements())
            {
                final String line = (String) headers.nextElement();
                builder.append(line).append("\n");
            }
            addContent(builder, message.getContent());
        }
        else if (content instanceof String)
        {
            builder.append((String) content).append("\n");
        }
        else
        {
            System.out.println(content.getClass().getName());
            throw CommonException.notImplementedYet();
        }
    }
    catch (final Exception theException)
    {
        throw CommonException.insteadOf(theException);
    }

}

/**
 * get a string from an email
 *
 * @param email
 * @return
 */
public static String fromHtmlEmail(final ImageHtmlEmail email)
{
    return fromMimeMessage(email.getMimeMessage());
}

/**
 * @param message
 * @return a string from a mime message
 */
private static String fromMimeMessage(final MimeMessage message)
{
    try
    {
        message.saveChanges();
        final StringBuilder output = new StringBuilder();
        final Enumeration<?> headers = message.getAllHeaderLines();
        while (headers.hasMoreElements())
        {
            final String line = (String) headers.nextElement();
            output.append(line).append("\n");
        }
        addContent(output, message.getContent());
        return output.toString();
    }
    catch (final Exception theException)
    {
        throw CommonException.insteadOf(theException);
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM