0

I am not able to send email using the following code. Can someone suggest where am i going wrong?

VF Page:

<apex:page StandardController="Order__c" extensions="SendEmail"> 
    <apex:pageblock title="">
        <apex:pageMessages ></apex:pageMessages>
        <apex:form >            
            <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>

Controller:

Public class SendEmail {
    public String subject {get; set;}
    public String body {get; set;}

    private Order__c order;

    // Constructor to populate instance of your object
    public SendEmail(ApexPages.StandardController controller) {
        this.order = (Order__c )controller.getRecord();
        order = [SELECT name  FROM Order__c
                 WHERE id = :ApexPages.CurrentPage().getParameters().get('id')];
    }

    public Order__c getOrder() {
        return order;
    }

    public pageReference send() {
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();   

        String[] toaddress = new String[]{};
        toaddress.add('[email protected]');
        email.setSubject(subject);
        email.setToAddresses(toaddress);//Use SOQL to retrieve addresses in the address
        email.setPlainTextBody(body);
        email.setBccSender(true);

        Messaging.SendEmailResult [] res = Messaging.SendEmail(new Messaging.SingleEmailMessage[] {email});      
        for ( Messaging.sendEmailResult result : res ) {

            if ( !res[0].isSuccess () ) {               
                System.debug ( result  );
            } else {                
                ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Email Sent Successfully' );
                ApexPages.addMessage(msg);
            }
        }
        return null;   
    }
}
7
  • and what error do you get?
    – cropredy
    Commented Jul 26, 2014 at 17:05
  • I am not getting any error but the email is not getting fired. Commented Jul 26, 2014 at 17:06
  • did you look in Setup | Monitoring | Email Log Files to see if SFDC sent the email but perhaps it bounced or was otherwise undeliverable?
    – cropredy
    Commented Jul 26, 2014 at 17:08
  • 1
    Also I believe this is in the Sandbox, so go to Email Administration -> Deliverability and check if the "Access to Send Email" is set to "All Emails".
    – AslamK
    Commented Jul 26, 2014 at 17:11
  • I am getting 451 4.7.5 [internal] SSL certificate subject does not match host message in logs . What is meant by this? Commented Jul 26, 2014 at 17:19

1 Answer 1

0

You can use this code if you want to send an email and use this method name as the action in your vf page command button :

public static void sendMail(String toAddress, String subject, String body) {
      list<Messaging.SingleEmailMessage> emailSend = new list<Messaging.SingleEmailMessage>();
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {toAddress};
        mail.setToAddresses(toAddresses);
        mail.setSubject(subject);
        body = '@@@@@@@Message to be passed in the mail' + 'Sincerely xyz';
     mail.setHtmlBody(body);  
  emailSend.add(mail);  
  if(!emailSend.isEmpty()){
        Messaging.sendEmail(emailSend);
   }     

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .