I have created the following string:

String emailBody = 'Dear customer, <br/><br/>Click on the following link to send reply. <a href="mailto:'+ replyMail +'?subject='+replysubject+'&body='+replyBody+'">Click Here</a> Thanks.'

When I copy and paste this string on a html file, the Click Here part is turning into a link, which when clicked, the compose window of the Live Mail is opening up with subject and body is already filled with replysubject and replyBody values. But, I want this string to be sant as an email. So, I used this code:

Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[] { emailId };
message.optOutPolicy = 'FILTER';
message.subject = emailSubject;
Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message};
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);

I am getting the email fine, but in the email body, the Click Here part is no longer a link. But when i use raw strings instead of variables, like this: <a href="mailto:'+ replyMail +'?subject=replysubject&body=replyBody">Click Here</a>, its a link.

And when I tried like this: <a href="mailto:'+ replyMail +'?subject="'+replysubject+'"&body="'+replyBody+'"">Click Here</a> , its still a link, but the replysubject and replyBodyvalues are null.

So, what am I doing wrong? How can I send an email with link in the email body?

1 Answer 1


You need to use EncodingUtil.urlEncode or PageReference to make sure the link is in a valid format. Things like spaces and special characters need to be encoded in order for HTML to recognize the markup correctly.

String emailBody = '<a href="mailto:'+ EncodingUtil.urlEncode(replyMail,'utf-8') +
    '?subject='+ EncodingUtil.urlEncode(replysubject,'utf-8')+
    '&body='+ EncodingUtil.urlEncode(replyBody,'utf-8)+
    '>Click Here</a>';
  • Thank you very much. It worked. Also, I found out that replyMail doesn't have to be encoded. It became null when I encoded it. Anyway, thank u very much for this answer. U saved my day.. Commented May 11, 2020 at 12:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .