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
Jack Thomas 3Jack Thomas 3 

How to send Single Email using Apex Class from vf page ?

hey i'm new to salesforce i like to send a single email from my vf page .

help me!

Best Answer chosen by Jack Thomas 3
Ajay K DubediAjay K Dubedi
Hi  JJack,
Try below code:

Visualforce Page:
<apex:page controller="singleEmailExample">
 <apex:form >
Email Address : <apex:inputText value="{!toAddress}" />
 <apex:commandButton value="SendEmail" action="{!sendingEmail}"/> <br/>
  Email Status :<b> {!message}  </b>
 </apex:form>
</apex:page>

Apex Controller:
public class singleEmailExample{    
    public string toAddress{get;set;}
    public string message{get;set;}
    public PageReference sendingEmail(){
        Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
        String[] sendingTo = new String[]{toAddress};
        semail.setToAddresses(sendingTo);
        //String[] sendingToBccAdd = new String[]{‘XXXX@gmail.com’};
       // semail.setBccAddresses(sendingToBccAdd);
       // String[] sendingTocAdd = new String[]{‘XXXXX@gmail.com’};
      // semail.setCcAddresses(sendingTocAdd); */
        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;
    }
}
You can also add Cc and Bcc address in your email.
Hope This will Help you.

Regards,
Ajay

All Answers

DeveloperSudDeveloperSud
Hi,
Sending an Email with Visualforce​:-
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_intro.htm
Did u check this site .I think it is matching your requirement .Please mark this as solved if it helps.
 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same
1) https://help.salesforce.com/articleView?id=000181102&type=1

Let us know if this will help you
 
Ajay K DubediAjay K Dubedi
Hi  JJack,
Try below code:

Visualforce Page:
<apex:page controller="singleEmailExample">
 <apex:form >
Email Address : <apex:inputText value="{!toAddress}" />
 <apex:commandButton value="SendEmail" action="{!sendingEmail}"/> <br/>
  Email Status :<b> {!message}  </b>
 </apex:form>
</apex:page>

Apex Controller:
public class singleEmailExample{    
    public string toAddress{get;set;}
    public string message{get;set;}
    public PageReference sendingEmail(){
        Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
        String[] sendingTo = new String[]{toAddress};
        semail.setToAddresses(sendingTo);
        //String[] sendingToBccAdd = new String[]{‘XXXX@gmail.com’};
       // semail.setBccAddresses(sendingToBccAdd);
       // String[] sendingTocAdd = new String[]{‘XXXXX@gmail.com’};
      // semail.setCcAddresses(sendingTocAdd); */
        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;
    }
}
You can also add Cc and Bcc address in your email.
Hope This will Help you.

Regards,
Ajay
This was selected as the best answer
Jack Thomas 3Jack Thomas 3
Hey Thanks AJay.
Satya Ranjan MohantySatya Ranjan Mohanty
Hi 
I have created  schedule apex class which sending mail to a user but the problem is mail also goes to the user who has schduled the apex job
can any one help me out to resolve this

Thanks