SENDING eMAIL TO ALL CONTACTS

Visualforce Page:

<apex:page controller=”relatedcontacts”>
<script>
function showModal(val){
//window.showModalDialog(‘/apex/secondpage?id=’+val);
window.showModalDialog(‘/apex/contactswithcheckbox?id=’+val);
}
</script>
<apex:form >
<apex:pageblock >
<apex:pageBlockSection title=”Records”>
<apex:pageBlockTable value=”{!Accounts}” var=”a”>
<apex:column headerValue=”Name”>
<apex:commandlink value=”{!a.Name}” onclick=”showModal(‘{!a.accid}’)” />
</apex:column>
<apex:column headerValue=”phone” >
{!a.phone}
</apex:column>
<apex:column headerValue=”website” >
{!a.website}
</apex:column>
<apex:column headerValue=”taxable” >
{!a.taxable}
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>

controller:
public with sharing class relatedcontacts {
public relatedcontacts(){
AccountData();
}
list<Account> lstacc = new list<Account>();
list<accountwrapclass> lstAccwrap = new list<accountwrapclass>();
public list<accountwrapclass> getAccounts() {
// lstacc=[select id,name,phone,website,taxable__c from Account];
return lstAccwrap ;
}
public void AccountData(){
for(Account acc:[select id,name,phone,website,taxable__c from Account]){
accountwrapclass objacc = new accountwrapclass();
objacc.Name = acc.Name;
objacc.accid = acc.id;
objacc.phone = acc.phone;
objacc.website = acc.website;
if(acc.taxable__C == true){
objacc.taxable =’yes’;
}else{
objacc.taxable =’NO’;
}
lstAccwrap.add(objAcc);
}
}

wrapper class :
public class accountwrapclass{
public string accid{get;set;}
public string Name{get;set;}
public string Phone{get;set;}
public string Website{get;set;}
public string taxable{get;set;}
}
}
contacts with checkbox:second page
<apex:page controller=”wrapperClassController” showHeader=”false” >
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons location=”bottom”>
<apex:commandButton value=”Send Email” action=”{!processSelected}” rerender=”table” />
</apex:pageBlockButtons>
<!– In our table we are displaying the cContact records –>
<apex:pageBlockTable value=”{!contacts}” var=”c” id=”table”>
<apex:column >
<!– This is our selected Boolean property in our wrapper class –>
<apex:inputCheckbox value=”{!c.selected}”/>
</apex:column>
<!– This is how we access the contact values within our cContact container/wrapper –>
<apex:column value=”{!c.con.Name}” />
<apex:column value=”{!c.con.Email}” />
<apex:column value=”{!c.con.Phone}” />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

controller:

public class wrapperClassController {
public string accid ;
public wrapperClassController (){
accid = apexpages.currentpage().getparameters().get(‘id’);
system.debug(‘***************Accid’+accid);
}
//Our collection of the class/wrapper objects cContact
public List<cContact> contactList {get; set;}
//This method uses a simple SOQL query to return a List of Contacts
public List<cContact> getContacts() {
if(contactList == null) {
contactList = new List<cContact>();
for(Contact c : [select Id, Name, Email, Phone from Contact where AccountId=:accid]) {
// As each contact is processed we create a new cContact object and add it to the contactList
contactList.add(new cContact(c));
}
}
return contactList;
}
public PageReference processSelected() {
//We create a new list of Contacts that we be populated only with Contacts if they are selected
List<Contact> selectedContacts = new List<Contact>();
//We will cycle through our list of cContacts and will check to see if the selected property is set to true, if it is we add the Contact to the selectedContacts list
for(cContact cCon : getContacts()) {
if(cCon.selected == true) {
selectedContacts.add(cCon.con);
}
}
// Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc
System.debug(‘These are the selected Contacts…’);
for(Contact con : selectedContacts) {
string conEmail = con.Email;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// Strings to hold the email addresses to which you are sending the email.
String[] toAddresses = new String[] {conEmail};
//String[] ccAddresses = new String[] {‘smith@gmail.com’};
// Assign the addresses for the To and CC lists to the mail object.
mail.setToAddresses(toAddresses);
// mail.setCcAddresses(ccAddresses);
// Specify the address used when the recipients reply to the email.
mail.setReplyTo(‘support@acme.com’);
// Specify the name used as the display name.
mail.setSenderDisplayName(‘Salesforce Support’);
// Specify the subject line for your email address.
mail.setSubject(‘New Case Created : ‘ + case.Id);
// Set to True if you want to BCC yourself on the email.
mail.setBccSender(false);
// Optionally append the salesforce.com email signature to the email.
// The email address of the user executing the Apex Code will be used.
mail.setUseSignature(false);
// Specify the text content of the email.
mail.setPlainTextBody(‘Thank for Contacting’);
mail.setHtmlBody(‘Thank for Contacting’);
// Send the email you have created.
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
// system.debug(con);
}
return null;
}
// This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
public class cContact {
public Contact con {get; set;}
public Boolean selected {get; set;}
//This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false
public cContact(Contact c) {
con = c;
selected = false;
}
}
}