So I have created a button on a Visualforce page to call the controller to send an email like so. It works perfectly for valid emails but little did I know the hell that came when I sending it to invalid emails.
When I mean invalid emails here are some I found on the Email logs:
1) 421 4.4.0 [internal] no mail servers for this domain could be reached at this time
2) 421 Too many concurrent SMTP connections; please try again later.
3) 451 4.4.2 [internal] no MAIL FROM response
4) 451 4.4.2 [internal] send BODY failed
5) 451 4.7.0 Temporary server error. Please try again later. PRX5 NextHop: substrate-int.office.com. RemoteHost: HKAPR04CA0009. Network Error: 10054 [HK2APC01FT038.eop-APC01.prod.protection.outlook.com]
6) 451 DT:SPM 163 mx1,M8CowABXCgG6kddeP6XKDQ--.23718S3 1591185887, please try again 15min later
7) 451 Temporarily unable to process your email. Please try again later.
8) 451 Temporary local problem - please try later
9) 452 4.3.1 Insufficient system resources (TSTE) [TYAPR04MB2381.apcprd04.prod.outlook.com] [HK2PR02CA0201.apcprd02.prod.outlook.com] [HK2APC01FT028.eop-APC01.prod.protection.outlook.com]
10) 452 4.7.1 <email>: Recipient address rejected: Users Mailbox Full
11) 454 4.4.4 [internal] no MX or A for domain
12) 550 5.5.0 Requested action not taken: mailbox unavailable (S2017062302). [CY1NAM02FT035.eop-nam02.prod.protection.outlook.com]
13) 550-5.1.1 The email account that you tried to reach does not exist. Please try\r\n550-5.1.1 double-checking the recipient's email address for typos or\r\n550-5.1.1 unnecessary spaces. Learn more at\r\n550 5.1.1 https://support.google.com/mail/?p=NoSuchUser nu3si2097233pjb.85 - gsmtp
14) 554 5.4.7 [internal] exceeded max time without delivery
See this code:
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
for (xxx){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage ();
email.setTemplateId(emailTemplates[0].Id);
email.setSaveAsActivity(false);
email.setToAddresses(addresses);
emails.add(email);
}
List<Messaging.SendEmailResult> results = Messaging.sendEmail(emails);
//Catch invalid emails and expose to VF page
for(Messaging.SendEmailResult ser : results){
if (ser.isSuccess()) {
Apexpages.addMessage(new apexpages.message(Apexpages.Severity.Info, ser + ' '));
} else {
for(Messaging.SendEmailError err : ser.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('fields that affected this error: ' + err.getFields());
Apexpages.addMessage(new apexpages.message(Apexpages.Severity.Error, 'Status Code: ' + err.getStatusCode() + ' Message: ' + err.getMessage() + ' Field Affected: ' + err.getFields() ));
}
}
}
Some how when I press the button to Email. It spits out the success toast message and the Developer console will spit out a debug log without any error every 6 seconds :
Debug Logs if I click into 1 of them:
46.0 APEX_CODE,FINE;APEX_PROFILING,NONE;CALLOUT,FINEST;DB,FINEST;NBA,NONE;SYSTEM,FINE;VALIDATION,NONE;VISUALFORCE,NONE;WAVE,NONE;WORKFLOW,NONE
08:16:23.0 (242543)|EXECUTION_STARTED
08:16:23.0 (259142)|CODE_UNIT_STARTED|[EXTERNAL]|0667xF000006oxVdf|VF: /apex/Invoicing
08:16:23.0 (13312753)|SYSTEM_MODE_ENTER|true
08:16:23.0 (13441311)|METHOD_ENTRY|[1]|01px7F0000x0LkNyP|InvoicingController.InvoicingController()
08:16:23.0 (13458226)|METHOD_EXIT|[1]|InvoicingController
08:16:23.0 (13605579)|SYSTEM_MODE_ENTER|true
08:16:23.0 (13770344)|SYSTEM_MODE_ENTER|true
08:16:23.0 (13856568)|SYSTEM_MODE_ENTER|true
08:16:23.0 (14154201)|SYSTEM_MODE_ENTER|true
08:16:23.0 (19599840)|SYSTEM_MODE_ENTER|true
08:16:23.0 (21998767)|SYSTEM_METHOD_ENTRY|[134]|InvoicingController.__sfdc_AttachId()
08:16:23.0 (22040027)|SYSTEM_METHOD_EXIT|[134]|InvoicingController.__sfdc_AttachId()
08:16:23.0 (22087004)|SYSTEM_METHOD_ENTRY|[137]|InvoicingController.__sfdc_invoicePDFAttMap(Map<Id,String>)
08:16:23.0 (22103726)|SYSTEM_METHOD_EXIT|[137]|InvoicingController.__sfdc_invoicePDFAttMap(Map<Id,String>)
08:16:23.0 (22112592)|SYSTEM_METHOD_ENTRY|[138]|InvoicingController.__sfdc_invoiceList()
08:16:23.0 (22122625)|SYSTEM_METHOD_EXIT|[138]|InvoicingController.__sfdc_invoiceList()
08:16:23.0 (22189795)|SYSTEM_METHOD_ENTRY|[138]|List<Invoice__c>.size()
08:16:23.0 (22202267)|SYSTEM_METHOD_EXIT|[138]|List<Invoice__c>.size()
08:16:23.0 (22248651)|SYSTEM_CONSTRUCTOR_ENTRY|[139]|<init>(Integer)
08:16:23.0 (22280784)|SYSTEM_CONSTRUCTOR_EXIT|[139]|<init>(Integer)
08:16:23.0 (22304748)|SYSTEM_METHOD_ENTRY|[140]|InvoicingController.__sfdc_invoiceList()
08:16:23.0 (22330237)|SYSTEM_METHOD_EXIT|[140]|InvoicingController.__sfdc_invoiceList()
08:16:23.0 (22346301)|SYSTEM_METHOD_ENTRY|[140]|List<Invoice__c>.iterator()
08:16:23.0 (22474025)|SYSTEM_METHOD_EXIT|[140]|List<Invoice__c>.iterator()
08:16:23.0 (22498553)|SYSTEM_METHOD_ENTRY|[140]|system.ListIterator.hasNext()
08:16:23.0 (22521036)|SYSTEM_METHOD_EXIT|[140]|system.ListIterator.hasNext()
08:16:23.0 (22624700)|SYSTEM_METHOD_ENTRY|[141]|Set<Id>.add(Object)
08:16:23.0 (22663304)|SYSTEM_METHOD_EXIT|[141]|Set<Id>.add(Object)
08:16:23.0 (22674726)|SYSTEM_METHOD_ENTRY|[142]|InvoicingController.__sfdc_invoicePDFAttMap()
08:16:23.0 (22685924)|SYSTEM_METHOD_EXIT|[142]|InvoicingController.__sfdc_invoicePDFAttMap()
08:16:23.0 (22715409)|SYSTEM_METHOD_ENTRY|[142]|Map<Id,String>.put(Object, Object)
08:16:23.0 (22729853)|SYSTEM_METHOD_EXIT|[142]|Map<Id,String>.put(Object, Object)
08:16:23.0 (22738630)|SYSTEM_METHOD_ENTRY|[140]|system.ListIterator.hasNext()
08:16:23.0 (22748831)|SYSTEM_METHOD_EXIT|[140]|system.ListIterator.hasNext()
08:16:23.0 (22770388)|SYSTEM_METHOD_ENTRY|[141]|Set<Id>.add(Object)
08:16:23.0 (22779678)|SYSTEM_METHOD_EXIT|[141]|Set<Id>.add(Object)
08:16:23.0 (22785850)|SYSTEM_METHOD_ENTRY|[142]|InvoicingController.__sfdc_invoicePDFAttMap()
08:16:23.0 (22791789)|SYSTEM_METHOD_EXIT|[142]|InvoicingController.__sfdc_invoicePDFAttMap()
08:16:23.0 (22804489)|SYSTEM_METHOD_ENTRY|[142]|Map<Id,String>.put(Object, Object)
08:16:23.0 (22826466)|SYSTEM_METHOD_EXIT|[142]|Map<Id,String>.put(Object, Object)
08:16:23.0 (22833300)|SYSTEM_METHOD_ENTRY|[140]|system.ListIterator.hasNext()
08:16:23.0 (22854234)|SYSTEM_METHOD_EXIT|[140]|system.ListIterator.hasNext()
08:16:23.0 (22863836)|SYSTEM_METHOD_ENTRY|[145]|Set<Id>.size()
08:16:23.0 (22877083)|SYSTEM_METHOD_EXIT|[145]|Set<Id>.size()
08:16:23.0 (24803336)|SOQL_EXECUTE_BEGIN|[146]|Aggregations:0|SELECT id, parentId FROM Attachment
08:16:23.0 (28930748)|SOQL_EXECUTE_END|[146]|Rows:2
08:16:23.0 (29023245)|SYSTEM_METHOD_ENTRY|[146]|Database.QueryLocator.iterator()
08:16:23.0 (29172496)|SYSTEM_METHOD_ENTRY|[7]|QueryLocatorIterator.QueryLocatorIterator()
08:16:23.0 (29186945)|SYSTEM_METHOD_EXIT|[7]|QueryLocatorIterator
08:16:23.0 (29376502)|SYSTEM_METHOD_EXIT|[146]|Database.QueryLocator.iterator()
08:16:23.0 (29408348)|METHOD_ENTRY|[146]||Database.QueryLocatorIterator.hasNext()
08:16:23.0 (29498555)|METHOD_EXIT|[146]||Database.QueryLocatorIterator.hasNext()
08:16:23.0 (29512041)|METHOD_ENTRY|[146]||Database.QueryLocatorIterator.next()
08:16:23.0 (29583621)|METHOD_EXIT|[146]||Database.QueryLocatorIterator.next()
08:16:23.0 (29598561)|SYSTEM_METHOD_ENTRY|[147]|InvoicingController.__sfdc_invoicePDFAttMap()
08:16:23.0 (29613987)|SYSTEM_METHOD_EXIT|[147]|InvoicingController.__sfdc_invoicePDFAttMap()
08:16:23.0 (29687170)|SYSTEM_METHOD_ENTRY|[147]|Map<Id,String>.put(Object, Object)
08:16:23.0 (29725618)|SYSTEM_METHOD_EXIT|[147]|Map<Id,String>.put(Object, Object)
08:16:23.0 (29735629)|METHOD_ENTRY|[146]||Database.QueryLocatorIterator.hasNext()
08:16:23.0 (29753927)|METHOD_EXIT|[146]||Database.QueryLocatorIterator.hasNext()
08:16:23.0 (29760129)|METHOD_ENTRY|[146]||Database.QueryLocatorIterator.next()
08:16:23.0 (29798424)|METHOD_EXIT|[146]||Database.QueryLocatorIterator.next()
08:16:23.0 (29807785)|SYSTEM_METHOD_ENTRY|[147]|InvoicingController.__sfdc_invoicePDFAttMap()
08:16:23.0 (29813452)|SYSTEM_METHOD_EXIT|[147]|InvoicingController.__sfdc_invoicePDFAttMap()
08:16:23.0 (29834644)|SYSTEM_METHOD_ENTRY|[147]|Map<Id,String>.put(Object, Object)
08:16:23.0 (29854927)|SYSTEM_METHOD_EXIT|[147]|Map<Id,String>.put(Object, Object)
08:16:23.0 (29862649)|METHOD_ENTRY|[146]||Database.QueryLocatorIterator.hasNext()
08:16:23.0 (29871409)|METHOD_EXIT|[146]||Database.QueryLocatorIterator.hasNext()
08:16:23.0 (38685737)|SYSTEM_MODE_ENTER|true
08:16:23.0 (38835424)|SYSTEM_MODE_ENTER|true
08:16:23.0 (38931168)|SYSTEM_MODE_ENTER|true
08:16:23.0 (38998425)|SYSTEM_MODE_ENTER|true
08:16:23.0 (39273616)|SYSTEM_MODE_ENTER|true
08:16:23.0 (39675353)|SYSTEM_MODE_ENTER|true
08:16:23.0 (47569436)|SYSTEM_MODE_ENTER|true
08:16:23.0 (99101833)|CODE_UNIT_FINISHED|VF: /apex/Invoicing
08:16:23.0 (99119007)|EXECUTION_FINISHED
- How do I force stop this email trying to be sent?
- How do I catch the error and display it to my visualforce page accordingly?