-4

can anyone pls tell me the solution this is the error

--sendanemail: execution of BeforeInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Email address is invalid: test: [toAddresses, test]: Trigger.sendanemail: line 33, column 1

I'm new in trigger I did this trigger for contact so I copied it for opportunity but unable to understand it for opportunity

trigger sendanemail on Opportunity(before insert,before update) {

  List<Messaging.SingleEmailMessage> mails = 
  new List<Messaging.SingleEmailMessage>();

  for (Opportunity opp: Trigger.new) {
if (opp.Name!= null && opp.StageName== 'Closed Won') {

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


  List<String> sendTo = new List<String>();
  sendTo.add(opp.Name);
  mail.setToAddresses(sendTo);

       mail.setReplyTo('alecrahul@gmail.com');
  mail.setSenderDisplayName('Official Bank of Nigeria');


  List<String> ccTo = new List<String>();
  ccTo.add('business@bankofnigeria.com');
  mail.setCcAddresses(ccTo);

        mail.setSubject('URGENT BUSINESS PROPOSAL');
  String body = 'Dear ' + opp.Name+ ', ';
  body += 'I confess this will come as a surprise to you.';
  body += 'I am John Alliston CEO of the Bank of Nigeria.';
  mail.setHtmlBody(body);
  mails.add(mail);
    }
  }
    Messaging.sendEmail(mails);
}
2

The error should be in the line sendTo.add(opp.Name);

List<String> sendTo = new List<String>();
sendTo.add(opp.Name);
mail.setToAddresses(sendTo);

You are setting the opportunity name as an email address and it could not be. A valid email information is necessary. You should get the contact email or user email, for example.

1
  • Thnx for the sol peixoto Oct 8 '19 at 2:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.