+ 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});
    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
If you want to send the same email to different addresses, you can just say 
List<String> mid = new List<String>{

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>{
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.
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
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.setPlainTextBody('Mail body');

Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{

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.setPlainTextBody('hi teest body');

<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"/>