I will focus on the part of converting your jsp to text.
There were some comments about the usage of jsp as templeting engine, but my suggestion is templeting agnostic.
The idea is to mimic a browser call, i.e. submit the form that we would like its result to be sent via email and make sure that we can access this page correctly via a browser.
I am assuming post request here change to get is easy.
public class MailServiceHelper {
public String getJsp(String url, Map<String,String> form, HttpServletRequest request) {
//we can figure out the base url from the request
String baseUrl ="";
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(baseUrl+"/"+url);
for (String formElement : form.keySet()) {
method.setParameter(formElement, form.get(formElement));
}
try {
int statusCode = client.executeMethod(method);
if (statusCode >= HttpStatus.SC_OK && statusCode < HttpStatus.SC_MULTIPLE_CHOICES) {
byte[] responseBody = method.getResponseBody();
return new String(responseBody,StandardCharsets.UTF_8);
}else{
throw new RuntimeException("Failed to read jsp, server returened with status code: "+statusCode);
}
} catch (Exception e) {
throw new RuntimeException("Failed to read jsp",e);
}finally{
method.releaseConnection();
}
}
You may require authenticating your client which is supported as well.
I am using httpclient 3.1 in my exanple see http 3.1 authentication ;switching to newer client should be easy.
Sending the HTML by email is referred to in the answer below: How do I send an HTML email?
It’s recommended to use internal Style Sheet so the email client will render the html correctly.