Solved

Quartz SendMailJob and HTML Format

Posted on 2013-01-28
7
1,017 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
  • 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 26

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 26

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

707 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now