+ Start a Discussion
CezuCezu 

Sendmail - one mail,one person

I have problem :

I want send one mail to one person using this code (for each user mail others mailadresses must be invisible , not bcc ):

 

global class mailSendBatchJob implements Database.Batchable<sObject>,Schedulable {
public List<String> usr{get;set;} //
public List<sObject> usr_email{get;set;}
public Map<string,string> Map_usr{get;set;}

global mailSendBatchJob(){
}

global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator([select id,E_mail__c from invoice_statement__c where Status__c = 'Negocjowana']);
}

global void execute(Database.BatchableContext BC,List<sObject> scope){
usr = new List<String>();
usr_email = new List<sObject>();
usr.clear();
usr_email.clear();
usr_email=[select id,E_mail__c from invoice_statement__c where Status__c = 'Negocjowana'];

Map_usr=new Map<string,string>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {};

for(sobject s : scope) {
toAddresses.add(string.valueof( s.get('E_mail__c')));
}

mail.setToAddresses(toAddresses);
mail.setSubject('Twoja faktura jest otwarta');
mail.setPlainTextBody('Twoja faktura jest otwarta');

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

global void execute (SchedulableContext sc){
MailSendBatchJob b = new MailSendBatchJob();
Database.executeBatch(b,10);
}

global void finish(Database.BatchableContext BC){
}
}

 

 

Can somebody help 

Best Answer chosen by Admin (Salesforce Developers) 
CezuCezu

I used that code :)

 

 

 

 

global class mailSendBatchJob implements Database.Batchable<sObject>,Schedulable {

global mailSendBatchJob(){
}

global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator([select id,E_mail__c from invoice_statement__c where Status__c = 'Negocjowana']);
}

global void execute(Database.BatchableContext BC,List<sObject> scope){
List<String> usr = new List<String>();

Messaging.Email[] emails = new Messaging.Email[]{};

for(sobject s : scope){
if(s != null){
EmailTemplate et = [select Id,Body from emailtemplate where name = 'Large Invoice Template' limit 1];

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] { string.valueof( s.get('E_mail__c')) }); //tablica jednoelementowa
emails.add(mail);
mail.setSubject('Twoja faktura jest otwarta');
mail.setHtmlBody(et.body);

mail.setCharset('UTF-8');

}
}

Messaging.sendEmail(emails, true);
}

global void execute (SchedulableContext sc){
MailSendBatchJob b = new MailSendBatchJob();
Database.executeBatch(b,10);
}

global void finish(Database.BatchableContext BC){
}
}

All Answers

dragon123dragon123

I think you must remove dupliacate data using set object

CezuCezu

I used that code :)

 

 

 

 

global class mailSendBatchJob implements Database.Batchable<sObject>,Schedulable {

global mailSendBatchJob(){
}

global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator([select id,E_mail__c from invoice_statement__c where Status__c = 'Negocjowana']);
}

global void execute(Database.BatchableContext BC,List<sObject> scope){
List<String> usr = new List<String>();

Messaging.Email[] emails = new Messaging.Email[]{};

for(sobject s : scope){
if(s != null){
EmailTemplate et = [select Id,Body from emailtemplate where name = 'Large Invoice Template' limit 1];

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] { string.valueof( s.get('E_mail__c')) }); //tablica jednoelementowa
emails.add(mail);
mail.setSubject('Twoja faktura jest otwarta');
mail.setHtmlBody(et.body);

mail.setCharset('UTF-8');

}
}

Messaging.sendEmail(emails, true);
}

global void execute (SchedulableContext sc){
MailSendBatchJob b = new MailSendBatchJob();
Database.executeBatch(b,10);
}

global void finish(Database.BatchableContext BC){
}
}

This was selected as the best answer