+ Start a Discussion
Anushka BansalAnushka Bansal 

Build a Conference Management App - Creating an Apex Class

public with sharing class EmailManager 
{
    public static void sendMail(String address, String subject, String body)
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] { address };
        mail.setToAddresses(toAddresses);
        mail.setSubject(subject);
        mail.setPlainTextBody(body);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }

in the above code .. line 3  we have created a new instance of the class Messaging.SingleEmailMessage ..  why in the last line we are again using the keyword new .. why cant we simply write it as ​"Messaging.sendEmail(mail) ;"
Best Answer chosen by Anushka Bansal
sfdcMonkey.comsfdcMonkey.com
hi Anushka Bansal
because sendEmail(); method always take list of messages as a paramenter in above case we create a singel 'main' message so when we call sendEmail() method we create a 'new Messaging.SingleEmailMessage[]' array and add mail in this array and pass it to the sendEmail() method for Immediately sends an email message.
Syntax
For single email messages:
SendEmailResult = connection.sendEmail(SingleEmailMessage emails[]);
For mass email messages:
SendEmailResult = connection.sendEmail(MassEmailMessage emails[]);

i hope it helps you
let me inform if it helps you
thanks