0

I'm using inboundEmail service to change current login user context for some reason. To do so I have to send a email to a service address and handle it in the handleInboundEmail method

Thing is that, if current login user's email address is fake or invalid which means the email address cannot send no email to the service address, so the service address will not receive the InboundEmail and will not trigger the handler method.

So the key point is to check whether the current login user's email address is valid or not before I go to the Messaging.sendEmail(), If the email is invalid, I throw an exception. Is there any solution?

CHANGE CONTEXT EXPLAINATION

I want to change the context of the user who is currently logged in to the 'System admin' because I need to reassign a workitem in a process.

It turns out that only a system admin can reaasign a ProcessInstanceWorkitem.

here is the Apex code:

    workItems = [
      SELECT Id, OriginalActorId, Actor.Name, ActorId
      FROM ProcessInstanceWorkitem
      WHERE
        ProcessInstance.TargetObjectId = :targetId
        AND ProcessInstance.Status = 'Pending'
    ];
    workItems[i].ActorId = id;
    workItems[i].OriginalActorId = id;
    Database.SaveResult[] srList = Database.update(workItems, false);

I tried to run the code in a without sharing controller with a non-admin user and failed. Only system admin can run the code above without throwing any exception.

So I have to change the context to system admin using EmailServicesAddress.

2
  • 3
    Please edit to describe why you need to change the context of the user who is currently logged in, and why you want to perform this action by email. Jul 2, 2021 at 18:36
  • @DavidCheng Thank you for your comment, I updated my question.
    – Lanjinest
    Jul 5, 2021 at 7:02

2 Answers 2

1

The Email field on the User field is already validated for you. You don't have a way to know if it's "fake" though, no regular expression or validation can help you here; you'd have to actually send the message and see if it bounces; you won't get an immediate response because emails are sent asynchronously. An invalid email, by contrast, cannot be saved in an Email field. So, realistically, there's nothing you need to do, just send the email.

0

You can try using the regular expression to find out if the email is valid before sending out the email.

Try using the following method https://developer.salesforce.com/forums/?id=906F000000092GXIAY

public static Boolean validateEmail(String email) {
        Boolean res = true;
        
        
        String emailRegex = '^[a-zA-Z0-9._|\\\\%#~`=?&/$^*!}{+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$'; // source: http://www.regular-expressions.info/email.html
        Pattern MyPattern = Pattern.compile(emailRegex);
        Matcher MyMatcher = MyPattern.matcher(email);
        
        if (!MyMatcher.matches()) 
            res = false;
        return res; 
    }  

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .