+ Start a Discussion

Emails to "TO" and "CC"

Hello All,

                 We have a registration system where the user receives Registration email upon registration.  Here we used Visualforce email Template. Is it possible, if the registrant do not want to receive email after the registration and he wants email to be sent only to CC address.


Here is code that is used to send emails.. I m posting only the part of code that is used for emails...


Here before calling sendEventConfirmation i am changing the email of the contact to cc emailaddress and after email is sent i am resetting back to old email.  But still the email is reaching both To and CC addresses...



 private void sendConfirmation() {
         if (this.contact.IsPersonAccount || this.registrantIsDoctor) {
             this.oldemail = this.email;
             this.account.PersonEmail =  this.reg.CC_Email__c;
             this.contactId = RegistrantPersister.persistAccount(this.account);
         } else{
                 this.oldemail = this.email; 
                 this.contact.Email = this.email;
                 this.contactId = RegistrantPersister.persistContact(this.contact);
        RegistrationEmailSender.sendEventConfirmation(this.eventId, this.reg.Id, this.contactId, this.reg.CC_Email__c, this.event.Planner_Email__c, testingEmail);
         if (this.contact.IsPersonAccount || this.registrantIsDoctor) {
                 this.account.PersonEmail =  this.oldemail;
                 this.contactId = RegistrantPersister.persistAccount(this.account);
         } else{
                 this.contact.Email = this.oldemail;
                 this.contactId = RegistrantPersister.persistContact(this.contact);






 webservice static Boolean sendEventConfirmation(Id eventId, Id registrantId, Id contactId, String ccEmailAddress, String plannerEmailAddress, Boolean testing) {
        Boolean emailSent = false;
        // Have to query to get the Id of the template and From address
        EmailTemplate et = [select Id from EmailTemplate where DeveloperName = 'Event_Registration_Confirmed' limit 1];
        OrgWideEmailAddress oe = [Select Id From OrgWideEmailAddress where DisplayName = 'Events' and Address = 'gem.meetings@events.com' limit 1];
        // Load attachments
        Integer totalAttachmentSize = 0;
        Attachment[] atts = [select id, name, body, bodylength from attachment where parentid = :eventId and name not in ('header', 'footer')];
        List<Messaging.EmailFileAttachment> fileAttachments = new List<Messaging.EmailFileAttachment>();
        for(Attachment a : atts) {
            totalAttachmentSize += a.bodylength;
            Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment();
        string email1 = [select id,email from contact where id = :contactId][0].email;
        system.debug('this is email id that is changed------------------>>>>>>>'+email1);
        // Build mail sending object
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        // Sender
        // Recipient(s) and Content
        if (ccEmailAddress != null && ccEmailAddress != '') {
            List<String> ccAddresses = new List<String>();
        if (fileAttachments.size() > 0 && totalAttachmentSize < 10485760) {
        // Other misc options 
        if (!testing) {
            List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            if (results.get(0).isSuccess())
                emailSent = true;
        return emailSent;

 So is there any other way to stop emails to "TO" address and send only to cc address.


Any help would be appreciated.





i dont think so , targetObjectId or To is requried at least. i guess if you sent "to" a bogus addres like no-reply@your company, and CC the user.


Thanks for your response. Here targetobjectId is must as we are using visualforce email template and for the merge fields to display properly in email's. 

What I want is, is there a way to change the "TO" email address before the email is sent, and set it back to the old email once the email is sent.


I tried to change the contact's email to cc email address before calling sendEventConfirmation and set it back to original email. But this is not working.