+ Start a Discussion

schuduler class sending email

i want send email to contacts when the opportunity was closed own
for perticular time am write schedular class
my scenario is
when the opportunity satge is closed own i want send email
in opportunity when i create contactrole i have to select is checked is primary i want send email to that particular contact

am write the pice of code am not getting any error but email was not sending can any one help me where am missing

global class Sending_Email_To_Contacts implements Schedulable


public List<Opportunity> Opp;

public List<contact> con;
public List<OpportunityContactRole> ocr;
public set<id> ids;
public set<id> ocrids;
global void execute(SchedulableContext ctx)

opp=[select id,Name,StageName,Accountid from Opportunity where StageName='Contracted Performance'];
for(Opportunity op:opp)
ocr=[select ContactId,Id,OpportunityId,IsPrimary from OpportunityContactRole where IsPrimary=true and OpportunityId in :ids];

for(OpportunityContactRole oc:ocr)

con=[select id,Name,Accountid,LeadSource,Email from contact where id in:ocrids];

List<String> toAddress = new List<String>();
for(Contact cn:con)

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

mail.setSubject('Good luck with the event today');
mail.setHtmlBody('Here is the email you requested! Check the attachment!');


Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });






why you are going to schedule class for sending email?


 you can write trigger on opportunity and put condition where stage = closed , and write email code.