It's all about the answers!

Ask a question

How to set content type to HTML "text/html" when using sendMail in IMailer service while using RTC extensions

anup Gaur (1391944) | asked Nov 03 '15, 10:35 a.m.
edited Nov 03 '15, 12:24 p.m.
I have written custom code using RTC extensions to send emails for different scenarios in my defect workflow.
I want to use the IMailer service sendmail() to do this. Currently I am using native javax mail api to do this.

a) I'm sending emails with custom HTML content.
b) I'm able to use sendMail() of IMailerservice to sendemails also. E.g sendmail line : 
MymailerService.sendMail(sender, toEmail, newsubject, contentStr, "");

I'm having trouble in setting the contentType to HTMl when using sendMail.

any suggestions / help is highly appreciated.

Thank you

Accepted answer

permanent link
Kevin Ramer (4.5k8180199) | answered Nov 03 '15, 12:34 p.m.

anup Gaur selected this answer as the correct answer

anup Gaur commented Nov 03 '15, 3:18 p.m.
Hi Kevin,
Thanks for that link. That was exactly the code I was looking for.
Thanks a lot.

One other answer

permanent link
Kevin Ramer (4.5k8180199) | answered Nov 03 '15, 11:15 a.m.
edited Nov 03 '15, 11:15 a.m.
Not an rtc question, really, but....

You need to add headers to the MimeMessage object

            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(from));
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
            msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc,false));
             msg.setHeader("X-Mailer", "JavaMail");
            msg.setHeader("Content-Type", "text/html");
            msg.setSentDate(new Date());

anup Gaur commented Nov 03 '15, 11:48 a.m. | edited Nov 03 '15, 11:55 a.m.
 Hi Kevin
Thanks for the answer. But this is the code that I'm already using and I'm able to send Emails in HTML format. 
I'm moving my code so as to use the RTC API IMailerService to send emails using the sendEmail() method call. 
Sample code is given below.
IMailerService MymailerService = getService(IMailerService.class);
MailSender sender = MymailerService.getDefaultSender();
MymailerService.sendMail(sender, toEmail, newsubject, mailBodyContent, ""); 
} else {
System.out.println("RTC Mail Service is Not ENABLED!!");
I'm migrating my code from native Java code, 
so I can reuse the JTS email settings without having to set SMTP HOST, Sent From etc. 
I was not able to find HTML header or MIME settings in sendMail RTC method.

Kevin Ramer commented Nov 03 '15, 12:12 p.m.

Well, users are able to elect format of the email sent to them via JTS in their User Profile.  Not sure "registered" users are your target, but that could be why html doesn't come out the back end.

anup Gaur commented Nov 03 '15, 12:48 p.m.

 Yes, thats a good direction to look at. Although I'm not using any existing templates or anything to send the email. The code example given above is pretty much it. I was trying to find a setting like setContentType, setMimeType, or setHeader to HTML type.

Also in the sendMail method, on of the argument is the content. This is the email body which is a string type. Sending HTML tags here also doesn't resolve the issue. Somewhere we need to specify the content type as HTML

Your answer

Register or to post your answer.