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
nduchnduch 

Cannot send Email from Apex using Visualforce button

HI all,

for some reason my Visualforce button is not sending an email.

 

<apex:page standardController="Contact" extensions="SendEmailPageController" >

<apex:form>
        <apex:pageBlockButtons location="bottom">
                  <apex:commandButton action="{!send}"   value="Send Email" />
                  <apex:commandButton action="{!cancel}" value="Cancel" />
        </apex:pageBlockButtons>

</apex:form>

 

My Controller class:

 

public class SendEmailPageController {

 

public PageReference send() {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'****@yahoo.com'};
        mail.setToAddresses(toAddresses);
        mail.setBccSender(false);
        mail.setSubject('Subject');
        mail.setPlainTextBody('Test Message Body');

 

        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
        for ( Messaging.SendEmailResult result : r ) {

           if ( !r[0].isSuccess () ) {
              
               System.debug ( result  );
           }

           else{

               ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Email Sent Successfully' );
               ApexPages.addMessage(msg);
           }
        }

        return null;

}

 

Any ideas please?

 

Many thanks!!!

Best Answer chosen by Admin (Salesforce Developers) 
nduchnduch

Ok, I solved the mystery.

 

Because my controller is ONLY an extension ofthe standard controller, the only methods that work are the ones that overwrite the methods of the standard controller.

 

<apex:page standardController="Contact" extensions="SendEmailPageController">

 

cancel() is a method of the standard controller and therefore once overwritten by me,  it works. My own methods do not work.

 

So I renamed the send() method to save() (another standard method) and it works! **bleep** the controller extension ...:smileysurprised:

All Answers

hemantgarghemantgarg

what exception it is throwing, please check the debug logs, code is looking fine!

nduchnduch

HI Hemant,

I am checking the logs, the result is success, but for some reason the Messaging code is ignored...

 

Navatar_DbSupNavatar_DbSup

Hi,

 

This is running code of send email of your code and make sure the email id should be valid emailid.

 

//Vf page

<apex:page standardController="Contact" extensions="SendEmailPageController" >
	<apex:form >
		<apex:commandButton action="{!send}"   value="Send Email" />
		<apex:commandButton action="{!cancel}" value="Cancel" />
	</apex:form>
</apex:page>


//Controller

public class SendEmailPageController 
{
	public SendEmailPageController(ApexPages.StandardController controller) 
	{
	}
	public PageReference send() 
	{
		Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
		String[] toAddresses = new String[] {'abc@yahoo.com'};
		mail.setToAddresses(toAddresses);
		mail.setBccSender(false);
		mail.setSubject('Subject');
		mail.setPlainTextBody('Test Message Body');
		Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
		for ( Messaging.SendEmailResult result : r ) 
		{
			if ( !r[0].isSuccess () ) 
			{
				System.debug ( result  );
			}
			else
			{
				ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Email Sent Successfully' );
				ApexPages.addMessage(msg);
			}
		}
		return null;

	}
}

 

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

nduchnduch

Navatar,

this is exactly what I have in my code. Did you change something? I can't see any change that you have made apart fromt he constructor (which I have in my code too).

nduchnduch

HI,

in the debug logs CANCEL button is invoked (... invoke(cancel) ), but the SEND button is completely ignored for some reason. Noooooo idea what is hapenning here.

 

I tried to put a very simple code in the send() method (returning to a previour page), but that is not working either!!!

 

So the problem is not with the Messaging code, but with the button ITSELF !

 

Is it a bug???

nduchnduch

Ok, it is official now - I copy pasted the Messaging code from the SEND button to COPY button, and it worked. Email was sent.

 

Is 'send' a special keyword in Apex? I dont understand why is SEND button ignored ... The definition for both buttons are the same:

 

This works like a charm:

 

public PageReference cancel() {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'***@yahoo.com'};
        mail.setToAddresses(toAddresses);
        mail.setBccSender(false);
        mail.setSubject('Subject');
        mail.setPlainTextBody('Test Message Body');
        
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
        for ( Messaging.SendEmailResult result : r ) {
           if ( !r[0].isSuccess () ) {
               System.debug (result);
           }
           else{
               ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Email Sent Successfully' );
               ApexPages.addMessage(msg);
           }
        }
        return null;
    }

 

And this is ignored:

 

public PageReference send() {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'***@yahoo.com'};
        mail.setToAddresses(toAddresses);
        mail.setBccSender(false);
        mail.setSubject('Subject');
        mail.setPlainTextBody('Test Message Body');
        
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
        for ( Messaging.SendEmailResult result : r ) {
           if ( !r[0].isSuccess () ) {
               System.debug (result);
           }
           else{
               ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Email Sent Successfully' );
               ApexPages.addMessage(msg);
           }
        }
        return null;
    }

 

What a Mystery !!!

hemantgarghemantgarg

Amazing , it should not be so , We can name our button as "Send" .

nduchnduch

Hi,

I think the problem must be somewhere else.

 

I have done some tests and ONLY 1 of those buttons works correctly at a time. If one works, the other not.

nduchnduch

Ok, I solved the mystery.

 

Because my controller is ONLY an extension ofthe standard controller, the only methods that work are the ones that overwrite the methods of the standard controller.

 

<apex:page standardController="Contact" extensions="SendEmailPageController">

 

cancel() is a method of the standard controller and therefore once overwritten by me,  it works. My own methods do not work.

 

So I renamed the send() method to save() (another standard method) and it works! **bleep** the controller extension ...:smileysurprised:

This was selected as the best answer
hemantgarghemantgarg

I dont think so, It should not be the reason, we can add our own methods and actions in extensions. reason must be something else.