I am working on transaction security policy that handles android device version logins using Apex (AndroidLoginEvent.cls)

This class verifies the users device meets criteria for login and if not, will still allow the login but alert that user that the android version needs to be upgraded.

This all works, however, where I am facing the issue is when using the sendEmail functionality. In the logs, it shows that the email is being sent but no email actually appears in the test users inbox.

the code:

global class AndroidLoginEvent implements TxnSecurity.EventCondition {
public String androidVersionThreshold;
public List<Messaging.SingleEmailMessage> emailMessages = new List<Messaging.SingleEmailMessage>();

public AndroidLoginEvent() {
    // Retrieve the Android version threshold from custom metadata type
    Device_Version__mdt[] metadataRecords = [SELECT Android_Version__c FROM Device_Version__mdt LIMIT 1];

    if (!metadataRecords.isEmpty()) {
        androidVersionThreshold = metadataRecords[0].Android_Version__c;
    } else {
        // Handle the case where no custom metadata record is found (set a default value, log an error, etc.)
        androidVersionThreshold = '0'; // Set a default, in this case 0.

public boolean evaluate(SObject event) {
    switch on event {
        when LoginEvent loginEvent {
            // Check if the platform is Android before evaluating
            if (loginEvent.Platform != null && loginEvent.Platform.contains('Android')) {
            } else {
                // Allow the login if the platform is not Android
                return false;
        when null {
            return false;
        when else {
            return false;
    return true; // Always return true to ensure the event is processed

private void evaluate(LoginEvent loginEvent) {
    String platform = loginEvent.Platform;
    String userEmail = loginEvent.User.Email;

    // Check if the platform is Android and version is less than or equal to the threshold
    if (compareAndroidVersions(platform, androidVersionThreshold) <= 0) {
        createEmailAlert(loginEvent.Username, userEmail);

// Helper method to compare Android versions
private Integer compareAndroidVersions(String version1, String version2) {
    // Implement your logic to compare Android versions (e.g., convert to numbers and compare)
    // For simplicity, assuming version format is "Android X" where X is a number
    Integer androidVersion1 = Integer.valueOf(version1.replaceAll('\\D+', ''));
    Integer androidVersion2 = Integer.valueOf(version2.replaceAll('\\D+', ''));

    // Directly compare integers
    return androidVersion1 - androidVersion2;

// Helper method to add an email alert to the list
private void createEmailAlert(String username, String userEmail) {
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

    String[] toAddresses = new String[]{'[email protected]'};

    // Set the email recipient dynamically based on the current login event
    email.setReplyTo('[email protected]'); // this can be set from the custom metadata type
    email.setSubject('Login Blocked - Android Version Check');

    // Set the email body in HTML format
    String emailBody = '<html><body>';
    emailBody += '<p>Dear User,</p>';
    emailBody += '<p>Your login from Android version less than or equal to ' + androidVersionThreshold + ' has been blocked.</p>';
    emailBody += '<p>If you have any questions, please contact support.</p>';
    emailBody += '</body></html>';

    // Add the email to the list instead of sending it immediately

// After evaluating all events, send the accumulated emails
public void sendEmails() {

However, the email never gets delivered to the user whose email is set even though the logs show that its attempted.

**Email deliverability is set to All Email as well.

Second, I also tried to do this more directly compared to adding the emails to a list first, but I am getting: "System.LimitException: Too many Email Invocations: 1" from the automated user in the logs. The limit being 10, not sure why 1 would cause this issue unless TSP's don't allow sendEmail functionality considering the limit of 10.

Any thoughts?

  • 1
    Even though the logs show that its attempted - what logs would those be? If they're Email Logs, please edit your question and show a redacted log entry. Else if they're not SF Email Logs, then it doesn't count.
    – identigral
    Dec 5, 2023 at 17:55


You must log in to answer this question.

Browse other questions tagged .