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
BrandiTBrandiT 

Outbound email class error - REQUIRED_FIELD_MISSING, Missing target address

I wrote a simple apex class that should send an outbound email when a button is clicked.  This is on a custom object called Email_Draft__c.  I need the To address to be populated from a contact record listed in a custom lookup field called Send_to_Contact__c.

 

I was able to create the class with no errors, but when I test the button I get the following error message:

SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing target address (target, to, cc, bcc): []

 

 

Here is the actual apex class I'm trying to use.  I did set a target value and ensured that the contact I chose has an email address populated.  What do I need to do to correct this class?

 

Thanks!

 

public class EmailDraft
{
private Email_Draft__c ed;
public EmailDraft(ApexPages.StandardController controller)
{
this.ed=(Email_Draft__c)controller.getRecord();
}

public void SendEmail()
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
/*Create an instance of SingleEmailMessage Object*/
mail.setTargetObjectId(ed.Send_to_Contact__r.Id);
/*Setting the recipient as the Contact mentioned in the Id*/
mail.setHTMLBody(ed.email_body__c);
/*Setting the html body as the rich text body field on email draft*/
mail.setSubject(ed.subject__c);
/*Setting the subject of the email to the subject field on email draft*/
mail.setWhatid(ed.id);
/*Setting the whatid of the email to the email draft record*/

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
/*Sending Email*/
}
}

Shashikant SharmaShashikant Sharma

You have not provideed toAddress to which mail should be send like this

 

// 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);

 

You have to set to address for which you want your mail should go.

 

see this for more :http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound.htm

BrandiTBrandiT

Thank you!  That solution works perfectly when I have a specific email address to send to.

 

What can I do if I want to reference the email address populated in another field.  On my custom object, I have a lookup field to contact and I want to send to that contact's email address

 

I tried this already:

String[] toAddresses = new String[] {ed.send_to_contact__r.email};

but I get the same error message.

 

Thanks again for your help! 

 

If you could show me how to code it for the lookup field, I would GREATLY appreciate it!

Shashikant SharmaShashikant Sharma

I just tested it without hard coded email adress here is code sample

VFP

<apex:page controller="sendEmailNotification">
   <apex:form>
   <apex:pageBlock>
       <apex:commandButton value="Send Mail" action="{!sendMailToContact}"/>
       <apex:pageBlockSection>
           
       </apex:pageBlockSection>
   </apex:pageBlock>
   </apex:form>
</apex:page>

 

Controller

 

public class sendEmailNotification 
{
    
    public sendEmailNotification()
    {
    }
    
    public void sendMailToContact()
    {
       Contact c = [Select Account.EmailField__c, Email from Contact where id = '00390000007JIcp'];
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
       String[] toAddresses = new String[] {c.Email};
       //String[] toAddresses = new String[] {c.Account.EmailField__c};
       mail.setToAddresses(toAddresses);
       mail.setSubject('Test Email to Related Object Email');
       mail.setPlainTextBody(' Test Email body 1');
       Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

    }

}

 I have used a contact id in this 00390000007JIcp from my org , you can try with any of your org. I have tested it for both

String[] toAddresses = new String[] {c.Email};     

And 

String[] toAddresses = new String[] {c.Account.EmailField__c};

 

 

In you case please verify whether this is ed.send_to_contact__r.email giving a value( a email address) or null.

Let me know if it helps you.