Email Scenario
Send an Email from Apex using Visualforce page
public class sendEmail {
public String subject{get;set;}
public String body{get;set;}
public String text{set;get;}
public String text1{set;get;}
public List <String> ToAddresses{get;set;}
public List<String> CcAddresses{set;get;}
public sendEmail()
{
ToAddresses = new List <String>();
//Add your custom logic to extract email of contact
CcAddresses = new List<String>();
}
public PageReference send(){
// Define the email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
ToAddresses.add(text);
CcAddresses.add(text1);
// Sets the paramaters of the email
email.setSubject( subject );
email.setToAddresses( ToAddresses );
email.setCcAddresses( CcAddresses);
email.setPlainTextBody( body );
Messaging.SendEmailResult[] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
return null;
}
}
<apex:page controller="sendEmail">
<apex:pageBlock title="Send an Email to Contact">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel value="Subject"/>
<apex:inputText value="{!subject}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Body" />
<apex:inputText value="{!body}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Mail-Id" />
<apex:inputText value="{!text}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Mail-Id2" />
<apex:inputText value="{!text1}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:commandButton value="Send Email" action="{!send}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:pageBlock>
</apex:page>
Comments
Post a Comment