java - MimeMessage.setContent() takes filename which contains html code -


I have a html.txt file that contains html code in it to send me a mail as my Java code below Should:

  MimeMessage msg = new MimeMessage (session); Msg.addHeader ("Content Type", "Text / HTML; Charset = UTF-8"); Msg.addHeader ("Format", "Flushed"); Msg.addHeader ("Content-Transfer-Encoding", "8-bit"); Msg.setFrom (new Internet address ("abc@xyz.com")); Msg.setSubject (subject, "UTF-8"); Msg.setContent (body, "text / html");  

I want to read the html.txt file above and send the mail in HTML format. How to do this

You will read your html.txt file in a string

  scanner scanner = new scanner (new file ("& html; txt; lt; pt;");); Stringbilder sb = new stringbiller (); While (scanner.hasNextLine ()) {sb.append (scanner.nextLine ()); } String body = sb.toString ();  

and then setContent method

  msg.setContent (body, "text / html; charset = UTF-8") ;  

Please note that your html.txt file contains & lt; Html & gt; , & lt; Head & gt; or & lt; Body & gt; tag.

Hope it helps.


Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -