It's all about the answers!

Ask a question

How can I display email body in bold letters


Dastan Ali (1710) | asked Aug 12 '18, 4:31 a.m.

 Hello,

 I am writing a plugin for work item save operation which is responsible for sending email to owner of task if certain conditions are met. I have successfully written plugin and tested it on my jetty plugin test environment setup. I am using below method to send email
IMailerService mailService = getService(IMailerService.class);
mailService.sendMail(mailService.getDefaultSender(), mailAddress, emailSubject, emailBody, "");

"emailBody" is string variable, content of emailBody variable are displayed in mail body when user receives email. 
Our users have demanded to make certain part of mail body in bold letters, I have been looking searching on how can I make content of string appear as bold letter in email body, one of the approach was
String emailBody  = "<b> this is mail body </b>"
But instead of displaying text in bold letters (like this - this is mail body) it is displaying entire text (i.e. <b> this is mail body) in plain letters.
Is there any way we can make content of email body in bold format?

3 answers



permanent link
Ralph Schoon (56.6k23642) | answered Aug 13 '18, 3:19 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

This seems to be a client problem and not a server or API problem. The client interprets the mail as a string and not as an HTML.


permanent link
David Lafreniere (4.4k7) | answered Aug 28 '18, 12:34 a.m.
FORUM MODERATOR / JAZZ DEVELOPER
edited Sep 06 '18, 9:11 a.m.
You are using the wrong API.
You need to create a MimeBodyPart and set the Content-type = text/html header, and then use the API that accepts a MimeMultipart.
See the following code:

IMailerService mailservice = getMailerService();

MimeBodyPart plainBody= new MimeBodyPart();
plainBody.setText(getPlainContent(), UTF_8);

MimeBodyPart htmlBody= new MimeBodyPart();
htmlBody.setText(getHtmlContent(), UTF_8);
htmlBody.setHeader("Content-type", "text/html; charset=" + UTF_8); //$NON-NLS-1$ //$NON-NLS-2$

MimeMultipart multipart= new MimeMultipart("alternative"); //$NON-NLS-1$
multipart.addBodyPart(plainBody);
multipart.addBodyPart(htmlBody);

mailer.sendMultipartMail(mailservice .getDefaultSender(), getEmailAddress(), "My email subject", multipart, null);

Comments
Rohini Kumar commented Sep 05 '18, 9:25 a.m. | edited Sep 05 '18, 9:25 a.m.
Hi David
I have tried your above mentions scenario but i am getting
Unable to save work item.javax.mail.BodyPart. error on save work item

David Lafreniere commented Sep 05 '18, 10:30 a.m.
FORUM MODERATOR / JAZZ DEVELOPER

Can you provide more of the stack trace, there's not enough information there to tell what's going wrong.


permanent link
Rohini Kumar (9213) | answered Sep 06 '18, 2:39 a.m.
edited Sep 06 '18, 8:21 a.m.

    MimeBodyPart plainBody = new MimeBodyPart();
        plainBody.setText("test", "UTF_8");

        MimeBodyPart htmlBody= new MimeBodyPart();
        htmlBody.setText("<b>test<\b>", "UTF_8");
        htmlBody.setHeader("Content-type", "text/html; charset=" + "UTF_8"); //$NON-NLS-1$ //$NON-NLS-2$

        MimeMultipart multipart= new MimeMultipart("alternative"); //$NON-NLS-1$
        multipart.addBodyPart(plainBody);
        multipart.addBodyPart(htmlBody);
    
    //
        String emailSubject = "Work item " + workItemID + " filed against " + categoryName + " in project area " + projectAreaName + " needs your attention";
        
        
        for(ITeamAreaHandle teamAreaHandle : teamAreaHandles)
        {
            ITeamArea teamArea = (ITeamArea)itemService.fetchItem(teamAreaHandle, IRepositoryItemService.COMPLETE);
            IContributorHandle[] memberHandles = teamArea.getMembers();
            if(memberHandles.length > 0)
            {
                for(IContributorHandle memberHandle : memberHandles)
                {
                    IContributor teamMember= (IContributor)itemService.fetchItem(memberHandle, IRepositoryItemService.COMPLETE);
                    String mailAddress = teamMember.getEmailAddress();                    
                    //send mail to user
                    //mailService.sendMail(mailService.getDefaultSender(), mailAddress, emailSubject, emailBody, "");
                    
                    //Using Multi-part Service here
                    //mailService.sendMultipartMail(mailService.getDefaultSender(), mailAddress,"test", mp, null);
                    mailService.sendMultipartMail(mailService.getDefaultSender(), mailAddress, emailSubject, multipart,"");

                }
          }

   }

I wrote  this code but its giving me error as mentioned above
please correct me


Comments
Rohini Kumar commented Sep 06 '18, 6:41 a.m.

please give me a suggestions on same


David Lafreniere commented Sep 06 '18, 9:09 a.m.
FORUM MODERATOR / JAZZ DEVELOPER

We're still going to need the exception stack trace.
There's not enough information in the statement of
"Unable to save work item.javax.mail.BodyPart".


Rohini Kumar commented Sep 07 '18, 8:43 a.m.

hi David I am unable o get stack trace data, I am stuck in to send html mail,can you please provide sample example related to it


David Lafreniere commented Sep 07 '18, 12:32 p.m.
FORUM MODERATOR / JAZZ DEVELOPER
The code provided in my comment was the sample example, I do not know what other code to provide other than that. Hopefully some other readers might be able to help out here.

Why can't you get the stack trace? It should be either in the server log or client log somewhere. Are you able to debug the issue to see what's happening?

Also, is that really the title that is given for the error? It's not even a grammatically correct sentence so I'm not sure how to understand it: "Unable to save work item.javax.mail.BodyPart"

Rohini Kumar commented Sep 08 '18, 7:27 a.m.

hi Devid, When I add above mentioned cod then I unable to debug the code ,I am unable to trigger code


David Lafreniere commented Sep 10 '18, 8:35 a.m.
FORUM MODERATOR / JAZZ DEVELOPER

Did you remember to add the appropriate imports?
Imports at the top of the java file, and updated 'requiredService' in the plugin.xml?


Rohini Kumar commented Sep 11 '18, 2:36 a.m.

Thank you Devid for quick response, now code is working fine.

showing 5 of 7 show 2 more comments

Your answer


Register or to post your answer.