Solved

Quartz SendMailJob and HTML Format

Posted on 2013-01-28
7
1,029 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Passing list of object to Oracle Database Procedure 3 59
Modeling a class in java 5 46
Non-recursive backtracking, using a stack 1 98
map related example 6 30
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

914 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

18 Experts available now in Live!

Get 1:1 Help Now