+ Start a Discussion
MIDHUN G REDDYMIDHUN G REDDY 

How to Create Custom Button to Send Email to a Specific Email

Hi,
   How to Create Custom Button to Send Email to a Specific Email ,Please explain with code
Best Answer chosen by MIDHUN G REDDY
Neetu_BansalNeetu_Bansal
Hi Midhun,

You can create a apex class to write code for sending email like this, here I have hard coded my email id 'neetu.bansal.5@gmail.com'. You can replace it. Also I have hard coded subject and body as Test Subject and Test Body. You need to replace that too.
public class Controller
{
	public void sendMailTo()
	{
    	Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

		//Strings to hold the email addresses to which you are sending the email. 
		String[] toAddresses = new String[] { 'neetu.bansal.5@gmail.com' }; 
		
		//Assign the addresses for the To list to the mail object. 
		mail.setToAddresses( toAddresses );
		
		//Specify the subject line for your email address. 
		mail.setSubject( 'Test Subject' );
		
		// To store mail body                 
		String mailBody = 'Test Body';
		
		// Specify the text content of the email. 
		mail.setHTMLBody(mailBody);
		
		// Check if Organization email limit exceeded
		try
		{
			// Send the email you have created.
			Messaging.reserveSingleEmailCapacity( 1 );
			Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
		}
		catch( Exception e )	
		{
			// To debug results
			system.debug( 'Email cannnot be send.' );
		}
	}
}
And visualforce page 'Vf_Page' like this:
<apex:page controller="Controller" action="{!sendMailTo}">
	Email sent Successfully
</apex:page>
After this you can create a custom button like this:
User-added image

Let me know if you need any other help.

Thanks,
Neetu

All Answers

Neetu_BansalNeetu_Bansal
Hi Midhun,

You can create a apex class to write code for sending email like this, here I have hard coded my email id 'neetu.bansal.5@gmail.com'. You can replace it. Also I have hard coded subject and body as Test Subject and Test Body. You need to replace that too.
public class Controller
{
	public void sendMailTo()
	{
    	Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

		//Strings to hold the email addresses to which you are sending the email. 
		String[] toAddresses = new String[] { 'neetu.bansal.5@gmail.com' }; 
		
		//Assign the addresses for the To list to the mail object. 
		mail.setToAddresses( toAddresses );
		
		//Specify the subject line for your email address. 
		mail.setSubject( 'Test Subject' );
		
		// To store mail body                 
		String mailBody = 'Test Body';
		
		// Specify the text content of the email. 
		mail.setHTMLBody(mailBody);
		
		// Check if Organization email limit exceeded
		try
		{
			// Send the email you have created.
			Messaging.reserveSingleEmailCapacity( 1 );
			Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
		}
		catch( Exception e )	
		{
			// To debug results
			system.debug( 'Email cannnot be send.' );
		}
	}
}
And visualforce page 'Vf_Page' like this:
<apex:page controller="Controller" action="{!sendMailTo}">
	Email sent Successfully
</apex:page>
After this you can create a custom button like this:
User-added image

Let me know if you need any other help.

Thanks,
Neetu
This was selected as the best answer
NNRNNR
Hi Midhun,
please go therow the below link for sending email to multiple users.
http://sfdc4learns.blogspot.in/2014/05/sending-mail-to-multiple-users-throw.html
pls mark as best answer if you have helpfull