I have used the following code for sending the mail with template. but I don't want to send the mail to owner.

 List<String> toAddress = new List<String>();
        toAddress.add(my email address);

If we set setTargetObjectId, the mail send to owner. So, how to avoid to send the mail to owner that?


@Lemon, You want to follow the code example found here:

Using APEX to assemble HTML Letterhead Emails

Essentially, by wrapping the sendEmail call in a db transaction that you immediately rollback, you end up with a Merged template that you can inject into a send without using setTargetObjectId.

I've taken the liberty of writing up this as a class, as part of my upcoming "highly opinionated standard library."

public class mailUtils {
  public class mailUtilsException extends exception {}

  public Boolean useSig {get; private set;}
  public Boolean saveActivity {get; private set;}
  public String senderDisplayName {get; private set;}

  public mailUtils(Boolean useSig, Boolean saveActivity, String senderDisplayName){
    this.useSig = usesig;
    this.saveActivity = saveActivity;
    this.senderDisplayName = senderDisplayName;

  // Derived from: 
  // https://salesforce.stackexchange.com/questions/13/using-apex-to-assemble-html-letterhead-emails/8745#8745
  public Messaging.SingleEmailMessage MergeTemplateWithoutSending(Id targetObjectId, Id templateId) {
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    // Intentionally set a bogus email address.
    String[] toAddresses = new String[]{'invalid@emailaddr.es'};

    // create a save point
    Savepoint sp = Database.setSavepoint();
    // Force the merge of the template.
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    // Force a rollback, and cancel mail send.

    // Return the mail object
    // You can access the merged template, subject, etc. via:
    // String mailTextBody = mail.getPlainTextBody();
    // String mailHtmlBody = mail.getHTMLBody();
    // String mailSubject = mail.getSubject();
    return mail;


  public static Map<String,String> getMergedTemplateForObjectWithoutSending(Id targetObjectId, Id templateId, Boolean useSig, Boolean saveActivity, String senderDisplayName) {
    Map<String,String> returnValue = new Map<String,String>();
    mailUtils mu = new mailUtils(useSig, saveActivity, senderDisplayName);
    Messaging.SingleEmailMessage mail = mu.MergeTemplateWithoutSending(targetObjectId, templateId);
    returnValue.put('textBody', mail.getPlainTextBody());
    returnValue.put('htmlBody', mail.getHTMLBody());
    returnValue.put('subject', mail.getSubject());
    return returnValue;

| improve this answer | |

Not sure what you mean by default email code.. but my guess is you can use the setToAddresses method to specify the email address to whom you want to send


This is just a sample code for reference

        EmailTemplate tmpl = [SELECT Id, Subject, HtmlValue, Body FROM EmailTemplate WHERE DeveloperName = 'YourTemplateName' limit 1];
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();        
        List<String> toAddress = new List<String>();
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
| improve this answer | |
  • Thanks. But if we set the targetobject id, the mail sent to owner. so i want to prevent. can you help? – Lemon Sep 2 '14 at 10:49

As per documentation:

setTargetObjectId(ID) Required if using a template, optional otherwise. The ID of the contact, lead, or user to which the email will be sent. The ID you specify sets the context and ensures that merge fields in the template contain the correct data.

This seems like its designed to work like that.

If you want to send email other email id then you will not be able to use template.

One work around is define HTML email body instead of template and then use setToAddresses

| improve this answer | |
  • If we put the settargetobjectid, user will get the mail but I dont want to send that mail how to protect that? – Lemon Sep 3 '14 at 5:28
  • Have you read and understood my answer? settargetobjectid designed for that and you cant prevent that that. you will need to write your own code as said above. – AtulRajguru9 Sep 3 '14 at 5:34
  • Yes. but I need to prevent that so is there any hack for that? – Lemon Sep 3 '14 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.