0

I have a static HTML page which gets generated after some operation. The HTML file contains iframe, external CSS, JS, XML, HTML files. How do I send the exact rendered HTML in the mail using Java mail API? I know I can type the HTML like

message.setContent("<html><body><h1>This is actual message</h1></body></html>","text/html" ); 

But I want to send an html page like

message.setContent("index.html","text/html" );

index.html contains external css, js, xml files.

saurav
  • 359
  • 1
  • 6
  • 18
  • possible duplicate - https://stackoverflow.com/questions/20637548/send-html-email-including-css-style-sheet-via-php – second Aug 06 '19 at 12:05
  • Are there actual email clients that load external CSS, and even load and execute JS? To have good compatibility with Mail clients I think you'll have to inline CSS and not use JS at all in html emails. – cello Aug 06 '19 at 12:05

1 Answers1

0

You can set the message content from a file like this:

message.setDataHandler(new DataHandler(
            new FileDataSource("index.html", "text/html")));

Note that many mail readers will not access the external css or images when displaying the message.

The JavaMail FAQ has several entries related to sending html messages, starting here. This guide to css support in email clients will also help.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40