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
SPRSPR 

I have a requirement of sending Emails to Public Groups by selecting Groups in visualforce page and adding all the group members email to the Toaddress and manually typing Subject and Body for the each case and linking it to the case.

Atla Satheesh 8Atla Satheesh 8
Hi SPR,

Here is teh solution: it might have syntax errors as i ddint execute it .Please modify it accordingly:

<apex:page controller="sendEmail">
 <apex:form >
Group name : <apex:inputText value="{!publicgroupname}" />// select group here
 <apex:commandButton value="SendEmails" action="{!sendingEmail}"/> <br/>
  Email Status :<b> {!message}  </b>
 </apex:form>
</apex:page>


public class sendEmail{    


    public List<string> getPublicGroups {get;set;}
    public string toAddress{get;set;}
    public string message{get;set;}
    
    public List<String> getEmailAddresses() {
    List<String> mailToAddresses = new List<String>();
    List<User> users = [SELECT Email FROM User WHERE Id IN (
                            SELECT UserOrGroupId
                            FROM GroupMember
                            WHERE Group.Name = :'Group Name'
                        )];

    for(User u : users)
        mailToAddresses.add(u.email);

    return mailToAddresses;
}
    
    public sendEmail(){
    
    
     //get lis of public groups here and display as drop down in vf page
    
    
    }
    
    public PageReference sendingEmail(){                
        Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
        String[] toWhom = new String[]{getEmailAddresses()};
        semail.setToAddresses(toWhom);
        semail.setSubject('Single Email message Example');
        semail.setPlainTextBody('Hello!!!!!This is a test email to test single email message program');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail});
        message='Email Sent !!!';
        return null;
    }
}

 
Atla Satheesh 8Atla Satheesh 8
you can add more fields in VF page ..if you would like to enter subject and other fields from VF page
SPRSPR
Thanks Satheesh.  I will try this and let you know.  Can we add more than 1 group at the same time (Multiselect).
SPRSPR
@Atla Satheesh.  It is giving error at Line 32 (String[] toWhom = new String[]{getEmailAddresses()};).  Initial expression is of incorrect type, expected: String but was: List<String>
Tad Aalgaard 3Tad Aalgaard 3
Change to
String[] toWhom = getEmailAddresses();