You need to sign in to do that
Don't have an account?
Nagma Khan
how to send the email to all contact person with the visualforce page in salesforce?
hi
i want to send a email to all contact person and showing all contact details on the visualfroce and want to send the email to all contact person just clicking button sending email
Thanks
Nagma khan
i want to send a email to all contact person and showing all contact details on the visualfroce and want to send the email to all contact person just clicking button sending email
Thanks
Nagma khan
Please try the below Code which works fine to me!!!
VF Page:
<apex:page controller="ContactList">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!Lstwrp}" var="w">
<apex:column value="{!w.acc.Id}" headerValue="Contact Id"/>
<apex:column value="{!w.acc.Name}" headerValue="Contact Name"/>
<apex:column value="{!w.acc.Email}" headerValue="Email"/>
</apex:pageblockTable>
<apex:pageBlockButtons >
<apex:commandButton value="Send Email" action="{!selected}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Class:
Public class ContactList
{
public List<wrapperAccountCheckBox> Lstwrp{get;set;}
public ContactList()
{
Lstwrp = new List<wrapperAccountCheckBox>();
List<Contact> accLst=new List<contact>([SELECT Id,Name,Email FROM contact where name='raj']);
for(integer i=0;i<acclst.size();i++)
{
Lstwrp.add(new wrapperAccountCheckBox(accLst[i]));
}}
List<String> selectedlst = new List<String>();
public void selected(){
System.debug('---------->'+Lstwrp);
for(wrapperAccountCheckBox w:Lstwrp){
System.debug('---------->'+w.acc);
selectedlst.add(w.acc.Email);
}
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSubject('Morning Wishes');
mail.setPlainTextBody('Good Morning');
mail.setToAddresses(selectedlst);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
//---------------------------
Public class wrapperAccountCheckBox
{
public contact acc{get;set;}
public wrapperAccountCheckBox(contact a)
{
this.acc=a;
}
}
//----------------------------
}
Can you please try the code and let me know if it works or not?
Mark this as solved if it's resolved.
Thanks,
Raj
All Answers
It is possible to send email using Visualforce by creating a custom controller to deliver the message. The Apex Messaging.SingleEmailMessage class handles the outbound email functionality available to Salesforce.
The following topics demonstrate a number of features available when sending email through Visualforce:
- https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_custom_controller.htm
- https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_sending_attachments.htm
May I also suggest you please try the below code snippet which might help.Visual Force Page: Apex controller: When you will run this code then first of all you will pass "Account Id" into URL.
Please let us know if this helps.
Mark this as solved if it's resolved.
Thanks,
Nagendra.
its showing erroe
Visualforce Error
Help for this Page
System.QueryException: List has no rows for assignment to SObject
Class.sendEmails.<init>: line 10, column 1
thanks
Nagma
Please try the below Code which works fine to me!!!
VF Page:
<apex:page controller="ContactList">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!Lstwrp}" var="w">
<apex:column value="{!w.acc.Id}" headerValue="Contact Id"/>
<apex:column value="{!w.acc.Name}" headerValue="Contact Name"/>
<apex:column value="{!w.acc.Email}" headerValue="Email"/>
</apex:pageblockTable>
<apex:pageBlockButtons >
<apex:commandButton value="Send Email" action="{!selected}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Class:
Public class ContactList
{
public List<wrapperAccountCheckBox> Lstwrp{get;set;}
public ContactList()
{
Lstwrp = new List<wrapperAccountCheckBox>();
List<Contact> accLst=new List<contact>([SELECT Id,Name,Email FROM contact where name='raj']);
for(integer i=0;i<acclst.size();i++)
{
Lstwrp.add(new wrapperAccountCheckBox(accLst[i]));
}}
List<String> selectedlst = new List<String>();
public void selected(){
System.debug('---------->'+Lstwrp);
for(wrapperAccountCheckBox w:Lstwrp){
System.debug('---------->'+w.acc);
selectedlst.add(w.acc.Email);
}
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSubject('Morning Wishes');
mail.setPlainTextBody('Good Morning');
mail.setToAddresses(selectedlst);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
//---------------------------
Public class wrapperAccountCheckBox
{
public contact acc{get;set;}
public wrapperAccountCheckBox(contact a)
{
this.acc=a;
}
}
//----------------------------
}
Can you please try the code and let me know if it works or not?
Mark this as solved if it's resolved.
Thanks,
Raj
its Error
Visualforce Error
Help for this Page
System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Email address is invalid: null: [toAddresses, null]
Error is in expression '{!selected}' in component <apex:commandButton> in page visualfrocepagemail: Class.ContactList.selected: line 27, column 1
Class.ContactList.selected: line 27, column 1
thanks
Nagma
your code is right
Thanls