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
rsenthil86rsenthil86 

Send email through Apex

Hi All,

 

Can anyone please help me.

I need to send a email from apex. can you please give me some example.

what i wanna do is....

if any field edited in the record. I wanna send a mail with the field name ("Name") and the new value("Senthil")

Thanks.

lakslaks

Hi,

 

Here is a simple way to send mail via Apex.

 

public static void sendMail(String r_stErrorMessage)
{
     Messaging.SingleEmailMessage l_objMail = new Messaging.SingleEmailMessage(); 
     String[] l_starrToAddress = new String[]{'laks@gmail.com'};
     l_objMail.setToAddresses(l_starrToAddress);
     l_objMail.setReplyTo('laks@gmail.com');
     l_objMail.setSenderDisplayName('Error-notify');
     l_objMail.setSubject('Exception occured in your program');
     l_objMail.setPlainTextBody(r_stErrorMessage);
     Messaging.sendEmail(new Messaging.SingleEmailMessage[] { l_objMail });
} 

 However in the above code we are hardcoding the address to which the mail needs to be sent.

In case you want to make it more dynamic, you can use Custom Settings (App Setup -> Develop -> Custom Settings) and create a mailing list.

 

The below code uses a Mailing list "AddrGrp__c" in which the list of "To address",  "Reply To address" is set.

 

public static void sendMail(String r_stErrorMessage)
{
    public static List<AddrGrp__c>  m_ltAddressList= new List<AddrGrp__c>();
    public static List<AddrGrp__c>  m_ltReplyAddressList= new List<AddrGrp__c>();
    public static String m_stSenderDisplayName = 'Error-notify';
    public static String m_stSubject = 'Exception occured in your program.';

   Messaging.SingleEmailMessage l_objMail = new Messaging.SingleEmailMessage(); 
        
 try
 {
    m_ltAddressList = [Select Name,ToAddress__c  From AddrGrp__c ]; 
    m_ltReplyAddressList = [Select Name,ReplyToaddress__c From AddrGrp__c  where  ReplyToaddress__c!='null' limit 1];
 }
 catch(QueryException l_objEx)
 {
    String l_stExceptionType = l_objEx.getTypeName();
    String l_stErrMessage = l_stExceptionType+': Error occured while retrieving  AddrGrp Records.';
    System.debug(l_stErrMessage);
 }
            
 for (AddrGrp__c l_objAdd : m_ltReplyAddressList)
 {  
    String  m_stReplyToAddress = l_objAdd.ReplyToaddress__c;
         
    for(AddrGrp__c l_objToAdd : m_ltAddressList)
    { 
	String m_stToAddress = l_objToAdd.ToAddress__c;
	String[] l_starrToAddress = new String[]{m_stToAddress};
	l_objMail.setToAddresses(l_starrToAddress);
	l_objMail.setReplyTo(m_stReplyToAddress);
	l_objMail.setSenderDisplayName(m_stSenderDisplayName);
	l_objMail.setSubject(m_stSubject);
	l_objMail.setPlainTextBody(r_stErrorMessage);
	Messaging.sendEmail(new Messaging.SingleEmailMessage[] { l_objMail });
    }
 }      
} 

 

Hope this helps you.

 

 

Regards,

Lakshmi.

 

lakslaks

Hi,

 

Did the above solve your problem ? If so please mark it as solution so that others may benefit. If not please let me know what went wrong.

 

 

Regards,

Lakshmi.