-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('[email protected]');
  mail.setSenderDisplayName('Official Bank of Nigeria');


  List<String> ccTo = new List<String>();
  ccTo.add('[email protected]');
  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);
}

1 Answer 1

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, 2019 at 2:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .