• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

send email in java with HTML/JSON

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to send email in java with the following specifications?

HTML template : Indicating the structure of email. It should take in JSON document to fill in the dynamic parameters
that need to be filled in during the run time

Java program should apply the JSON to HTML template, generate the email and send email

I am currently using RTF templates with XML. I want to use JSON, HTML and Java.

Can you suggest some links to refer to, if possible suggest the design and sample code
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

There are many tutorials online which give you examples of how to use JavaMail to send e-mail messages from Java code. (Google keywords: "JavaMail tutorial".)

I don't understand how you apply JSON to an HTML template, or what that means, but to send the result in e-mail you need that process to produce the text which will be the body of the e-mail. Or you can also send files as attachments, so perhaps your JSON-applying process might be producing some kind of files.
 
sire anna
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. So now I need a java program which applies dynamic parameters that are in json file (like name, product) to HTML template.

Java Programe has to take in the html template name(eg: order_place) load it into  memory from file system, replace its parameters like (name/product) with the real values from JSON

At the run time these params(name, product..) , JSON file contains parameters that user choose.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to split your problem in two:
1) Take a template and JSON, and turn it into HTML.

2) Send an email with the HTML as body.


For the first you can check out template frameworks like Velocity, FreeMarker or Thymeleaf. They don't work with JSON, but they do work with a context or data-model. You can relatively easily turn the JSON into a Map, which can be turned into a context / data-model (for FreeMarker, the Map is the data-model).

For the second I can recommend Apache Commons Email. It makes it easier to work with JavaMail (and is safer - it can prevent CRLF injection in subjects).
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:You need to split your problem in twothree:
1) Read JSON into a Java Object Model (e.g., a Map).

2) Take an HTML template and the object model JSON, and generate HTML (using one of the template engines Rob has recommended or some equivalent.)

2) Send an email with the HTML as body.



Yeah, I'm being pedeantic. But I'd rather avoid the "??? / Profit!" approach.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic