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
RaffiRaffi 

what am I missing?

Hello,

 

I created this very simple VS page and supporting cotroller to send out an email upon page load. The send() function executes fine, but the recepient does not get the email.  I've succesfully run the Email Deliverability Tool and checked corporate mail filter without success.  I must be missing something very obvious here.

 

Thanks,

Raf 

 

 

<apex:page controller="sendEmailController">
  <apex:outputText value="{!errorMessage}"/>

</apex:page>


public class sendEmailController {

    public String errorMessage {get; set;}

    public sendEmailController(){
           send();
    }
    
    public PageReference send(){

        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
        email.setSubject('Test email from Salesforce');
        String[] toAddresses = new String[] {'username@company.com'};
        email.setToAddresses(toAddresses);
        email.setPlainTextBody('sent from quoteSendToCop');
        
        Messaging.SendEmailResult [] r = 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
        
        if (!r.get(0).isSuccess()) {
            System.StatusCode statusCode = r.get(0).getErrors()[0].getStatusCode();
            errorMessage = r.get(0).getErrors()[0].getMessage();
        } else {errorMessage = 'success';}
            
        return null;
    }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
RaffiRaffi

I got it sorted out by adding action="{!send}" attribute to visualforce page tag like so:

<apex:page controller="sendEmailController" action="{!send}">
<apex:outputText value="{!errorMessage}"/>
</apex:page>

 

Thanks for the help,

Raffi

All Answers

abhishektandon2abhishektandon2

Plz share the debug logs

RaffiRaffi
24.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
15:18:38.176 (176849000)|EXECUTION_STARTED
15:18:38.176 (176892000)|CODE_UNIT_STARTED|[EXTERNAL]|066G0000001KWY4|VF: /apex/sendEmail
15:18:38.178 (178742000)|CODE_UNIT_STARTED|[EXTERNAL]|01pG0000002bZ6n|sendEmailController <init>
15:18:38.178 (178765000)|SYSTEM_MODE_ENTER|true
15:18:38.179 (179545000)|METHOD_ENTRY|[1]|01pG0000002bZ6n|sendEmailController.sendEmailController()
15:18:38.179 (179682000)|METHOD_EXIT|[1]|sendEmailController
15:18:38.179 (179817000)|METHOD_ENTRY|[6]|01pG0000002bZ6n|sendEmailController.send()
15:18:38.180 (180990000)|SYSTEM_METHOD_ENTRY|[13]|LIST.add(ANY)
15:18:38.181 (181043000)|SYSTEM_METHOD_EXIT|[13]|LIST.add(ANY)
15:18:38.181 (181165000)|SYSTEM_METHOD_ENTRY|[17]|LIST.add(ANY)
15:18:38.181 (181211000)|SYSTEM_METHOD_EXIT|[17]|LIST.add(ANY)
15:18:38.181 (181240000)|SYSTEM_METHOD_ENTRY|[17]|Messaging.sendEmail(LIST<Messaging.Email>)
15:18:38.192 (192688000)|EMAIL_QUEUE|[17]|subject: Test email from Salesforce, bccSender: false, saveAsActivity: true, useSignature: true, toAddresses: [rafal@kanatek.com], plainTextBody: sent from quoteSendToCop, 
15:18:38.192 (192968000)|SYSTEM_METHOD_EXIT|[17]|Messaging.sendEmail(LIST<Messaging.Email>)
15:18:38.193 (193024000)|SYSTEM_METHOD_ENTRY|[20]|LIST.get(Integer)
15:18:38.193 (193114000)|SYSTEM_METHOD_EXIT|[20]|LIST.get(Integer)
15:18:38.193 (193222000)|METHOD_ENTRY|[23]|01pG0000002bZ6n|sendEmailController.__sfdc_errorMessage(String)
15:18:38.193 (193325000)|METHOD_EXIT|[23]|01pG0000002bZ6n|sendEmailController.__sfdc_errorMessage(String)
15:18:38.193 (193350000)|METHOD_EXIT|[6]|01pG0000002bZ6n|sendEmailController.send()
15:18:38.193 (193392000)|CODE_UNIT_FINISHED|sendEmailController <init>
15:18:38.293 (293948000)|CODE_UNIT_STARTED|[EXTERNAL]|01pG0000002bZ6n|sendEmailController get(errorMessage)
15:18:38.293 (293982000)|SYSTEM_MODE_ENTER|true
15:18:38.294 (294008000)|CODE_UNIT_STARTED|[EXTERNAL]|01pG0000002bZ6n|errorMessage
15:18:38.294 (294033000)|CODE_UNIT_FINISHED|errorMessage
15:18:38.294 (294045000)|CODE_UNIT_FINISHED|sendEmailController get(errorMessage)
15:18:38.209 (303403000)|CUMULATIVE_LIMIT_USAGE
15:18:38.209|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 10 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 1 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

15:18:38.209|CUMULATIVE_LIMIT_USAGE_END

15:18:38.303 (303450000)|CODE_UNIT_FINISHED|VF: /apex/sendEmail
15:18:38.303 (303461000)|EXECUTION_FINISHED

 Thansk,

Raf

Big VBig V

I have worked with Email classes and all the emails triggered from Apx classes were found in my Spam box.

Ulas KutukUlas Kutuk

Did you set something for 'toAddress' ? It seems like to be coming null value.

just try to set value before this line and check the debugs. 

email.setToAddresses(toAddresses);
RaffiRaffi

Yes, toAddreesses is populated with string array of sizes of 1-4 with my different email addresses ( I tried multiple variants). Nothing in SPAM or junk mail folders for any of those accounts,  no emails logged as coming in either.  Interestingly, a single email message arrived to my gmail account after reloading the page about 10 times. I was also able to create email alert through workflow and that's coming in fine.

Ulas KutukUlas Kutuk

Can you try these similar codes just like yours, I just added an attachment in email,I you do not need skip this part.

 

Attachment at=[Select Id, Name,Body,ContentType,Description 
                                   From Attachment
                                   Where Id=:documentId];
                                        
                    Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
                    
                    attach.setContentType('image/jpeg');
                    attach.setFileName(at.Name);
                    attach.setInline(false);
                    attach.Body = at.Body;
                        
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    mail.setUseSignature(false);
                    mail.setSenderDisplayName(MNCOnfig.Email_Display_Name__c);
                    mail.setToAddresses(new String[] { UserInformationList[0].Email});
                    mail.setSubject(MNConfig.Email_Display_Name__c);
                    mail.setHtmlBody('Here is the attachment you requested: '+at.Description+' This email sent from: '+MNCOnfig.Email_Display_Name__c);
                    mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 
                 
                    // Send the email
                    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                 
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with Attachment sent to '+email));
                    successful='Email with Attachment sent to '+UserInformationList[0].Email;

 

RaffiRaffi

I got it sorted out by adding action="{!send}" attribute to visualforce page tag like so:

<apex:page controller="sendEmailController" action="{!send}">
<apex:outputText value="{!errorMessage}"/>
</apex:page>

 

Thanks for the help,

Raffi

This was selected as the best answer