如何解决我java代码调用microsoft graph api报错

lpz 0 信誉分
2024-03-25T05:48:09.87+00:00

codeUser's image console
User's image

Microsoft Graph
Microsoft Graph
一种 Microsoft 可编程性模型,用于公开 REST API 和客户端库以访问 Microsoft 365 服务上的数据。
25 个问题
0 个注释 无注释
{count} 票

1 个答案

排序依据: 非常有帮助
  1. CarlZhao-MSFT 39,656 信誉分
    2024-03-25T09:04:29.5266667+00:00

    Hi @lpz

    'text/html' 不是一个有效的 contentType,它应该是一个单独的 'text' 或 'html' 值。

    因此请尝试将您的代码段改为:

    GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
    
    com.microsoft.graph.users.item.sendmail.SendMailPostRequestBody sendMailPostRequestBody = new com.microsoft.graph.users.item.sendmail.SendMailPostRequestBody();
    Message message = new Message();
    message.setSubject("Meet for lunch?");
    ItemBody body = new ItemBody();
    body.setContentType(BodyType.Text);
    body.setContent("The new cafeteria is open.");
    message.setBody(body);
    LinkedList<Recipient> toRecipients = new LinkedList<Recipient>();
    Recipient recipient = new Recipient();
    EmailAddress emailAddress = new EmailAddress();
    emailAddress.setAddress("frannis@contoso.com");
    recipient.setEmailAddress(emailAddress);
    toRecipients.add(recipient);
    message.setToRecipients(toRecipients);
    LinkedList<Recipient> ccRecipients = new LinkedList<Recipient>();
    Recipient recipient1 = new Recipient();
    EmailAddress emailAddress1 = new EmailAddress();
    emailAddress1.setAddress("danas@contoso.com");
    recipient1.setEmailAddress(emailAddress1);
    ccRecipients.add(recipient1);
    message.setCcRecipients(ccRecipients);
    sendMailPostRequestBody.setMessage(message);
    sendMailPostRequestBody.setSaveToSentItems(false);
    graphClient.users().byUserId("{user-id}").sendMail().post(sendMailPostRequestBody);
    
    

    User's image


    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.

    Regards.

    Carl