Getting UnknowError while send mail using MS Graph API

Sandesh Santram Giram 0 Reputation points
2023-06-13T08:44:34.7766667+00:00

getting below error while sending the mail. I have confirmed, Credentials are correct and inbox exist for the mentioned mail. also, it has application level permission granted to send the mail. So, what might the reason I am getting it?

com.microsoft.graph.http.GraphServiceException: Error code: UnknownError

Error message: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">

<HTML><HEAD><TITLE>Not Found</TITLE>

<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>

<BODY><h2>Not Found</h2>

<hr><p>HTTP Error 404. The requested resource is not found.</p>

</BODY></HTML>

 

 

POST https://graph.microsoft.com/v1.0/users/******@kiaindia.net/sendMail

Content-Type : text/plain

SdkVersion : graph-java/v5.27.0

[...]

 

404 : Not Found

[...]
Outlook Windows Classic Outlook for Windows For business
Microsoft Security Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 46,371 Reputation points
    2023-06-14T08:00:27.9833333+00:00

    Hi @Sandesh Santram Giram

    1. Make sure the user has an O365 license.
    2. Make sure you are using the application context to send mail on behalf of the user, not the delegated context.

    Refer to the complete sample code:

    final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
                    .clientId("xxxxxxxxxxxxxx")
                    .clientSecret("xxxxxxxxxxxxx")
                    .tenantId("xxxxxxxxxxxxxxxxxxxx")
                    .build();
    
            List<String> scopes = new ArrayList<>();
            scopes.add("https://graph.microsoft.com/.default");
    
            final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(scopes, clientSecretCredential);
    
            final GraphServiceClient graphClient =
                    GraphServiceClient
                            .builder()
                            .authenticationProvider(tokenCredentialAuthProvider)
                            .buildClient();
    
            Message message = new Message();
            message.subject = "xxxxxxxxxxxxxx";
            ItemBody body = new ItemBody();
            body.contentType = BodyType.TEXT;
            body.content = "xxxxxxxxxxxx";
            message.body = body;
            LinkedList<Recipient> toRecipientsList = new LinkedList<Recipient>();
            Recipient toRecipients = new Recipient();
            EmailAddress emailAddress = new EmailAddress();
            emailAddress.address = "xxxxxxxxxxxxx";
            toRecipients.emailAddress = emailAddress;
            toRecipientsList.add(toRecipients);
            message.toRecipients = toRecipientsList;
            LinkedList<Recipient> ccRecipientsList = new LinkedList<Recipient>();
            Recipient ccRecipients = new Recipient();
            EmailAddress emailAddress1 = new EmailAddress();
            emailAddress1.address = "xxxxxxxxxxxxxxxxx";
            ccRecipients.emailAddress = emailAddress1;
            ccRecipientsList.add(ccRecipients);
            message.ccRecipients = ccRecipientsList;
    
            boolean saveToSentItems = false;
    
            graphClient.users("xxxxxxxxxxxxxxxxx")
                    .sendMail(UserSendMailParameterSet
                            .newBuilder()
                            .withMessage(message)
                            .withSaveToSentItems(saveToSentItems)
                            .build())
                    .buildRequest()
                    .post();
    
    

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


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.