To actually perform this task, following code will help you to rectify the error. The only thing you need to do is you need to create a dummy contact of your own instead of going for an existing one. Following below is the dummy contact.
Once you created a dummy contact, then do this code.
// Pick a dummy Contact
Contact c = [select id, Email from Contact where email <> null limit 1];
// Construct the list of emails we want to send
List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
msg.setTemplateId( [select id from EmailTemplate where DeveloperName='My_Email_Template'].id );
msg.setWhatId( [select id from Account limit 1].id );
msg.setTargetObjectId(c.id);
msg.setToAddresses(new List<String>{'[email protected]'});
lstMsgs.add(msg);
// Send the emails in a transaction, then roll it back
Savepoint sp = Database.setSavepoint();
Messaging.sendEmail(lstMsgs);
Database.rollback(sp);
// For each SingleEmailMessage that was just populated by the sendEmail() method, copy its
// contents to a new SingleEmailMessage. Then send those new messages.
List<Messaging.SingleEmailMessage> lstMsgsToSend = new List<Messaging.SingleEmailMessage>();
for (Messaging.SingleEmailMessage email : lstMsgs) {
Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
emailToSend.setToAddresses(email.getToAddresses());
emailToSend.setPlainTextBody(email.getPlainTextBody());
emailToSend.setHTMLBody(email.getHTMLBody());
emailToSend.setSubject(email.getSubject());
lstMsgsToSend.add(emailToSend);
}
Messaging.sendEmail(lstMsgsToSend);
I hope this will help. Want to know more about salesforce apex? prefer this link batch apex in salesforce