+ Start a Discussion
Jayesh Babu A VJayesh Babu A V 

How to send email from salesforce apex class with a link in the email body?

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?
AnudeepAnudeep (Salesforce Developers) 
Hi Jayesh, 

Mailto is generally used in the below format. Can you try using the below format and see if you are able to send an email with the link the email body
<a href="mailto:john.smith@acmecorp.com">john.smith@acmecorp.com</a> 
<a href="mailto:john.smith@acmecorp.com">John Smith</a>
<a href="mailto:homer@example.com">Email Homer</a>
Reference: http://https://help.salesforce.com/apex/HTViewSolution?urlname=How-do-I-add-an-email-link-in-an-email-template-1327109106048&language=en_US

And if you are using bind variables, the string should be used like this example below
email.setHtmlBody('Hello, <br/><br/>This is the test mail that you generated. <br/>The Email Id for which this mail was generated by '+toMail+'<br/><br/>Regards<br/> Developer');


Jayesh Babu A VJayesh Babu A V
thank u for ur reply. But, I didnt understand ur answer. What if I want to add a subject and body to the mail?
Jayesh Babu A VJayesh Babu A V
I got the answer: https://salesforce.stackexchange.com/a/305616/82119 . Thank u for your reply.