Solved

Quartz SendMailJob and HTML Format

Posted on 2013-01-28
7
1,074 Views
Last Modified: 2013-02-05
Hi All,

I am using the Quartz SendMailJob to send a report by email. My requirement is to send the Report in HTML format. However, MS Outlook is not recognizing the format and displaying HTML text as is.

My method is as below:

      private void sendEmail(JobExecutionContext arg0, String jobMessage) {
            if(arg0.getMergedJobDataMap().containsKey("recipient")){
                  //send an e-mail to the recipient
                  try {
                        arg0.getMergedJobDataMap().put("message", jobMessage);
                        arg0.getMergedJobDataMap().put("contentType", "text/html");
                        
                        SendMailJob mailJob = new SendMailJob();
                        mailJob.execute(arg0);
                        logger.info(jobMessage);
                  } catch (Exception e) {
                        e.printStackTrace();
                        StringBuilder message = new StringBuilder();
                        message.append("Could not send email to ");
                        message.append(arg0.getMergedJobDataMap().getString("recipient"));
                        logger.info(message);
                  }
            }
      }
0
Comment
Question by:somaghoshCU
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 38827626
Please post an example (obfuscated if necessary) of a mail body
0
 

Author Comment

by:somaghoshCU
ID: 38827701
Here it is:

      StringBuilder html = new StringBuilder();
            html.append("<html>");
            html.append("<head><h1>Test Report</h1></head>");
            html.append("<body>");
            html.append("<table cellpadding='10'>");
            html.append("<th align='left'>Reference ID</th><th align='left'>Origination Date</th><th align='left'>Previous Status</th>");
            html.append("<tr>");
            html.append("This is the Report Body");
            html.append("</tr>");
            html.append("</table>");
            html.append("</body>");
            html.append("</html>");
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 38828010
That's certainly ok if the headers have been set correctly. I'm not convinced they have but unfortunately the api docs that i'm able to see are decidedly thin when it comes to comment/explanations:
http://quartz-scheduler.org/api/2.0.0/org/quartz/jobs/ee/mail/SendMailJob.html
If you know of any better ones, let me know.
Perhaps you ought to be using http://quartz-scheduler.org/api/2.0.0/org/quartz/jobs/ee/mail/SendMailJob.html#PROP_CONTENT_TYPE but i'm not sure. If you can find a link to the source, post that too please.

Otherwise, someone who's actually used this api might come along...
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 27

Expert Comment

by:mrcoffee365
ID: 38828177
You're not appending the html as a mime attachment.  That will almost certainly fix the problem.
0
 

Author Comment

by:somaghoshCU
ID: 38828298
mrcoffee365,

Would you give an example as the current documentation does not show one. It will be really helpful!
0
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 200 total points
ID: 38829064
The current document has 2 calls relating to the mime message attachment.  This person shows an example (with Velocity and Spring, but you can ignore those parts):
http://shahmukesh.wordpress.com/2010/12/02/spring-integration-with-quartz-for-sending-bulk-e-mails/

He doesn't mention it, but you should try setting the content-type for your email to "text/html" to see if that helps with your current implementation.

To see the api, here:
http://quartz-scheduler.org/api/2.0.0/org/quartz/jobs/ee/mail/SendMailJob.html

We don't use Quartz to send email, but apparently it's a fairly simple-minded interface.  They expect you to write your own implementation of it if you have additional needs.  See this discussion here:
http://stackoverflow.com/questions/12517219/example-using-quartz-sendmailjob
0
 

Author Comment

by:somaghoshCU
ID: 38855863
I am posting my implementation for reference. Thank you mrcoffeee365 and CEHJ!

Extend the SendMailJob and implement the following:

protected void setMimeMessageContent(MimeMessage mimeMessage, MailInfo mailInfo) throws MessagingException {
            final MimeBodyPart textPart = new MimeBodyPart();
              textPart.setText("This is the text");

            // HTML version
             final MimeBodyPart htmlPart = new MimeBodyPart();
            htmlPart.setContent("<html><body>...</body></html>", "text/html");
            htmlPart.setFileName("test.html");

            // Create the Multipart.  Add BodyParts to it.
            final Multipart mp = new MimeMultipart();
            mp.addBodyPart(textPart);
            mp.addBodyPart(htmlPart);

            // Set Multipart as the message's content
            mimeMessage.setContent(mp);
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unhandled exception type Exception 18 65
jsp login check 12 67
Convert from a json string array to a Java object 3 81
ejb entity bean example issue 2 43
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question