function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DevmenDevmen 

How to get bcc mail id in emailservice class?

Hi,

I've created a email field (bcc) in lead.I want to populate bcc mail id here using email service class.How can i acheive this?

Thanks
Devmen
Best Answer chosen by Devmen
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
BCC stands for "Blind Carbon Copy".  Email addresses listed in the BCC field of an email will receive the email, but the BCC list is not transmitted with the email.  This is so no one who receives the email can see who was BCC'ed -- that's what is meant by "blind".  So, incoming email does not have a BCC field -- only outgoing email can have a BCC field.  Therefore, there is NO WAY to obtain BCC information from a received email.  You will have to talk to whoever created the requirement and explain that it is not possible.

All Answers

Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Not sure if this is what you're asking for, but the Messaging.SingleEmailMessage class has a method:
 
public void setBccAddresses( String[] bccAddresses )

Is this what you're looking for?
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
If you're trying to get BCC address from an email that you have received, I don't think that's possible.  The whole idea of the BCC address is that the receiver can't see it.
Raj VakatiRaj Vakati
Sample code is here .. try to set by using  setBccAddresses(bccAddresses) on SingleEmailMessage 

mail.setBccAddresses(lead.bccemail__c) ;


https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm
 
Messaging.reserveSingleEmailCapacity(2);

  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

  String[] toAddresses = new String[] {Lead.email}; 

  String[] ccAddresses = new String[] {Lead.email};

  // Assign the addresses for the To and CC lists to the mail object.

  mail.setToAddresses(toAddresses);

  mail.setCcAddresses(ccAddresses);

  mail.setBccAddresses(lead.bccemail__c) ;
  
  // Specify the address used when the recipients reply to the email. 

  mail.setReplyTo('sfdcsrini@gmail.com');

  // Specify the name used as the display name.

  mail.setSenderDisplayName('Salesforce Support');

  // Specify the subject line for your email address.

  mail.setSubject('New Lead Created : ' + Lead.Id);

  // Set to True if you want to BCC yourself on the email.

  mail.setBccSender(false);

  // Optionally append the salesforce.com email signature to the email.

  // The email address of the user executing the Apex Code will be used.

  mail.setUseSignature(true);

  // Specify the text content of the email.

  mail.setPlainTextBody('Your Lead: ' + Lead.Id +' has been created.');

  mail.setHtmlBody('Your Lead: ' + Lead.Id +' has been created.'+

       'To view your Contact click here.');

  // Send the email you have created.

  Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 }

}

 
DevmenDevmen
hi,

Thanks to all for your replies. 
global class CreateLeadExample implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {

        Lead lead;

        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult(); 
         Messaging.SingleEmailMessage mail= new Messaing.SingleEmailMessage();   
        try {
            //Look for lead whos name is the email and create it if necessary

            List<Lead> leadList = new List<Lead>();


            String tempLead = email.FromAddress;
            String [] ccAddress=email.ccAddresses;
            System.debug('ToAddress'+email.toAddresses);
            System.debug('FROMADDRESS = '+email.FromAddress);
            System.debug('FROMADDRESS TEMPLEAD = '+tempLead);


            String leadQuery = 'select LastName from Lead where LastName = \'' + tempLead +'\'';

            leadList = Database.Query(leadQuery);
            Integer leadCount = leadList.size();
            System.debug('LEAD COUNT FROM THE QUERY -'+leadCount);
              
            if ([select count() from Lead where Name = :email.FromAddress] == 0) {
            
             for (String address : email.ccAddresses)
               {
               
                System.Debug('INSIDE IF for LEAD COUNT EQUAL TO 0');
                system.debug('EmailAddress'+ email.ccAddresses);
                lead = new Lead();
                lead.Status = 'New';
                lead.LastName = email.FromName;
                lead.Company = (email.FromAddress.split('@').get(1));
                lead.Email = email.FromAddress;
                system.debug(address);
                lead.Cc__c=address;
                system.debug(lead.Cc__c);
                lead.Description='Subject:'+ email.subject  + '/n' + email.plainTextBody;
                mail.setBccAddresses(lead.Bcc__c);
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                }
                
                insert lead;
                
                
            //Save any text attachments, if any
            if (email.textAttachments != null)
            {
            for (integer i = 0 ; i < email.textAttachments.size() ; i++) {
            Attachment attachment = new Attachment();
            // attach to the newly created contact record
            attachment.ParentId = lead.Id;
            attachment.Name = email.textAttachments[i].filename;
            attachment.Body = Blob.valueof(email.textAttachments[i].body);
            insert attachment;
            }
            
            }

            //Save any Binary Attachment
            
             if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
          for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
            Attachment attachment = new Attachment();
            // attach to the newly created contact record
            attachment.ParentId = lead.Id;
            attachment.Name = email.binaryAttachments[i].filename;
            attachment.Body = email.binaryAttachments[i].body;
            insert attachment;
          }
       }
          
            }
            
            else { //Lead already exists
                lead = [select Id from Lead where Name = :email.FromAddress];
            }          
            result.success = true;
            
        } catch (Exception e) {
            result.success = false;
            result.message = 'Error processing email...';
        }
        return result;
    }
}

Here is my sample code.Im able to display cc address in lead.Cc__c field. Can i use singe mail message to populate bcc address .Now its showing  Error: Compile Error: Method does not exist or incorrect signature: void setBccAddresses(String) from the type Messaging.SingleEmailMessage at line 46 column 22.Please help me to resolve this?
Thanks
Raj VakatiRaj Vakati
Change it as below 
 
