0

My Email class is getting a compilation error as below. Can someone please help?

Error

 Method does not exist or incorrect signature: [Messaging.SingleEmailMessage].setToAddresses(String) 

Apex class snippet

      String addresses; 
      // activeUser = [Select Email From User where Username = : UserInfo.getUserName()];
       for (User u : [Select Email From User where Username = : UserInfo.getUserName()]) {     

            addresses+=':'+ u.Email;

            }
         String[] toAddresses = addresses.split(':', 0); 

            // Sets the paramaters of the email
            email.setSubject( 'Email Generated Blank Forms');
            email.setToAddresses( toAddresses[0] );
            email.setPlainTextBody( 'Please find attached forms');

            email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

            // Sends the email
            Messaging.SendEmailResult [] r = 
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   

            return null;
  • Whenever you face such an error, you can check the documentation and confirm the method signature. Documentation helps a lot when such errors are there. – ajinkyah Feb 15 '16 at 9:03
1
 email.setToAddresses( toAddresses[0] );

You need to pass here list of string not string.

try this

List<string> addresses =  new List<string>(); 
      // activeUser = [Select Email From User where Username = : UserInfo.getUserName()];
       for (User u : [Select Email From User where Username = : UserInfo.getUserName()]) {     

            addresses.add( u.Email);

            }
//or can do this addresses.add( userinfo.getuserEmail()); don't need any soql

            // Sets the paramaters of the email
            email.setSubject( 'Email Generated Blank Forms');
            email.setToAddresses( addresses );
            email.setPlainTextBody( 'Please find attached forms');

            email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

            // Sends the email
            Messaging.SendEmailResult [] r = 
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   

            return null;
| improve this answer | |
  • toAddresses[0] will give a single address and not multiple addresses. So this will also throw an error. It should be email.setToAddresses(toAddresses); where toAddresses is a list of String. – ajinkyah Feb 15 '16 at 9:01
  • @Azinq check my answer. – Tushar Sharma Feb 15 '16 at 9:03
  • @TusharSharma Thanks , but I am not receiving the emails with it. – Testing_SFDC Feb 15 '16 at 9:15
1

Try this

email.setToAddresses( toAddresses );

setToAddresses method accepts List and here you are passing String.

Ref: https://developer.salesforce.com/docs/atlas.en-us.200.0.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm#apex_Messaging_SingleEmailMessage_setToAddresses

| improve this answer | |

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.