2

I have a custom button which will send a SingleEmailMessage and below is my SingleEmailMessage code

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new string[] {'[email protected]'});
mail.setCcAddresses(new string[] {'[email protected]'});
mail.setSubject('Testing Subject');
mail.setPlainTextBody('Testing Body');
mail.setReplyTo('[email protected]');
mail.setSenderDisplayName(UserInfo.getUserEmail());
Messaging.SendEmailResult[] res = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

Everything works fine but can I not to send this email to user under CcAddresses?

I wan userA to receive the email but not CC_User, so when userA pick up the email and reply, he will see reply To [email protected] and CC to [email protected]

Thanks

2 Answers 2

4

I don't think email can work like that, regardless of if you are using Salesforce or not. Either the CC_User is specified in the CC addresses or they aren't.

Instead, you could try building up a mailto: hyperlink in the email body that would populate the to and cc addresses as required.

4
  • 1
    I imagine in a crazy hardcore SMTP admin world, you might be able to "spoof" mail headers to achieve this curious sounding result... but I have to totally agree with @DanielBallinger on this one, you cannot feign the delivery of an e-mail to an address, it would undermine too much of the integrity of e-mail (just think, you could claim and almost prove you sent a user an e-mail that they would never have got!). Commented May 6, 2014 at 10:28
  • The base email class that is extended by both the SingleEmailMessage and MassEmailMessage classes has a replyTo field which would be used by the email client on reply but I am not sure if this field supports multiple SMTP addresses within it.
    – Mark Pond
    Commented May 6, 2014 at 16:28
  • i cant really set mailto: hyperlink in the body, the reason is we have a custom email client which will received the email and is required to used to reply the email and if we click on the mailto: hyperlink it will launches Outlook which we do not want to reply from outlook Commented May 7, 2014 at 1:51
  • In that case I think you will need to send out the CC in the first email so it will appear in the reply as well. Commented May 7, 2014 at 1:53
1

The only way I know of to do what you're attempting would be if CC_User's email address was set using a custom setting. If you go that route, I'd also recommend adding an if statement to allow you to turn the CC address on and off using a boolean setting. This of course assumes CC_User remains the same. If not, you'd need to set this up to handle several different custom settings email addresses. You'd want to do something like the following:

CustSttng_CC__c CCUser = CustSttng_CC__c.getValues('CCUser1');
CustSttng_CCBool__c CCBool = CustSttng_CCBool__c.getValues('BCC1');
    if(BCC1.SendCC__c == true){ 
        string[] ccAddresses = new string[] {string.valueOf(CCUser1.Email__c)};
    }

To set the replyTo to [email protected], you may want to set up an inbound mail service that's been configured by UserB to help facilitate that, but that could just be a matter of preference. The setReplyTo method is one of the Base Email Class methods and isn't specific to the Single Email Message Class.

In general, using your own Mail handler class would probably be good idea for what you're trying to accomplish. You could easily build it as a method that exists inside your inbound mail handler class.

Edit

SF isn't going to allow you to continue to send email to an address that either doesn't exist or that gives the appearance of sending email to an address without sending it. What you'll want to do for CCUser is create dummy email account someplace so CC_User will appear on your outbound email (I presume that's your purpose).

Send the email to an unattended inbound email handler account where only email from your outbound email address is accepted. Set it up so email from any other address, such as one of your recipients, receives an auto-reply of some kind stating the address isn't monitored.

You must log in to answer this question.

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