+ Start a Discussion
KeithJKeithJ 

Error when sending email from Visual Force page

Hi there.
I was wondering could anyone help me out.
Im trying to send an email when a visualforce page is loaded.
I have changed the email addresses for the purpose of this post.
 
Basically I have a simple visualforce page which consists of:
 
<apex:page controller="EmailCardListController" action="sendMail" title="Email Card List">
   <apex:outputText value="{!emailSent}" />
</apex:page>
 
and a relatively simple controller that sends an email (taken pretty much from the Apex pdf guide)
 
public class EmailCardListController {
    //Did the email send successfully?
    public Boolean emailSent { get; set; }
    //Create the email handler;
    public Messaging.SingleEmailMessage mailHandler = new Messaging.SingleEmailMessage();
   
    //The recipient
    String[] emailRecipient = new String[] {'jbloggs@acme.com'};
    public void sendMail() {
   
        //set the recipient
        mailHandler.setToAddresses(emailRecipient);
       
        //set the reply email address
        mailHandler.setReplyTo('jbloggs@acme.com');
       
        //set the display name
        mailHandler.setSenderDisplayName('Keith Nixon');
       
        //set the subject
        mailHandler.setSubject('Card List');
       
        //set the template ID
        //mailHandler.setTemplateId('00X200000015XFL');
       
        mailHandler.setHtmlBody('This is a simple email from me');
       
        try {
            Messaging.sendEmail(new Messaging.Email[] { mailHandler });
            emailSent = true;
        }catch(EmailException e) {
            System.debug(e.getMessage());
            emailSent = false;
        }
    }  
}
 
Unfortunately when I call up the VisualForce page, I get the following error:
 
Illegal view ID sendMail. The ID must begin with /
 
 
Best Answer chosen by Admin (Salesforce Developers) 
dchasmandchasman
Code:
<apex:page controller="EmailCardListController" action="{!sendMail}" title="Email Card List">
<apex:outputText value="{!emailSent}" />
</apex:page>

 You were specifying a literal for your page action instead of an action binding and this is interpreted as display the page named /apex/sendMail.

All Answers

dchasmandchasman
Code:
<apex:page controller="EmailCardListController" action="{!sendMail}" title="Email Card List">
<apex:outputText value="{!emailSent}" />
</apex:page>

 You were specifying a literal for your page action instead of an action binding and this is interpreted as display the page named /apex/sendMail.
This was selected as the best answer
KeithJKeithJ


dchasman, thank you very much.
I overlooked the binding syntax...
Easily done!
Thank you
yespeeyespee

I am facing same problem. Read the documentation but could not figure it out.

Can someone please eloberate action binding and what needs to be changed here.

Thanks.

 

Here is my code: 

 

 

<apex:pageBlockButtons >
<apex:commandButton action="{!drconvert}" value="Convert"/>
<apex:commandButton action="{!backtolead}" value="Cancel"/>
</apex:pageBlockButtons>

 

 

public PageReference backtolead()
{
PageReference leadpage = new PageReference('/' + lead.id);
leadpage.setRedirect(true);
return leadpage;
}

 

 

 Message Edited by yespee on 01-27-2009 11:39 AM

Message Edited by yespee on 01-27-2009 11:48 AM
Vishal_ThoriyaVishal_Thoriya

your post helped me a lot.............

 

thanks for that but when i am trying to send it with file attachment it is giving me error.........

 

 

can you tell me how to send email with any kind of file attachments..............

ziaullahkhaksarziaullahkhaksar

It was really very helpful e-mail sending method

but Please can u modify this to send an e-mail through scheduler I mean to set a specific time to send the e-mail

I will be really very thankful to you for this kindness.