Ajax (1) Apex Class (12) Apex Trigger (2) Community (2) Home Page (1) HTML (4) Integration (3) JS (7) KB (1) Label (1) Licenses (1) Listing (1) Log (1) OOPs (5) Sharing (1) Static Resource (1) Test Class (3) URI (1) Visualforce (10)

Wednesday 9 April 2014

Send Email

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