---
schema_version: "1"
id: ab4d3733ef3eb22149f55d523f2eb3f3ba1c2d655cefc78a47b4f9db8811ad6e
title: Creating a Custom Controller with the Messaging Class
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/salesforce-pages-developers-guide/guides/pages/email-and-templates/pages-email-custom-controller.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/guide/pages-email-custom-controller.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: d8d387fe80c9619dcf3eb47e342c0fa8e60372b2aefc379d8903fe882376f175
products:
  - Visualforce Developer Guide
  - Platform Developer Content
---

# Creating a Custom Controller with the Messaging Class

At minimum, a custom controller that uses the Apex `Messaging` namespace needs a subject, a body, and a recipient for the email. You will need a page that acts as a form to fill out the subject and body and deliver the email.

Create a new page called `sendEmailPage` and use the following code:

<!-- otherprops: apex_page set_clipboard -->
```markup
<apex:page controller="sendEmail">
	<apex:messages />
	<apex:pageBlock title="Send an Email to Your 
			{!account.name} Representatives">
		<p>Fill out the fields below to test how you might send an email to a user.</p>
		<br />
		<apex:dataTable value="{!account.Contacts}" var="contact" border="1">
			<apex:column >
				<apex:facet name="header">Name</apex:facet>
				{!contact.Name}
			</apex:column>
			<apex:column >
				<apex:facet name="header">Email</apex:facet>
				{!contact.Email}
			</apex:column>
		</apex:dataTable>
    
		<apex:form >
		<br /><br />
			<apex:outputLabel value="Subject" for="Subject"/>:<br />     
			<apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
			<br /><br />
			<apex:outputLabel value="Body" for="Body"/>:<br />     
			<apex:inputTextarea value="{!body}" id="Body"  rows="10" cols="80"/>           
			<br /><br /><br />
			<apex:commandButton value="Send Email" action="{!send}" /> 
		</apex:form>
	</apex:pageBlock>
</apex:page>
```

Notice in the page markup that the account ID is retrieved from the URL of the page. For this example to render properly, you must associate the Visualforce page with a valid account record in the URL. For example, if `001D000000IRt53` is the account ID, the resulting URL should be:

```nolang
https://MyDomain_login_URL/apex/sendEmailPage?id=001D000000IRt53
```

[Displaying Field Values with Visualforce](https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/quick-start/pages-quick-start-display-field-values.md) has more information about retrieving the ID of a record.

The following code creates a controller named `sendEmail` that implements the `Messaging.SingleEmailMessage` class, and uses the contacts related to an account as recipients:

<!-- otherprops: apex_code set_clipboard -->
```controller
public class sendEmail {
	public String subject { get; set; }
	public String body { get; set; }

	private final Account account;

	// Create a constructor that populates the Account object
	public sendEmail() {
		account = [select Name, (SELECT Contact.Name, Contact.Email FROM Account.Contacts) 
				from Account where id = :ApexPages.currentPage().getParameters().get('id')];
	}

	public Account getAccount() {
		return account;
	}

	public PageReference send() {
		// Define the email
		Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 

    String addresses;
    if (account.Contacts[0].Email != null)
    {
        addresses = account.Contacts[0].Email;
        // Loop through the whole list of contacts and their emails
        for (Integer i = 1; i < account.Contacts.size(); i++) 
        {
            if (account.Contacts[i].Email != null)
            {
                addresses += ':' + account.Contacts[i].Email;
            }
        }
    }

		String[] toAddresses = addresses.split(':', 0);

		// Sets the paramaters of the email
		email.setSubject( subject );
		email.setToAddresses( toAddresses );
		email.setPlainTextBody( body );
    
		// Sends the email
		Messaging.SendEmailResult [] r = 
			Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
		
		return null;
	}
}
```

Notice in the controller that:

-   The subject and body of the email are set through a separate Visualforce page and passed into the controller.
-   The method that sends the email is called `send()`. This name must match the name of the action for the Visualforce button that sends the email.
-   The recipients of the email, that is, the email addresses stored in `toAddresses[]`, come from the addresses of the contacts available in an associated account. When compiling a list of recipients from contacts, leads, or other records, it is a good practice to loop through all the records to verify that an email address is defined for each. The account ID is retrieved from the URL of the page.

![A form on sendEmailpage called "Send an email to your Burlington Textiles Corp of America representatives."](https://developer.salesforce.com/media/salesforce-pages-developers-guide/pages_email_example.jpg "Example of the Form on sendEmailPage")

**See Also**  


-   [*Apex Developer Guide*: Outbound Email](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_forcecom_email_outbound.htm)

