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
tchrisbakertchrisbaker 

Sending an email from Customer Portal

We have a custom object which has an embedded VF Page on it. This object is available on Customer Portal. When one of the buttons is pressed by the CP user, an email should go out. When I am logged into SF (not customer portal), this button works fine. However, when I am logged into Customer Portal the email does not work. The email is sent using Apex code. 

 

Is there something special I have to do to email someone from within the CP. 

 

public void sendDeclineEmail() {
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { 
            composeSingleEmailMessage(
                'tchrisbaker@gmail.com', null, null, 
                null, 'Test', 
                'test', 'test'
            )
        });
    }

 

 

 

private static Messaging.SingleEmailMessage composeSingleEmailMessage(
            string toAddress, string ccAddress, string replyToEmail, 
            string senderDisplayName, string subject, 
            string plainTextBody, string htmlBody
    ) {
        // Now create a new single email message object 
        // that will send out a single email to the addresses in the To, CC & BCC list. 
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        if(!DI_Helper.isValidEmail(toAddress)) {
            return mail;
        }
        
        // First, reserve email capacity for the current Apex transaction to ensure 
        // that we won't exceed our daily email limits when sending email after 
        // the current transaction is committed.
        integer numberOfEmails = 1;
        if(DI_Helper.isValidEmail(ccAddress)) {
            numberOfEmails++;
        }
        Messaging.reserveSingleEmailCapacity(numberOfEmails);
        
        // Processes and actions involved in the Apex transaction occur next, 
        // which conclude with sending a single email. 
        
        
        // Strings to hold the email addresses to which you are sending the email. 
        String[] toAddresses = new String[] {toAddress}; 
        String[] ccAddresses;
        if(numberOfEmails > 1) {
            ccAddresses = new String[] {ccAddress};
        }
          
        // Assign the addresses for the To and CC lists to the mail object. 
        mail.setToAddresses(toAddresses);
        mail.setCcAddresses(ccAddresses);
        
        // Specify the address used when the recipients reply to the email.  
        if(DI_Helper.isValidEmail(replyToEmail)) {
            mail.setReplyTo(replyToEmail);
        }
        
        // Specify the name used as the display name. 
        if(senderDisplayName != null && senderDisplayName.length() > 0) {
            mail.setSenderDisplayName(senderDisplayName);
        }
        
        // Specify the subject line for your email address.
        if(subject != null && subject.length() > 0) {
            mail.setSubject(subject);
        }
        
        // Set to True if you want to BCC yourself on the email. 
        //mail.setBccSender(false);
        
        // Optionally append the salesforce.com email signature to the email. 
        // The email address of the user executing the Apex Code will be used. 
        //mail.setUseSignature(false);
        
        // Specify the text content of the email.
        if(plainTextBody != null && plainTextBody.length() > 0) {
            mail.setPlainTextBody(plainTextBody);
        }
        if(htmlBody != null && htmlBody.length() > 0) {
            mail.setHtmlBody(htmlBody);
        }
        
        return mail;
    }

 

Best Answer chosen by Admin (Salesforce Developers) 
tchrisbakertchrisbaker

I resolved this by adding my Apex Class to the CP user's profile.

All Answers

Santhosh KumarSanthosh Kumar

I would interested to know why we cannot send but to solve your problem for now, try sending the email in future method.

tchrisbakertchrisbaker
    @future
    public static void sendDeclineEmail() {
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { 
            composeSingleEmailMessage(
                'tchrisbaker@gmail.com', null, null, 
                null, 'Test', 
                'test', 'test'
            )
        });
    }

 I tried the above change but I didn't have any luck.

tchrisbakertchrisbaker

I resolved this by adding my Apex Class to the CP user's profile.

This was selected as the best answer