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
Athira VenugopalAthira Venugopal 

SingleEmailMessage() not working

I am working in Visual studio code, I am passing some values to this priceMail() method while calling from the js file. I have got the 'success' msg, but the mail is not send
 @AuraEnabled
    public static String priceMail(String name, String email, Double cost) {
        // Create an email message object
        String sMessage='';
        String subject = 'Price List';
        String body = 'Dear ' + name + '/n' + 'ggg' + cost;
        try{
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {email};
        mail.setToAddresses(toAddresses);
        mail.setSubject(subject);
        mail.setPlainTextBody(body);
        // Pass this email message to the built-in sendEmail method 
        // of the Messaging class
       Messaging.sendEmail( new Messaging.SingleEmailMessage[] { mail });
        sMessage='Success';
        }
        catch(Exception ex){
            sMessage=ex.getLineNumber()+'\n'+ex.getCause()+'\n'+ex.getMessage()+'\n'+ex.getStackTraceString();
        }
        return sMessage;
      
    
    }

 
Brahian Calo 7Brahian Calo 7
Hi Athira Venugopal,
Your code is correct, I've tested on my own org and it works, maybe is a miss configuration under Deliverability. In your salesforce org:
  • Go to the Setup
  • Type "Deliverability" into the Quick find box
  • Click on Deliverability link
  • Change Access Level to "All email" (just to make sure it is related)
  • Finally click on save and try again.
Note I've called your method from the Execute Anonymous Apex to test it

Hope this help you.
Athira VenugopalAthira Venugopal
yes it is working when called from anonymous window. The deliverability is already set as 'All Email'. I am working in Visual studio code in Lightning Web components, and calling this method from the js file, and it is returning the succes message. I don't know why this is not working.