Error when sending email to a newly created user via Microsoft Graph Api

Tien Dang 81 Reputation points
2022-01-06T07:15:20.473+00:00

I'm trying to send a welcome new user email to a newly created user, which is retrieved by:

https://learn.microsoft.com/en-us/graph/api/user-post-users
Send-EmailMessage (supported by powershell)

But I cannot send email automatically to the new user due to an error

emailUPN wasn't found at domain.com.

It is now resolved by signing in the new mailbox, sending email again and work. My question: is there any way to send email to new user without signing-in first?
More information: this is run in cloud-only environment, so I cannot use EXO Powershell command (which is known as on prem environment)

Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. JanardhanaVedham-MSFT 3,566 Reputation points
    2022-01-06T17:48:41.697+00:00

    Hi @Tien Dang ,

    I tested this use case scenario at my end and it is working fine as expected and the email is being successfully sent to the newly created users (without signing-in first) using Microsoft Graph API. I used the below APIs and sample JSON request body.

    Note : We would need to wait for 2 to 3 minutes for SMTP email configuration / mapping to happen for the newly created & licensed user before sending the email As per my testing, new user mail box became available/active after 2 minutes and welcome email was succesfully sent.

    1. Create User (Sample Request) :

    POST https://graph.microsoft.com/v1.0/users  
    {  
    "accountEnabled": true,  
    "displayName": "TestUser111",  
    "mailNickname": "TestUser111",  
    "usageLocation": "IN",  
    "userPrincipalName": "******@o365XXXX.onmicrosoft.com",  
    "passwordProfile": {  
        "forceChangePasswordNextSignIn": false,  
        "password": "XXXXX"  
        }  
    }  
    

    Note : Make sure to set "usageLocation" propery while creating the user as shown in above sample JSON request body. A two letter country code (ISO standard 3166). This property is required for users that will be assigned licenses due to legal requirement to check for availability of services in countries. Examples include: US, IN, JP, and GB.

    163166-image.png

    2. Assign license / subscription to the newly created user (Sample Request) :

    POST /users/{id | userPrincipalName}/assignLicense

    POST https://graph.microsoft.com/v1.0/users/******@o365XXXX.onmicrosoft.com/assignLicense  
    {  
        "addLicenses": [  
            {  
                "skuId": "c42b9cae-ea4f-4ab7-9717-81576235ccac"  
            }  
        ],  
        "removeLicenses": []  
    }  
    

    Note : Product names and service plan identifiers for licensing. GUID of products can be refered in this documentation and GUID must be used to set "skuId" in the above JSON request body.
    163206-image.png.

    3. Wait for 2 minutes and then Send Email (Sample Request) :

    POST https://graph.microsoft.com/v1.0/me/sendMail   
     {  
        "message": {  
            "subject": "Welcome Email Test",  
            "body": {  
                "contentType": "Text",  
                "content": "Welcome User"  
            },  
            "toRecipients": [  
                {  
                    "emailAddress": {  
                        "address": "******@o365XXXX.onmicrosoft.com"  
                    }  
                }  
            ]  
        }  
    }  
    

    Note : We would need to wait for 2 to 3 minutes for SMTP email configuration / mapping to happen for the newly created & licensed user before sending the email As per my testing, new user mail box became available/active after 2 minutes and welcome email was succesfully sent.

    163263-image.png

    Final outout is that the new user has received welcome email as shown below :

    163244-image.png

    Hope this helps.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it, so that it will be useful for other community users. If you have any further questions about this answer, please click "Comment".

    2 people found this answer helpful.

  2. Tien Dang 81 Reputation points
    2022-01-13T11:02:28.64+00:00

    @JanardhanaVedham-MSFT I know your way above work, waiting couple of minutes, but I look for a better answer, like the way to know exactly when the mailbox ready for us to send email.

    I did some of things: get-mailbox, get-user, get-license, all of them existing, then I send email but still failed

    I hope someone has better solution for this!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.