mail.setBccAddresses(new String[]{lead.Bcc__c});

 
Raj VakatiRaj Vakati
global class CreateLeadExample implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {

        Lead lead;

        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult(); 
         Messaging.SingleEmailMessage mail= new Messaing.SingleEmailMessage();   
        try {
            //Look for lead whos name is the email and create it if necessary

            List<Lead> leadList = new List<Lead>();


            String tempLead = email.FromAddress;
            String [] ccAddress=email.ccAddresses;
            System.debug('ToAddress'+email.toAddresses);
            System.debug('FROMADDRESS = '+email.FromAddress);
            System.debug('FROMADDRESS TEMPLEAD = '+tempLead);


            String leadQuery = 'select LastName from Lead where LastName = \'' + tempLead +'\'';

            leadList = Database.Query(leadQuery);
            Integer leadCount = leadList.size();
            System.debug('LEAD COUNT FROM THE QUERY -'+leadCount);
              
            if ([select count() from Lead where Name = :email.FromAddress] == 0) {
            
             for (String address : email.ccAddresses)
               {
               
                System.Debug('INSIDE IF for LEAD COUNT EQUAL TO 0');
                system.debug('EmailAddress'+ email.ccAddresses);
                lead = new Lead();
                lead.Status = 'New';
                lead.LastName = email.FromName;
                lead.Company = (email.FromAddress.split('@').get(1));
                lead.Email = email.FromAddress;
                system.debug(address);
                lead.Cc__c=address;
                system.debug(lead.Cc__c);
                lead.Description='Subject:'+ email.subject  + '/n' + email.plainTextBody;
                mail.setBccAddresses(new String[]{lead.Bcc__c});
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                }
                
                insert lead;
                
                
            //Save any text attachments, if any
            if (email.textAttachments != null)
            {
            for (integer i = 0 ; i < email.textAttachments.size() ; i++) {
            Attachment attachment = new Attachment();
            // attach to the newly created contact record
            attachment.ParentId = lead.Id;
            attachment.Name = email.textAttachments[i].filename;
            attachment.Body = Blob.valueof(email.textAttachments[i].body);
            insert attachment;
            }
            
            }

            //Save any Binary Attachment
            
             if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
          for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
            Attachment attachment = new Attachment();
            // attach to the newly created contact record
            attachment.ParentId = lead.Id;
            attachment.Name = email.binaryAttachments[i].filename;
            attachment.Body = email.binaryAttachments[i].body;
            insert attachment;
          }
       }
          
            }
            
            else { //Lead already exists
                lead = [select Id from Lead where Name = :email.FromAddress];
            }          
            result.success = true;
            
        } catch (Exception e) {
            result.success = false;
            result.message = 'Error processing email...';
        }
        return result;
    }
}

 
DevmenDevmen
Hi Raj,

Now the error is not showing.Lead is not getting created.In debug log Bccaddess is showing null value.
System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Email body is required.: []
Thanks
Raj VakatiRaj Vakati
Please add the below send email logic as shown below .. you need to set this values
 
// First, reserve email capacity for the current Apex transaction to ensure
// that we won't exceed our daily email limits when sending email after
// the current transaction is committed.
Messaging.reserveSingleEmailCapacity(2);

// Processes and actions involved in the Apex transaction occur next,
// which conclude with sending a single email.

// Now create a new single email message object
// that will send out a single email to the addresses in the To, CC & BCC list.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

// Strings to hold the email addresses to which you are sending the email.
String[] toAddresses = new String[] {'user@acme.com'}; 
String[] ccAddresses = new String[] {'smith@gmail.com'};
  

// Assign the addresses for the To and CC lists to the mail object.
mail.setToAddresses(toAddresses);
mail.setCcAddresses(ccAddresses);

// Specify the address used when the recipients reply to the email. 
mail.setReplyTo('support@acme.com');

// Specify the name used as the display name.
mail.setSenderDisplayName('Salesforce Support');

// Specify the subject line for your email address.
mail.setSubject('New Case Created : ' + case.Id);

// Set to True if you want to BCC yourself on the email.
mail.setBccSender(false);

// Optionally append the salesforce.com email signature to the email.
// The email address of the user executing the Apex Code will be used.
mail.setUseSignature(false);

// Specify the text content of the email.
mail.setPlainTextBody('Your Case: ' + case.Id +' has been created.');

mail.setHtmlBody('Your case:<b> ' + case.Id +' </b>has been created.<p>'+
     'To view your case <a href=https://***yourInstance***.salesforce.com/'+case.Id+'>click here.</a>');

// Send the email you have created.
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 
DevmenDevmen
Hi,

In lead page there are few fields(Description,email id,Bcc ,Cc).These fields should populate values from mail when i create a lead using emailtolead.eg: subject in email should populate in description in lead page.Currently im getting all the values except bcc.I cannot assign the string to bcc .
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
BCC stands for "Blind Carbon Copy".  Email addresses listed in the BCC field of an email will receive the email, but the BCC list is not transmitted with the email.  This is so no one who receives the email can see who was BCC'ed -- that's what is meant by "blind".  So, incoming email does not have a BCC field -- only outgoing email can have a BCC field.  Therefore, there is NO WAY to obtain BCC information from a received email.  You will have to talk to whoever created the requirement and explain that it is not possible.
This was selected as the best answer
Deepika Chauhan 17Deepika Chauhan 17
Hi,
ChimpConnect, an application will allow you to integrate MailChimp and Salesforce together to optimize your Email Marketing Campaigns.
You can consider these links for more info: https://eshopsync.com/mailchimp-salesforce-integration/
https://eshopsync.com/mailchimp