GCL
GCL

Reputation: 45

Sending email from Visualforce apex controller

I'm having trouble trying to send out an email from a visualforce page controller. Not exposed through a site but rather in the UI. Here's some sample code:

Contact contact = [SELECT Id, Email FROM Contact WHERE Id = '003Pw000005Y121IAC' LIMIT 1];
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject('subject');
email.setPlainTextBody('body'); 
email.setTargetObjectId(contact.Id);
email.setSaveAsActivity(true);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });

If I try this in a "normal" apex class or in the Execute Anonymous window, everything works as expected. But When I try this in an the controller I get

Messaging.SendEmailError[getMessage=Email address is invalid: ;getStatusCode=INVALID_EMAIL_ADDRESS;getTargetObjectId=003Pw00000AXApOIAX

I can't find any similar cases online ... what am I doing wrong?

Thanks in advance!

Upvotes: 0

Views: 70

Answers (0)

Related Questions