Single Email Messaging :
//First Method(Using list of String)
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//String[] toAddresses = new String[]{'krishnanand.gupta@gmail.com','kg91.in@gmail.com'};//You can use this way
List<String> toAddresses = new List<String>();//or you can use this way.
toAddresses.add('kg91.in@gmail.com');
mail.setToAddresses(toAddresses);
mail.setSubject('Just Test');
mail.setPlainTextBody('Hi! This is Simple dfgbfTest');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
-----------------------------------------------------------------
//Second Method(Using Object)
public void SendEmail()
{
contact con=[Select id from contact limit 1];
EmailTemplate et=[Select id from EmailTemplate where name=:'EmailTemplatename'];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(con.Id);
mail.setSenderDisplayName(‘Charan Tej’);
mail.setTemplateId(et.id);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
-------------------------------
-------------------------------
Mass Email Messaging:
//First Method
public void SendEmail()
{
List<contact> lstcon=[Select id from contact limit 200];
List<Id> lstids= new List<Id>();
for(Contact c:lstcon){
lstids.add(c.id);
}
EmailTemplate et=[Select id from EmailTemplate where name=:'EmailTemplatename'];
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
mail.setTargetObjectIds(lstIds);
mail.setSenderDisplayName(‘Charan Tej’);
mail.setTemplateId(et.id);
Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
}
No comments:
Post a Comment