function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Harsh vardhana 18Harsh vardhana 18 

HI I want to understand the code could any one help me


public class EmailManager {
// Public method
public void sendMail(String address, String subject, String body) {
// Create an email message object
Messaging.SingleEmailMessage mail = new    Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {address};
mail.setToAddresses(toAddresses);
mail.setSubject(subject);
mail.setPlainTextBody(body);
// Pass this email message to the built-in sendEmail method
// of the Messaging class
Messaging.SendEmailResult[] results = Messaging.sendEmail( new Messaging.SingleEmailMessage[] { mail });
// Call a helper method to inspect the returned results
inspectResults(results);
}

what are    mail.setToAddresses(toAddresses); 
                 mail.setSubject(subject);
                 mail.setPlainTextBody(body);
  they methord Or what ? 
I am studing apex and find a lot of difficulty in codes.. 
if they are objects then why do we have not declared/initilise them? 
Kiran Kumar( Personal )Kiran Kumar( Personal )

Hi Harsh,

I will try to explain : 

  • Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    • Messaging is a namespace and SingleEmailMessage is a Class in that Namespace.
    • Messaging.SingleEmailMessage -- Creating a new variable of type 'Messaging.SingleEmailMessage' Class.
    • new Messaging.SingleEmailMessage() - Creates a new instance of the class
  • Messaging.SendEmail() -- Here Messaging is class, SendEmail is a method.
    • This method is used to Send email. For it to send email, we need to provide it the values in the form it needs. So please refer to this page https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_messaging.htm?search_text=SendEmail to know what this method needs.
    • This method needs information in this format : 
      • sendEmail(Messaging.Email[] emails, Boolean allOrNothing)
        • Boolean allOrNothing - its a condition to either send all email or not to send any email. ignore it for now please.
        • Messaging.Email[] emails - the first part needs a list of emails.
    • This means that to be able to send email, you need to provide the method the input as EMAIL type. which means of the type Messaging.SingleEmailMessage. ( if i say variable A is of integer type, you will create the variable as Integer A. If i say variable B is of Account type, you will create a variable as Account B. Same way, when you have to provide input of type Email, you have to create that variable as Messaging.SingleEmailMessage emails.)

In Summary, 'Messaging.sendEmail' method which needs some parameters of type Messaging.SingleEmailMessage to be able to work.

Now that you understood the above, lets understand these :

                 mail.setToAddresses(toAddresses); 
                 mail.setSubject(subject);
                 mail.setPlainTextBody(body);
 

  • mail is a variable of type 'Messaging.SingleEmailMessage'. This type has some properties, (attributes ). We need to populate those properties with the required information. 
  • mail.setToAddresses is a property. the email addresses to whom you want to send email are set using this.
  • mail.setSubject - the subject of the email is set using this.
  • mail.setPlainTextBody - the body of the email is set here.

Once you set the properties, the mail variable is ready to be used by 'Messaging.sendEmail' method which sends the email.

If you are a beginner and have never coded before, don't feel disappointed if you do not understand this. just keep practicing and learn gradually. This is tough even for me to understand when I saw the first time or was learning. 

Please mark this as answer if I could help you here. In case you have further  doubts you can post here. ALso refer to others answers here who may respond correcting me if I missed something or did not explain correctly.

Good Luck.