You need to sign in to do that
Don't have an account?
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!!!
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
what exception it is throwing, please check the debug logs, code is looking fine!
HI Hemant,
I am checking the logs, the result is success, but for some reason the Messaging code is ignored...
Hi,
This is running code of send email of your code and make sure the email id should be valid emailid.
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
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).
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???
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 !!!
Amazing , it should not be so , We can name our button as "Send" .
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.
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:
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.