Microsoft Graph API 不支持发送电子邮件的 MIME(多用途 Internet 邮件扩展)标准(POST 请求)

Microsoft Graph API Not Support MIME (Multipurpose Internet Mail Extensions) Standard for Send Email (POST Request)


**我有一个应用程序可以通过 Microsoft Graph API 集成向一个用户发送电子邮件。*

当我们发送包含 7 位 ASCII 字符的电子邮件时,它正在工作。但它不足以 non-ASCII 文本编码(如 Unicode)、二进制内容或附件。*

那么如何通过Microsoft Graph发送MIME标准数据API,请帮帮我。


MIME:SMTP 协议最初设计用于发送仅包含 7 位 ASCII 字符的电子邮件消息。此规范使 SMTP 不足以支持 non-ASCII 文本编码(如 Unicode)、二进制内容或附件。多用途 Internet 邮件扩展标准 (MIME) 的开发使得使用 SMTP 发送许多其他类型的内容成为可能。

MIME 标准的工作原理是将消息 body 分成多个部分,然后指定对每个部分执行的操作。例如,电子邮件的一部分 body 可能是纯文本,而另一部分可能是 HTML。此外,MIME 允许电子邮件包含一个或多个附件。邮件收件人可以从他们的电子邮件客户端中查看附件,也可以保存附件。

消息header和内容用空行隔开。电子邮件的每个部分都由边界分隔,边界是表示每个部分开始和结束的字符串。欲了解更多信息 click here


请求Body:

{
  "message": {
    "subject": "Special Mail Testing - 23652",
    "body": {
      "contentType": "html",
      "content": "<p>Hi RAJIB GARAI,</p><p>Copy Content :</p><p>In this module, you’ll learn how to manage the lifecycle of groups, the different types of groups and obtain information about the users.</p>
                    <p>Write Content :</p><p>In this module you'll learn how to manage it. Some special characters type from keybord :&nbsp;</p>
                    <p>! @ # $ % ^ &amp; * ( ) _ + = - ~ ` . / * - +&nbsp;</p><p>0 9 8 7 6 5 4 3 2 1&nbsp;</p>
                    <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.&nbsp;</p>
                    <p>The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.</p>
                    <p>Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.
                        Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
                    <p> €   ‚ ƒ „ … † ‡ ˆ ‰ Š ‹ Œ Ž ‘ ’ “ ” • – — ˜ ™ š › œ ž Ÿ ´ µ · º » ¼ û </p>"
    },
    "toRecipients": [
      {
        "emailAddress": {
          "address": "rajibgarai@gamail.com"
        }
      }
    ]
  },
  "saveToSentItems": "true"
}

请求Header:

HttpHeaders headers = new HttpHeaders();

headers.add("Authorization",  KeyConstant.USER_TOKEN);  
headers.setContentType(MediaType.APPLICATION_JSON);

请求URL:KeyConstant.URL_SEND_MAIL=

https://graph.microsoft.com/v1.0/me/sendMail

请求过程

HttpEntity<String> requestBody = new HttpEntity<String>(message, headers);

    try 
    {
        ResponseEntity<String> result = restTemplate.exchange(KeyConstant.URL_SEND_MAIL, HttpMethod.POST, requestBody, String.class);

    }
    catch (org.springframework.web.client.HttpClientErrorException e)
    {
        log.error("Exception occurred while sending email : {} {}", e.getCause() ,e.getMessage());  
    }
    catch (Exception e) 
    {
        log.error("Exception occurred while sending email {} {}", e.getCause(), e.getMessage());
    }

我在 Microsoft Graph API 中找到了支持所有类型特殊字符 (info) 的解决方案,方法是对我的代码进行以下更改。

更改 Header 配置:

HttpHeaders headers = new HttpHeaders();

headers.add("Authorization",  KeyConstant.USER_TOKEN); 
headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // Not MediaType.APPLICATION_JSON

谢谢大家