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
Alfredo OrnelasAlfredo Ornelas 

Sending an email in VFP

Hi,
I'm new in VFP can you send me a simple Custom Controller code and VFP code so i can send an email to a email address?
The user will enter the email address on the VFP.
Thanks!
AshlekhAshlekh
Hi,

Below I write a function, Class this function to send email

sendemail(String toAddress,String FromAddress, String CCEMaIL ,String bodycontent,String SUBJECT)
{  
  
  List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
  
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    
      // Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(toAddress);
      mail.setToAddresses(sendTo);
    
      // Step 3: Set who the email is sent from
      mail.setReplyTo(FromAddress);
      mail.setSenderDisplayName('thank you email');
    
      // (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add(CCEMaIL);
      mail.setCcAddresses(ccTo);

      // Step 4. Set email contents - you can use variables!
      mail.setSubject(SUBJECT);
      mail.setHtmlBody(bodycontent);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
      // Step 6: Send all emails in the master list
     Messaging.sendEmail(mails);
}

IF it helps you than please mark it as a solution and ENJOY APEX
Deepak Kumar ShyoranDeepak Kumar Shyoran
Controller :

public class sendEmailController() {
		public void sendEmailToSomeAddress() {
			Messaging.SingleEmailMessage mail = 
			  new Messaging.SingleEmailMessage();
			  List<String> sendTo = new List<String>() ;
			  String sendToAddress = ApexPages.currentPage().getParameters().get('mailAddress') ;
			  sendTo.add(sendToAddress);
			  mail.setToAddresses(sendTo);
			  st<String> ccTo = new List<String>();
			  
			  mail.setSubject('Test Subject');
			  
			  String body = 'Test Body content ';
			  mail.setHtmlBody(body);
			  
			  mails.add(mail);

			  Messaging.sendEmail(mails);
		}
	}

V.F.
<apex:page controller="sendEmailController" >
<apex:form>
<b> Email Address : </b>
<input type="text" id="mailAddress"><br/>
<apex:commandButton action="{!sendEmailToSomeAddress}" value="Send Mail" />
</apex:form>
<apex:page>

Above code is not tested but will properly except some small error that can easily be solved ..also will not provide you any attractive UI.

Please mark it as best solution to your problem if it does solve your problem.


AshlekhAshlekh
Hi,

The code which is provide by Deepak just change one line

<input type="text" id="mailAddress"><br/> to 

<input type="text" name="mailAddress"><br/>

Both post helps you.


IF it helps you than please mark it as a solution and ENJOY APEX
Alfredo OrnelasAlfredo Ornelas
Thank you all! It work.
AshlekhAshlekh

Pleae mark a solution to any ans which helps you. It boost us. ENjoy APEX.
Alfredo OrnelasAlfredo Ornelas
Thank you! that worked!!