2

I'm working on a trigger that sends an e-mail to a contact when his e-mail address is updated. The e-mail has an HTML template (so a target object id must be specified). The problem is; we also want to send an e-mail to the previous address. Is there a way to accomplish this using two e-mails (we don't want the e-mail to the old address to show the new one and vice-versa).

The code I'm using at the moment is:

if (newContact.Email != oldContact.Email){
    List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
    Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
    message.setTemplateId(emailTemplateId);
    message.setTargetObjectId(newContact.Id);
    //this line adds the old e-mail to the recipients, but this is visible to the other
    //message.setToAddresses(new String[] {oldContact.Email});
    message.setSenderDisplayName('I love hats');
    message.setReplyTo('[email protected]');
    messages.add(message);
    List<Messaging.SendEmailResult> ser = Messaging.sendEmail(messages);
}

1 Answer 1

-1

message.setToAddresses(new String[] {oldContact.Email}); Instead of above line you can take list of strings and add both the emails and add that list in the message.setToAddresses(list);

3
  • If I use this, the receiver with the old address will still see the new address and vice-versa. We don't want that. Commented Dec 19, 2013 at 10:05
  • Then Use setBccAddresses(String[]). So neither of them can view others emailId's
    – sunny
    Commented Dec 19, 2013 at 10:08
  • As far as I know I can't use BCC addresses in an e-mail that uses a template. Commented Dec 19, 2013 at 11:41

You must log in to answer this question.

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