function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dmchengdmcheng 

Messaging.sendMail: multiple different emails in a single call?

Hello.  On p. 420 of the 2008 Force.com Developers Guide (distributed at Dreamforce 2008), it says that "Message.sendMail method takes an array of messages to send, so you can send multiple messages in one single call."

 

Does this mean I can send an array of different email messages, each to a single recipient, in one call?  What is the syntax for constructing the array?  The documentation is not clear.

 

Here are the specifics of my situation: I need to send an email containing a personalized URL to several people, and I can't use merge fields/templates because the people's data are stored in a custom object, not in the Contact object.

 

Thanks

David

Best Answer chosen by Admin (Salesforce Developers) 
ahab1372ahab1372

you create a list for email messages. Then you create your emails, one by one (maybe in a loop) and add them to that list. Finally you do a Messaging.sendEmail(yourList).

 

Pseudo Code

 

 

List<Messaging.SingleEmailMessage>  myEmails = new List<Messaging.SingleEmailMessage>();

//repeat this paragraph as often as you like
Messaging.SingleEmailMessage oneEmail = new Messaging.SingleEmailMessage();
    //specify details of oneEmail here
myEmails.add(oneEmail);

//now send them

Messaging.sendEmail(myEmails);

 

I agree that the documentation is not easy to understand, becuase the examples only create one single message, and then create the list (with this one single email as the only item) on the fly in the Messaging.sendEmail() call

 


from the Apex Guide:

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_classes_email_outbound.htm

 

 

 

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

// details deleted

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

// new Messaging.SingleEmailMessage[] { mail }  is the list of email messages, they create it here on the fly.
// The [] means it is a list
// You should be able to create it in advance (see example above), or 
// do something like 
// Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail1, mail2, mail3 } ) ;

 

 

All Answers

waylonatcimwaylonatcim

In the api docs there are examples of sendMail() in Jave and c# that might help shed some light on the usage.  They do mention sending mass emails with the function and it seems like the examples can be easily scaled.

 

Hope that helps.

ahab1372ahab1372

you create a list for email messages. Then you create your emails, one by one (maybe in a loop) and add them to that list. Finally you do a Messaging.sendEmail(yourList).

 

Pseudo Code

 

 

List<Messaging.SingleEmailMessage>  myEmails = new List<Messaging.SingleEmailMessage>();

//repeat this paragraph as often as you like
Messaging.SingleEmailMessage oneEmail = new Messaging.SingleEmailMessage();
    //specify details of oneEmail here
myEmails.add(oneEmail);

//now send them

Messaging.sendEmail(myEmails);

 

I agree that the documentation is not easy to understand, becuase the examples only create one single message, and then create the list (with this one single email as the only item) on the fly in the Messaging.sendEmail() call

 


from the Apex Guide:

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_classes_email_outbound.htm

 

 

 

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

// details deleted

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

// new Messaging.SingleEmailMessage[] { mail }  is the list of email messages, they create it here on the fly.
// The [] means it is a list
// You should be able to create it in advance (see example above), or 
// do something like 
// Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail1, mail2, mail3 } ) ;

 

 

This was selected as the best answer
dmchengdmcheng

Thanks for your reply.  After I posted this I did some further reading and came to understand the syntax of the new [sObject] {} statement and realized what you said, that I need to create a separate list and use that.

 

David

DTCFDTCF

Can you still only send 10 emails at a time this way inside the list? What is the maximum number of distinct emails that can be sent at once through Salesforce?

DTCFDTCF

More to the point, if I needed to send many more emails, how would I be able to accomplish this?

ahab1372ahab1372

check out the Mass Email Method of the Apex Email Class:

http://www.salesforce.com/us/developer/docs/apexcode/index.htm

 

dmchengdmcheng

Good question DTCF - I've started another thread to get clarification on the 10 maximum.

andhra123andhra123
Hi dmcheng, Did you get answer for maximum number of emails can be sent at once from salesforce using list. I am sending number of distinct emails, by storing each email in a list as below, and then sending all those emails at once. I would like to know, how may maximum number of mails i can send like this. List emailList = new List(); Messaging.sendEmail(emailList,false); Thanks
dmchengdmcheng

Here is the thread I posted on the maximum number.  Does that have the info you need?

 

http://boards.developerforce.com/t5/Apex-Code-Development/Messaging-SingleEmailMessage-array-and-sendEmail-limits/m-p/185839

andhra123andhra123
Anand mentioned we can send 10*1000 emails theoretically , assuming that each list contains 1000 emails. But i would like to know whether 1000 is maximum limit for one sendEmail call, or we can send any number of emails.say 2000, 3000 etc. Please let me know if you have answer for this. I have also messaged Anand at other thread, and waiting for reply. Thank you very much for your response.