Today i am going to introduce the the email services via Apex code in salesforce. Some time we need to send an email via vf page . That time we need of Cc , Bcc. Here i will post the code what will be used to create the email service using Apex code.
Here i am adding on more thing to send the in email that List of Account. Account is just for an example. In the same process we can send also the another object's records.
*************************VF page****************************************
<apex:page showHeader="false" sidebar="false" standardStylesheets="false" controller="EmailSendController">
<apex:form id="frm">
<apex:actionFunction name="addmeAccount" action="{!sendAccount}" status="status">
<apex:param name="index" value="" assignTo="{!Index}"/>
</apex:actionFunction>
<apex:actionStatus id="status">
<apex:facet name="start">
<div style="width: 500px;">
<img src="/img/loading24.gif" style="vertical-align:middle;margin-left:50px;"/>
<span style="margin-left:10px; font-size: 12px; font-weight: bold; color: #000000;">Please wait...</span>
</div>
</apex:facet>
</apex:actionStatus>
<table border="0" width="40%" align="center" cellspacing="0" cellpadding="0">
<tr bgcolor="#222222">
<td width="90%" colspan="4">
<b><font size="3" color="White">New Message</font></b>
</td>
<td align="right" colspan="4">
<apex:image value="{!$Resource.Minusimage}" width="15px" height="15px" title="Minimize"/>
</td>
<td align="right" colspan="4">
<apex:image value="{!$Resource.MultiplyImage}" width="15px" height="15px" title="Close"/>
</td>
</tr>
</table><br/>
<table border="0" width="40%" align="center" cellspacing="0" cellpadding="2">
<tr>
<td width="14%">
<b><font size="3" color="#2F4F4F">To</font></b>
</td>
<td width="90%">
<apex:inputText size="80" value="{!to}"/>
</td>
<td width="14%">
<apex:commandLink value="Cc" action="{!mailcc}"/>
</td>
<td width="5%">
<apex:commandLink value="Bcc" action="{!mailbcc}"/>
</td>
</tr>
<apex:pageBlock rendered="{!cc}" id="mecc">
<tr>
<td width="5%" rowspan="4">
<b><font size="3" color="#2F4F4F">Cc</font></b>
</td>
<td width="90%" rowspan="4">
<apex:inputText size="80" value="{!addcc}"/>
</td>
</tr>
</apex:pageBlock>
</table>
<table border="0" width="40%" align="center" cellspacing="0" cellpadding="2">
<apex:pageBlock rendered="{!bcc}">
<tr>
<td width="9%" rowspan="4">
<b><font size="3" color="#2F4F4F">Bcc</font></b>
</td>
<td width="90%" rowspan="4">
<apex:inputText size="80" value="{!addBcc}"/>
</td>
</tr>
</apex:pageBlock>
</table>
<table border="0" width="40%" align="center" cellspacing="0" cellpadding="2">
<tr>
<td width="9%">
<b><font size="3" color="#2F4F4F">Subject</font></b>
</td>
<td width="90%" >
<apex:inputText size="80" value="{!sub}"/>
</td>
</tr>
<tr>
<td colspan="2">
<apex:inputTextarea richText="true" value="{!body}"/>
</td>
</tr>
</table>
<table border="0" width="40%" align="center" cellspacing="0" cellpadding="2">
<tr>
<td>
<apex:commandButton image="{!$Resource.SendButton}" action="{!mail}"/>
</td>
<td>
<b><apex:commandlink value="+" style="text-decoration: none;" action="{!hello}"/></b>
</td>
<apex:pageBlock id="acol" rendered="{!acol}">
<td>
<b><apex:commandlink value="Account" style="text-decoration: none;" action="{!addAccount}"/></b>
</td>
<td>
<b><apex:commandlink value="Contact" style="text-decoration: none;"/></b>
</td>
<td>
<b><apex:commandlink value="Opportunity" style="text-decoration: none;"/></b>
</td>
<td>
<b><apex:commandlink value="Lead" style="text-decoration: none;"/></b>
</td>
</apex:pageBlock>
</tr>
</table><br/>
<table border="0" width="40%" align="center" cellspacing="0" cellpadding="2">
<tr>
<td align = "center">
<apex:commandButton value="Add to mail" rendered="{!accbtn}" action="{!sendAccount}"/>
</td>
</tr>
<tr>
<td width="100%">
<apex:pageBlock >
<apex:pageblockTable value="{!nc}" var="nsc" width="100%">
<apex:column >
<apex:inputCheckbox value="{!nsc.acctrue}"/>
</apex:column>
<apex:column >
{!nsc.sr}
</apex:column>
<apex:column value="{!nsc.acct.name}"/>
<apex:column value="{!nsc.acct.accountnumber}"/>
<apex:column value="{!nsc.acct.phone}"/>
</apex:pageblockTable>
</apex:pageBlock>
</td>
</tr>
</table>
</apex:form>
</apex:page>
*****************************Controller***********************************
Public Class EmailSendController{
public boolean cc {get; set;}
public boolean bcc {get; set;}
public boolean acol {get; set;}
public string to {get; set;}
public string sub {get; set;}
public string body {get; set;}
public string addBcc {get; set;}
public string addcc {get; set;}
public List<Account> acc {get; set;}
public List<NestedClass> nc {get; set;}
public integer index {get; set;}
public boolean accbtn {get; set;}
public List<Account> selectedAcc {get; set;}
public String s {get; set;}
public List<String> s1 {get; set;}
public EmailSendController(){
cc = false;
bcc = false;
acol = false;
accbtn = false;
s1 = new List<String>();
}
public pagereference mailcc(){
if(cc == false){
cc = true;
}else{
cc = false;
}
return null;
}
public void mailbcc(){
if(bcc == false){
bcc = true;
}else{
bcc = false;
}
}
public pagereference hello(){
if(acol == false){
acol = true;
}else{
acol = false;
}
return null;
}
public pagereference addAccount(){
acc = new list<Account>();
nc = new List<NestedClass>();
accbtn = true;
acc = [select id, name , AccountNumber, phone from account];
integer i = 1;
for(Account ac : acc){
nestedClass nsc = new NestedClass();
nsc.sr = i++;
nsc.acct = ac;
nc.add(nsc);
}
return null;
}
public pageReference sendAccount(){
selectedAcc = new List<Account>();
if(nc.size() > 0){
account selectedAccount = new account();
for(NestedClass ac : nc){
if(ac.acctrue == true){
selectedAcc.add(ac.acct);
}
}
}
for(Account acc : selectedAcc){
s = '<Html>' ;
s += '<head>' ;
s += '</head>' ;
s += '<body>' ;
s += '<table border="0" bgcolor="#E6E6FA">';
s += '<tr>';
s += '<th style="color : red;">';
s += 'Account Name';
s += '</th>';
s += '<th style="color : red;">';
s += 'Account Number';
s += '</th>';
s += '<th style="color : red;">';
s += 'Phone';
s += '</th>';
s += '</tr>';
s += '<tr>';
s += '<td style="color : blue;">';
s += string.valueOf(acc.Name);
s += '</td>';
s += '<td style="color : blue;">';
s += string.valueOf(acc.AccountNumber);
s += '</td>';
s += '<td style="color : blue;">';
s += string.valueOf(acc.phone);
s += '</td>';
s += '</tr>';
s += '</table>';
s += '<body>' ;
s += '</html>' ;
s1.add(s);
}
return null;
}
public pagereference mail(){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String addresses;
addresses = to;
String[] toAddresses = addresses.split(',', 0);
email.setSubject(sub);
if(addBcc != null){
String[] toBccAddresses = addBcc.split(',', 0);
if(toBccAddresses.size() > 0 && toBccAddresses != null){
email.setBccAddresses(toBccAddresses);
}
}
if(addcc != null){
String[] toccAddresses = addcc.split(',', 0);
if(toccAddresses.size() > 0 && toccAddresses != null ){
email.setccAddresses(toccAddresses);
}
}
email.setReplyTo('excellencegaur@gmail.com');
email.setHTMLBody(body+ '' +s1);
email.setToAddresses(toAddresses);
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>{email};
Messaging.SendEmailResult [] r = Messaging.sendEmail(emails);
return null;
}
public class NestedClass{
public account acct {get; set;}
public integer sr {get; set;}
public boolean acctrue {get; set;}
}
}
If you have any confusion related to this post , then let me know.
Here i am adding on more thing to send the in email that List of Account. Account is just for an example. In the same process we can send also the another object's records.
*************************VF page****************************************
<apex:page showHeader="false" sidebar="false" standardStylesheets="false" controller="EmailSendController">
<apex:form id="frm">
<apex:actionFunction name="addmeAccount" action="{!sendAccount}" status="status">
<apex:param name="index" value="" assignTo="{!Index}"/>
</apex:actionFunction>
<apex:actionStatus id="status">
<apex:facet name="start">
<div style="width: 500px;">
<img src="/img/loading24.gif" style="vertical-align:middle;margin-left:50px;"/>
<span style="margin-left:10px; font-size: 12px; font-weight: bold; color: #000000;">Please wait...</span>
</div>
</apex:facet>
</apex:actionStatus>
<table border="0" width="40%" align="center" cellspacing="0" cellpadding="0">
<tr bgcolor="#222222">
<td width="90%" colspan="4">
<b><font size="3" color="White">New Message</font></b>
</td>
<td align="right" colspan="4">
<apex:image value="{!$Resource.Minusimage}" width="15px" height="15px" title="Minimize"/>
</td>
<td align="right" colspan="4">
<apex:image value="{!$Resource.MultiplyImage}" width="15px" height="15px" title="Close"/>
</td>
</tr>
</table><br/>
<table border="0" width="40%" align="center" cellspacing="0" cellpadding="2">
<tr>
<td width="14%">
<b><font size="3" color="#2F4F4F">To</font></b>
</td>
<td width="90%">
<apex:inputText size="80" value="{!to}"/>
</td>
<td width="14%">
<apex:commandLink value="Cc" action="{!mailcc}"/>
</td>
<td width="5%">
<apex:commandLink value="Bcc" action="{!mailbcc}"/>
</td>
</tr>
<apex:pageBlock rendered="{!cc}" id="mecc">
<tr>
<td width="5%" rowspan="4">
<b><font size="3" color="#2F4F4F">Cc</font></b>
</td>
<td width="90%" rowspan="4">
<apex:inputText size="80" value="{!addcc}"/>
</td>
</tr>
</apex:pageBlock>
</table>
<table border="0" width="40%" align="center" cellspacing="0" cellpadding="2">
<apex:pageBlock rendered="{!bcc}">
<tr>
<td width="9%" rowspan="4">
<b><font size="3" color="#2F4F4F">Bcc</font></b>
</td>
<td width="90%" rowspan="4">
<apex:inputText size="80" value="{!addBcc}"/>
</td>
</tr>
</apex:pageBlock>
</table>
<table border="0" width="40%" align="center" cellspacing="0" cellpadding="2">
<tr>
<td width="9%">
<b><font size="3" color="#2F4F4F">Subject</font></b>
</td>
<td width="90%" >
<apex:inputText size="80" value="{!sub}"/>
</td>
</tr>
<tr>
<td colspan="2">
<apex:inputTextarea richText="true" value="{!body}"/>
</td>
</tr>
</table>
<table border="0" width="40%" align="center" cellspacing="0" cellpadding="2">
<tr>
<td>
<apex:commandButton image="{!$Resource.SendButton}" action="{!mail}"/>
</td>
<td>
<b><apex:commandlink value="+" style="text-decoration: none;" action="{!hello}"/></b>
</td>
<apex:pageBlock id="acol" rendered="{!acol}">
<td>
<b><apex:commandlink value="Account" style="text-decoration: none;" action="{!addAccount}"/></b>
</td>
<td>
<b><apex:commandlink value="Contact" style="text-decoration: none;"/></b>
</td>
<td>
<b><apex:commandlink value="Opportunity" style="text-decoration: none;"/></b>
</td>
<td>
<b><apex:commandlink value="Lead" style="text-decoration: none;"/></b>
</td>
</apex:pageBlock>
</tr>
</table><br/>
<table border="0" width="40%" align="center" cellspacing="0" cellpadding="2">
<tr>
<td align = "center">
<apex:commandButton value="Add to mail" rendered="{!accbtn}" action="{!sendAccount}"/>
</td>
</tr>
<tr>
<td width="100%">
<apex:pageBlock >
<apex:pageblockTable value="{!nc}" var="nsc" width="100%">
<apex:column >
<apex:inputCheckbox value="{!nsc.acctrue}"/>
</apex:column>
<apex:column >
{!nsc.sr}
</apex:column>
<apex:column value="{!nsc.acct.name}"/>
<apex:column value="{!nsc.acct.accountnumber}"/>
<apex:column value="{!nsc.acct.phone}"/>
</apex:pageblockTable>
</apex:pageBlock>
</td>
</tr>
</table>
</apex:form>
</apex:page>
*****************************Controller***********************************
Public Class EmailSendController{
public boolean cc {get; set;}
public boolean bcc {get; set;}
public boolean acol {get; set;}
public string to {get; set;}
public string sub {get; set;}
public string body {get; set;}
public string addBcc {get; set;}
public string addcc {get; set;}
public List<Account> acc {get; set;}
public List<NestedClass> nc {get; set;}
public integer index {get; set;}
public boolean accbtn {get; set;}
public List<Account> selectedAcc {get; set;}
public String s {get; set;}
public List<String> s1 {get; set;}
public EmailSendController(){
cc = false;
bcc = false;
acol = false;
accbtn = false;
s1 = new List<String>();
}
public pagereference mailcc(){
if(cc == false){
cc = true;
}else{
cc = false;
}
return null;
}
public void mailbcc(){
if(bcc == false){
bcc = true;
}else{
bcc = false;
}
}
public pagereference hello(){
if(acol == false){
acol = true;
}else{
acol = false;
}
return null;
}
public pagereference addAccount(){
acc = new list<Account>();
nc = new List<NestedClass>();
accbtn = true;
acc = [select id, name , AccountNumber, phone from account];
integer i = 1;
for(Account ac : acc){
nestedClass nsc = new NestedClass();
nsc.sr = i++;
nsc.acct = ac;
nc.add(nsc);
}
return null;
}
public pageReference sendAccount(){
selectedAcc = new List<Account>();
if(nc.size() > 0){
account selectedAccount = new account();
for(NestedClass ac : nc){
if(ac.acctrue == true){
selectedAcc.add(ac.acct);
}
}
}
for(Account acc : selectedAcc){
s = '<Html>' ;
s += '<head>' ;
s += '</head>' ;
s += '<body>' ;
s += '<table border="0" bgcolor="#E6E6FA">';
s += '<tr>';
s += '<th style="color : red;">';
s += 'Account Name';
s += '</th>';
s += '<th style="color : red;">';
s += 'Account Number';
s += '</th>';
s += '<th style="color : red;">';
s += 'Phone';
s += '</th>';
s += '</tr>';
s += '<tr>';
s += '<td style="color : blue;">';
s += string.valueOf(acc.Name);
s += '</td>';
s += '<td style="color : blue;">';
s += string.valueOf(acc.AccountNumber);
s += '</td>';
s += '<td style="color : blue;">';
s += string.valueOf(acc.phone);
s += '</td>';
s += '</tr>';
s += '</table>';
s += '<body>' ;
s += '</html>' ;
s1.add(s);
}
return null;
}
public pagereference mail(){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String addresses;
addresses = to;
String[] toAddresses = addresses.split(',', 0);
email.setSubject(sub);
if(addBcc != null){
String[] toBccAddresses = addBcc.split(',', 0);
if(toBccAddresses.size() > 0 && toBccAddresses != null){
email.setBccAddresses(toBccAddresses);
}
}
if(addcc != null){
String[] toccAddresses = addcc.split(',', 0);
if(toccAddresses.size() > 0 && toccAddresses != null ){
email.setccAddresses(toccAddresses);
}
}
email.setReplyTo('excellencegaur@gmail.com');
email.setHTMLBody(body+ '' +s1);
email.setToAddresses(toAddresses);
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>{email};
Messaging.SendEmailResult [] r = Messaging.sendEmail(emails);
return null;
}
public class NestedClass{
public account acct {get; set;}
public integer sr {get; set;}
public boolean acctrue {get; set;}
}
}
If you have any confusion related to this post , then let me know.
No comments:
Post a Comment