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
B TulasiB Tulasi 

send two Emails at time using Visualforce Page

Hi All,

         How to send two Emails at a time from inputtext field. Actually now am sending only one mail like thulasi.204@gmail.com.
But My requirement is, I need to send one or more mails at time. like thulasi.204@gmail.com,abcd@gmail.com. So, how to send two mail at time.
My code :
public class sendemailcontroller {
    public String to { get; set; }
    public PageReference sendmail() {
    
    Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
    list<String> mid=(new String[]{to});
    mid.add(to);   
 
    mail.setToAddresses(mid);
    mail.setSubject('mail Sent Successfulluy');
    
    mail.setPlainTextBody('your case has been created');
    mail.setHtmlBody( 'I am not getting this sentence '+ 'here some link is there');
    Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
        return null;
    }

}


Thanks in Advance
Thulasi
pconpcon
If you want to send the same email to different addresses, you can just say 
 
List<String> mid = new List<String>{
    'address1@example.com',
    'address2@example.com'
}

If you want to send two different emails you can do
 
Messaging.SingleEmailMessage mail1 = new Messaging.SingleEmailMessage();
mail1.setToAddresses(new List<String>{ 'address1@example.com' });
mail1.setSubject('Mail 1');
mail1.setPlainTextBody('Mail 1 body');

Messaging.SingleEmailMessage mail2 = new Messaging.SingleEmailMessage();
mail2.setToAddresses(new List<String>{ 'address2@example.com' });
mail2.setSubject('Mail 2');
mail2.setPlainTextBody('Mail 2 body');

Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{
    mail1,
    mail2
});
B TulasiB Tulasi
Hi Pcon,

Thanks for giving your reply. What you given logic is  correct for when we are giving mails within the code. But, my requirement is, we need to give emails to input field in visulaforce page. I have one inputtext field as "Email" and command button is "Send Mail". If suppose enduser has to send 5 mails at time while enter the mails in inputfield.
 
pconpcon
So you are trying to cover the usecase of someone putting in "email1@example.com email2@example.com email3@example.com" in the text box?
B TulasiB Tulasi
Yes
 
pconpcon
In that case then you'd just want to split on the space and send the emails to that
 
Messaging.SingleEmailMessage mail1 = new Messaging.SingleEmailMessage();
mail1.setToAddresses(to.split(' '));
mail1.setSubject('Mail');
mail1.setPlainTextBody('Mail body');

Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{
    mail
});

Alternatively, you could enforce that the field not have any non-valid email characters and dynamically build a list of fields that would get added to a list of Strings that you would then send to.
Gyanender SinghGyanender Singh
Hi B Tulasi,

I write a demo code in which use a text type field to store all the email id and after click on send button its send mail to all email id , i try this code on the account object , please modified this code according to your requirement.
 
public class SendEmailCls {
    
    public Account acc{get;set;}
    public SendEmailCls1(ApexPages.StandardController controller) {
        acc = [select id,Send_Email__c from account where id=: ApexPages.currentPage().getParameters().get('id')];
    }
    
    public void send(){
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        list<string> toAddresses = (acc.Send_Email__c).split(';'); 
       
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String subject ='Hi Test' ;
        mail.setSubject(subject);
        mail.setToAddresses(toAddresses);
        
        mail.setPlainTextBody('hi teest body');
        mails.add(mail); 
        Messaging.SendEmail(mails);
    }
}


<apex:page standardController="Account" extensions="SendEmailCls">
    <Apex:form id="theform">
        <apex:inputtext value="{!acc.Send_Email__c}"/>
        <apex:commandButton value="Send Email" action="{!send}" reRender="theform"/>
    </Apex:form>
</apex:page>

Thanks
Gyani