Search code examples
javaspring-mvcjakarta-mailmime-message

Spring 3 MVC: Display java mimemessage emails as html


I did some searching around, and while I could find plenty of good posts on how to send emails in Java, I couldn't quite find a good jumping off point for displaying them. Here's the deal, my Spring 3 web-app has just received an object of type,

javax.mail.internet.MimeMessage

(More accurately it just built one that is now read to send.)

How would I go about displaying a preview of that message in HTML? I know that I could just keep track of everything that get puts into it, but there are header and footer, signature, etc. components that get built in beyond my reach.


Solution

  • If your concern is iterating through a non-trivial number of fields, why not use an ObjectMapper? For example, an org.springframework.web.servlet.view.json.MappingJacksonJsonView could send JSON data to the UI, and would also keep UI in charge of rendering of that data.

    You can pass this to the ``:

    <bean id="contentNegotiatingViewResolver"
        class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"
        p:defaultContentType="text/html">       
        <property name="mediaTypes">
            <map>
                <entry key="xml" value="application/xml" />
                <entry key="json" value="application/json" />
                <entry key="${mimeKey}" value="${mimeMessageType} />
            </map>
        </property>
        <property name="defaultViews">
            <list>
                <bean
                    class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"
                    p:objectMapper-ref="objectMapper" />
                <bean class="org.springframework.web.servlet.view.xml.MarshallingView"
                    p:marshaller-ref="objectMarshaller" />
            </list>
        </property>
        ...
    </bean>