You need to sign in to do that
Don't have an account?

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*/
}
}
You have not provideed toAddress to which mail should be send like this
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
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!
I just tested it without hard coded email adress here is code sample
VFP
Controller
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.