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
rupesh ranjanrupesh ranjan 

I want to send email. But TO Address should be number of email address.

I want to send email. 
But TO Address should be number of email address. 
Note :- email address are not relating with any contact or Lead Object

Some sample Code plss
AshlekhAshlekh
Hi,

Here is a blog.

http://www.sfdc99.com/2014/03/01/sending-emails-using-apex/

-Thanks
Ashlekh Gera
ManojjenaManojjena
HI Rupesh ,
in singleEmailmessage class there is a method setToAddress() which takes a parameter of a listof address .So always it will take list of address .
Where you have a limit that you can not add more then 100 email in that list which you will assign in the to addrsssList .

Check belwo links for all methods .

https://developer.salesforce.com/docs/atlas.en-us.200.0.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm#apex_Messaging_SingleEmailMessage_setToAddresses

You need to assign like belwo .
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
List<String> toAddress =new List<String>{'mk@gmail.com','kk@gmail.com'}
mail.SetToAddresss(toAddress);
I think this will help you .
Let  me know if it helps !!
Thanks
Manoj
 
rupesh ranjanrupesh ranjan
Hi Manoj i dont knw what To adress will be... It will be random. So how its possible

Some sample code pls
ManojjenaManojjena
Hi Rupesh,
Check with below code ,change email with your email  and try to execute in developer console .
Messaging.SingleEmailMessage  email = new Messaging.SingleEmailMessage();
	List<String> toAddress =new List<String>{'mm@gmail.com.com','kk@gmail.com'};
	email.setPlainTextBody('Testing here ');             
	email.setSubject('Demo Emil');          
	email.setToAddresses(toAddress);
	Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email});
Let me know if it helps !!
Thanks
Manoj
 
rupesh ranjanrupesh ranjan
Hi Manoj you are getting me. 
you have entered 
mm@gmail.com.com','kk@gmail.com

I cant replace email id with this because this is at user end what they will enter i dont know. It will be any thing

 
ManojjenaManojjena
Do one thing you change your email and test in console it will send email to your mail box .
Else create a custom lable ask them to add emails with seperated by semicolon .Add code like belwo .
Messaging.SingleEmailMessage  email = new Messaging.SingleEmailMessage();
	List<String> toAddress =new List<String>();
	toAddress=System.Label.EmailList.split(';');
	email.setPlainTextBody('Testing here ');             
	email.setSubject('Demo Emil');          
	email.setToAddresses(toAddress);
	Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email});
Your lable name should EmailList then your code wil work .
Modify your body as per your user .
Let me know in case any issue .
Thanks
Manoj
 
rupesh ranjanrupesh ranjan
But In this case governer limit will hit. Not more than 10 mails :(
ManojjenaManojjena
Rupesh,
This limit is if you will hit the Email server 10 times .
Incase you are using  this email inside for loop then you need to create a list like belwo and declare outside the loop .
List< Messaging.SingleEmailMessage> emailList=new List<Messaging.SingleEmailMessage>();
add the email inside loop and sendEmail outside the loop like belwo .
 
Messaging.sendEmail(emailList);

Thanks
Manoj

 
rupesh ranjanrupesh ranjan
This is my code plz do some changes in this what you are trying to say

Public class SendEmail
{
    public String subject {get; set;}
    public String body {get; set;}
    public String to{get; set;}
    private contact order;
     public SendEmail(ApexPages.StandardController controller) {
    this.order = (contact )controller.getRecord();
    }

    public contact getOrder(){
      return order;
    }
    public pageReference send(){

    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    String[] toaddress = new String[]{};
    toaddress.add(to);
    email.setSubject(subject);
    email.setToAddresses(toaddress);
    email.setPlainTextBody(body);
    email.setBccSender(true);
  Messaging.SendEmailResult [] res = Messaging.SendEmail(new Messaging.SingleEmailMessage[] {email});  
  for ( Messaging.sendEmailResult result : res ) {

           if ( !res[0].isSuccess () ) {

               System.debug ( result  );
           }

           else{

               ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Email Sent Successfully' );
               ApexPages.addMessage(msg);
           }
       }
     
return null; 
       
}
public SendEmail(){
        body = apexpages.currentpage().getparameters().get('att');
    }


}