发送包含与用户收件箱中的其他邮件不同的邮件作为附件的新邮件 [英] Send new message that includes a different message from user's inbox as attachment

查看:110
本文介绍了发送包含与用户收件箱中的其他邮件不同的邮件作为附件的新邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理多租户守护程序.该应用程序需要代表用户发送电子邮件.应用程序发送的电子邮件必须包含来自用户收件箱的另一封电子邮件作为附件.

能否仅通过引用现有电子邮件的ID而不下载现有电子邮件的内容来实现?

我在下面尝试这样的事情.使用sendMail api并尝试引用现有的电子邮件项目作为附件.但是我遇到了这个错误:

无法处理抽象类型Microsoft.OutlookServices.Item

的输入

我的方向正确吗?实现此用例的最佳方法是什么.

 POST https://graph.microsoft.com/v1.0/me/sendMail HTTP/1.1
authorization: bearer {access_token}
content-type: application/json
content-length: 96

{
    "message": {
        "subject": "Meet for lunch?",
        "body": {
            "contentType": "Text",
            "content": "The new cafeteria is open."
        },
        "toRecipients": [{
            "emailAddress": {
                "address": "garthf@a830edad9050849NDA1.onmicrosoft.com"
            }
        }],
        "attachments": [
            "@odata.type": "#Microsoft.OutlookServices.ItemAttachment",
            "name": "menu.txt",
            "item": {
                "id": "AAMkADBlOWNlOWExLTdjMzktNDI5NC04MDY3LTRiZGM2NTIxMzUyNABGAAAAAAC11nCh2QXMSJ7F766v_WCUBwBSt5DMAwrBRJGMMbg9jqoYAAAGNiHGAABSt5DMAwrBRJGMMbg9jqoYAAAJXgInAAA="
                <!--This is the id of an existing email in User's inbox -->
            }
        ]
    },
    "saveToSentItems": "false"
}
 

解决方案

您不能仅提供消息的id.如果这样做,您将得到一个空白附件.不幸的是,未设置API来按ID检索项目并为您插入.

所以这意味着您需要自己检索消息,并将整个JSON有效负载包括在附件的item属性中. (请参阅 https://developer. microsoft.com/en-us/graph/docs/api-reference/v1.0/api/message_post_attachments )

您需要了解一个关键的细节:在添加要附加的消息的JSON表示之前,您需要为其添加一个@odata.type属性,并将其设置为microsoft.graph.message.

例如:

获取要附加的邮件

GET /me/messages/{id}

响应

{
  "@odata.etag": "W/\"CQAAABYAAACImwNLdwWFR4SE3YBnGvEfAAAOjRuW\"",
  "id": "AAMkADRkOWJjMjdlLTM3OWMtNDU5ZS05YWZlLTRjMjkwZWE5NWMyYgBGAAAAAACUZXoWyqoWRohmHfOzvx9wBwCImwNLdwWFR4SE3YBnGvEfAAAAAAEMAACImwNLdwWFR4SE3YBnGvEfAAAOi7KdAAA=",
  "createdDateTime": "2017-10-26T12:22:03Z",
  "lastModifiedDateTime": "2017-10-26T12:22:03Z",
  "changeKey": "CQAAABYAAACImwNLdwWFR4SE3YBnGvEfAAAOjRuW",
  "categories": [],
  "receivedDateTime": "2017-10-26T12:22:03Z",
  "sentDateTime": "2017-10-26T12:22:02Z",
  "hasAttachments": false,
  "internetMessageId": "<BLUPR13MB0274245999AAD00105B87333A6450@BLUPR13MB0274.namprd13.prod.outlook.com>",
  "subject": "Test attachment stuff",
  "bodyPreview": "Hello world!",
  "importance": "normal",
  "parentFolderId": "AQMkADRkOWJjMjdlLTM3OWMtNDU5ZS05YWZlLTRjMjkwZWE5NWMyYgAuAAADlGV6FsqqFkaIZh3zs78fcAEAiJsDS3cFhUeEhADdgGca8R8AAAIBDAAAAA==",
  "conversationId": "AAQkADRkOWJjMjdlLTM3OWMtNDU5ZS05YWZlLTRjMjkwZWE5NWMyYgAQAOcAXOsMKqFDlKHlkwvtP0Y=",
  "isDeliveryReceiptRequested": false,
  "isReadReceiptRequested": false,
  "isRead": false,
  "isDraft": false,
  "webLink": "https://outlook.office365.com/owa/?ItemID=AAMkADRkOWJjMjdlLTM3OWMtNDU5ZS05YWZlLTRjMjkwZWE5NWMyYgBGAAAAAACUZXoWyqoWRohmHfOzvx9wBwCImwNLdwWFR4SE3YBnGvEfAAAAAAEMAACImwNLdwWFR4SE3YBnGvEfAAAOi7KdAAA%3D&exvsurl=1&viewmodel=ReadMessageItem",
  "inferenceClassification": "focused",
  "body": {
    "contentType": "html",
    "content": "<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n<meta content=\"text/html; charset=iso-8859-1\">\r\n<style type=\"text/css\" style=\"display:none\">\r\n<!--\r\np\r\n\t{margin-top:0;\r\n\tmargin-bottom:0}\r\n-->\r\n</style>\r\n</head>\r\n<body dir=\"ltr\">\r\n<div id=\"divtagdefaultwrapper\" dir=\"ltr\" style=\"font-size:12pt; color:#000000; font-family:Calibri,Helvetica,sans-serif\">\r\n<p>Hello world!</p>\r\n</div>\r\n</body>\r\n</html>\r\n"
  },
  "sender": {
    "emailAddress": {
      "name": "MOD Administrator",
      "address": "admin@contoso.onmicrosoft.com"
    }
  },
  "from": {
    "emailAddress": {
      "name": "MOD Administrator",
      "address": "admin@contoso.onmicrosoft.com"
    }
  },
  "toRecipients": [
    {
      "emailAddress": {
        "name": "Adele Vance",
        "address": "AdeleV@contoso.onmicrosoft.com"
      }
    }
  ],
  "ccRecipients": [],
  "bccRecipients": [],
  "replyTo": []
}

发送带有附件的新邮件

请注意,在item属性中,在附件消息的JSON表示中添加了"@odata.type": "microsoft.graph.message",.

POST /me/sendmail
Content-Type: application/json

{
  "message": {
    "subject": "Meet for lunch?",
    "body": {
      "contentType": "Text",
      "content": "The new cafeteria is open."
    },
    "toRecipients": [{
      "emailAddress": {
        "address": "adelev@contoso.onmicrosoft.com"
      }
    }],
    "attachments": [
      {
        "@odata.type": "#microsoft.graph.itemAttachment",
        "name": "Test attachment stuff",
        "item": {
          "@odata.type": "microsoft.graph.message",
          "@odata.etag": "W/\"CQAAABYAAACImwNLdwWFR4SE3YBnGvEfAAAOjRuW\"",
          "id": "AAMkADRkOWJjMjdlLTM3OWMtNDU5ZS05YWZlLTRjMjkwZWE5NWMyYgBGAAAAAACUZXoWyqoWRohmHfOzvx9wBwCImwNLdwWFR4SE3YBnGvEfAAAAAAEMAACImwNLdwWFR4SE3YBnGvEfAAAOi7KdAAA=",
          "createdDateTime": "2017-10-26T12:22:03Z",
          "lastModifiedDateTime": "2017-10-26T12:22:03Z",
          "changeKey": "CQAAABYAAACImwNLdwWFR4SE3YBnGvEfAAAOjRuW",
          "categories": [],
          "receivedDateTime": "2017-10-26T12:22:03Z",
          "sentDateTime": "2017-10-26T12:22:02Z",
          "hasAttachments": false,
          "internetMessageId": "<BLUPR13MB0274245999AAD00105B87333A6450@BLUPR13MB0274.namprd13.prod.outlook.com>",
          "subject": "Test attachment stuff",
          "bodyPreview": "Hello world!",
          "importance": "normal",
          "parentFolderId": "AQMkADRkOWJjMjdlLTM3OWMtNDU5ZS05YWZlLTRjMjkwZWE5NWMyYgAuAAADlGV6FsqqFkaIZh3zs78fcAEAiJsDS3cFhUeEhADdgGca8R8AAAIBDAAAAA==",
          "conversationId": "AAQkADRkOWJjMjdlLTM3OWMtNDU5ZS05YWZlLTRjMjkwZWE5NWMyYgAQAOcAXOsMKqFDlKHlkwvtP0Y=",
          "isDeliveryReceiptRequested": false,
          "isReadReceiptRequested": false,
          "isRead": false,
          "isDraft": false,
          "webLink": "https://outlook.office365.com/owa/?ItemID=AAMkADRkOWJjMjdlLTM3OWMtNDU5ZS05YWZlLTRjMjkwZWE5NWMyYgBGAAAAAACUZXoWyqoWRohmHfOzvx9wBwCImwNLdwWFR4SE3YBnGvEfAAAAAAEMAACImwNLdwWFR4SE3YBnGvEfAAAOi7KdAAA%3D&exvsurl=1&viewmodel=ReadMessageItem",
          "inferenceClassification": "focused",
          "body": {
            "contentType": "html",
            "content": "<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n<meta content=\"text/html; charset=iso-8859-1\">\r\n<style type=\"text/css\" style=\"display:none\">\r\n<!--\r\np\r\n\t{margin-top:0;\r\n\tmargin-bottom:0}\r\n-->\r\n</style>\r\n</head>\r\n<body dir=\"ltr\">\r\n<div id=\"divtagdefaultwrapper\" dir=\"ltr\" style=\"font-size:12pt; color:#000000; font-family:Calibri,Helvetica,sans-serif\">\r\n<p>Hello world!</p>\r\n</div>\r\n</body>\r\n</html>\r\n"
          },
          "sender": {
            "emailAddress": {
              "name": "MOD Administrator",
              "address": "admin@contoso.onmicrosoft.com"
            }
          },
          "from": {
            "emailAddress": {
              "name": "MOD Administrator",
              "address": "admin@contoso.onmicrosoft.com"
            }
          },
          "toRecipients": [
            {
              "emailAddress": {
                "name": "Adele Vance",
                "address": "AdeleV@contoso.onmicrosoft.com"
              }
            }
          ],
          "ccRecipients": [],
          "bccRecipients": [],
          "replyTo": []
        }
      }
    ]
  },
  "saveToSentItems": "false"
}

I am working on a multi-tenant daemon application. The application needs to send an email on behalf of the user. The email sent by the application would have to include another email from the user's inbox as attachment.

Can this be achieved by simply referring to the id of the existing email instead of downloading the content of the existing email?

I am trying something like this below. Using the sendMail api and trying to refer the existing email item as an attachment. But I am getting this error :

Cannot process input of abstract type Microsoft.OutlookServices.Item

Am I in the right direction? What is the best way of achieving this use case.

POST https://graph.microsoft.com/v1.0/me/sendMail HTTP/1.1
authorization: bearer {access_token}
content-type: application/json
content-length: 96

{
    "message": {
        "subject": "Meet for lunch?",
        "body": {
            "contentType": "Text",
            "content": "The new cafeteria is open."
        },
        "toRecipients": [{
            "emailAddress": {
                "address": "garthf@a830edad9050849NDA1.onmicrosoft.com"
            }
        }],
        "attachments": [
            "@odata.type": "#Microsoft.OutlookServices.ItemAttachment",
            "name": "menu.txt",
            "item": {
                "id": "AAMkADBlOWNlOWExLTdjMzktNDI5NC04MDY3LTRiZGM2NTIxMzUyNABGAAAAAAC11nCh2QXMSJ7F766v_WCUBwBSt5DMAwrBRJGMMbg9jqoYAAAGNiHGAABSt5DMAwrBRJGMMbg9jqoYAAAJXgInAAA="
                <!--This is the id of an existing email in User's inbox -->
            }
        ]
    },
    "saveToSentItems": "false"
}

解决方案

You cannot provide just the id of the message. If you do that, you'll get a blank attachment. Unfortunately the API is not set up to retrieve the item by ID and insert it for you.

So what that means is you need to retrieve the message yourself and include that entire JSON payload in the item property of the attachment. (See https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/message_post_attachments)

There is one key detail that you need to be aware of: before adding the JSON representation of the message to be attached, you need to add an @odata.type property to it, and set it to microsoft.graph.message.

For example:

Get the message to be attached

GET /me/messages/{id}

Response

{
  "@odata.etag": "W/\"CQAAABYAAACImwNLdwWFR4SE3YBnGvEfAAAOjRuW\"",
  "id": "AAMkADRkOWJjMjdlLTM3OWMtNDU5ZS05YWZlLTRjMjkwZWE5NWMyYgBGAAAAAACUZXoWyqoWRohmHfOzvx9wBwCImwNLdwWFR4SE3YBnGvEfAAAAAAEMAACImwNLdwWFR4SE3YBnGvEfAAAOi7KdAAA=",
  "createdDateTime": "2017-10-26T12:22:03Z",
  "lastModifiedDateTime": "2017-10-26T12:22:03Z",
  "changeKey": "CQAAABYAAACImwNLdwWFR4SE3YBnGvEfAAAOjRuW",
  "categories": [],
  "receivedDateTime": "2017-10-26T12:22:03Z",
  "sentDateTime": "2017-10-26T12:22:02Z",
  "hasAttachments": false,
  "internetMessageId": "<BLUPR13MB0274245999AAD00105B87333A6450@BLUPR13MB0274.namprd13.prod.outlook.com>",
  "subject": "Test attachment stuff",
  "bodyPreview": "Hello world!",
  "importance": "normal",
  "parentFolderId": "AQMkADRkOWJjMjdlLTM3OWMtNDU5ZS05YWZlLTRjMjkwZWE5NWMyYgAuAAADlGV6FsqqFkaIZh3zs78fcAEAiJsDS3cFhUeEhADdgGca8R8AAAIBDAAAAA==",
  "conversationId": "AAQkADRkOWJjMjdlLTM3OWMtNDU5ZS05YWZlLTRjMjkwZWE5NWMyYgAQAOcAXOsMKqFDlKHlkwvtP0Y=",
  "isDeliveryReceiptRequested": false,
  "isReadReceiptRequested": false,
  "isRead": false,
  "isDraft": false,
  "webLink": "https://outlook.office365.com/owa/?ItemID=AAMkADRkOWJjMjdlLTM3OWMtNDU5ZS05YWZlLTRjMjkwZWE5NWMyYgBGAAAAAACUZXoWyqoWRohmHfOzvx9wBwCImwNLdwWFR4SE3YBnGvEfAAAAAAEMAACImwNLdwWFR4SE3YBnGvEfAAAOi7KdAAA%3D&exvsurl=1&viewmodel=ReadMessageItem",
  "inferenceClassification": "focused",
  "body": {
    "contentType": "html",
    "content": "<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n<meta content=\"text/html; charset=iso-8859-1\">\r\n<style type=\"text/css\" style=\"display:none\">\r\n<!--\r\np\r\n\t{margin-top:0;\r\n\tmargin-bottom:0}\r\n-->\r\n</style>\r\n</head>\r\n<body dir=\"ltr\">\r\n<div id=\"divtagdefaultwrapper\" dir=\"ltr\" style=\"font-size:12pt; color:#000000; font-family:Calibri,Helvetica,sans-serif\">\r\n<p>Hello world!</p>\r\n</div>\r\n</body>\r\n</html>\r\n"
  },
  "sender": {
    "emailAddress": {
      "name": "MOD Administrator",
      "address": "admin@contoso.onmicrosoft.com"
    }
  },
  "from": {
    "emailAddress": {
      "name": "MOD Administrator",
      "address": "admin@contoso.onmicrosoft.com"
    }
  },
  "toRecipients": [
    {
      "emailAddress": {
        "name": "Adele Vance",
        "address": "AdeleV@contoso.onmicrosoft.com"
      }
    }
  ],
  "ccRecipients": [],
  "bccRecipients": [],
  "replyTo": []
}

Send a new mail with item attachment

Notice the addition of "@odata.type": "microsoft.graph.message", to the JSON representation of the attached message in the item property.

POST /me/sendmail
Content-Type: application/json

{
  "message": {
    "subject": "Meet for lunch?",
    "body": {
      "contentType": "Text",
      "content": "The new cafeteria is open."
    },
    "toRecipients": [{
      "emailAddress": {
        "address": "adelev@contoso.onmicrosoft.com"
      }
    }],
    "attachments": [
      {
        "@odata.type": "#microsoft.graph.itemAttachment",
        "name": "Test attachment stuff",
        "item": {
          "@odata.type": "microsoft.graph.message",
          "@odata.etag": "W/\"CQAAABYAAACImwNLdwWFR4SE3YBnGvEfAAAOjRuW\"",
          "id": "AAMkADRkOWJjMjdlLTM3OWMtNDU5ZS05YWZlLTRjMjkwZWE5NWMyYgBGAAAAAACUZXoWyqoWRohmHfOzvx9wBwCImwNLdwWFR4SE3YBnGvEfAAAAAAEMAACImwNLdwWFR4SE3YBnGvEfAAAOi7KdAAA=",
          "createdDateTime": "2017-10-26T12:22:03Z",
          "lastModifiedDateTime": "2017-10-26T12:22:03Z",
          "changeKey": "CQAAABYAAACImwNLdwWFR4SE3YBnGvEfAAAOjRuW",
          "categories": [],
          "receivedDateTime": "2017-10-26T12:22:03Z",
          "sentDateTime": "2017-10-26T12:22:02Z",
          "hasAttachments": false,
          "internetMessageId": "<BLUPR13MB0274245999AAD00105B87333A6450@BLUPR13MB0274.namprd13.prod.outlook.com>",
          "subject": "Test attachment stuff",
          "bodyPreview": "Hello world!",
          "importance": "normal",
          "parentFolderId": "AQMkADRkOWJjMjdlLTM3OWMtNDU5ZS05YWZlLTRjMjkwZWE5NWMyYgAuAAADlGV6FsqqFkaIZh3zs78fcAEAiJsDS3cFhUeEhADdgGca8R8AAAIBDAAAAA==",
          "conversationId": "AAQkADRkOWJjMjdlLTM3OWMtNDU5ZS05YWZlLTRjMjkwZWE5NWMyYgAQAOcAXOsMKqFDlKHlkwvtP0Y=",
          "isDeliveryReceiptRequested": false,
          "isReadReceiptRequested": false,
          "isRead": false,
          "isDraft": false,
          "webLink": "https://outlook.office365.com/owa/?ItemID=AAMkADRkOWJjMjdlLTM3OWMtNDU5ZS05YWZlLTRjMjkwZWE5NWMyYgBGAAAAAACUZXoWyqoWRohmHfOzvx9wBwCImwNLdwWFR4SE3YBnGvEfAAAAAAEMAACImwNLdwWFR4SE3YBnGvEfAAAOi7KdAAA%3D&exvsurl=1&viewmodel=ReadMessageItem",
          "inferenceClassification": "focused",
          "body": {
            "contentType": "html",
            "content": "<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n<meta content=\"text/html; charset=iso-8859-1\">\r\n<style type=\"text/css\" style=\"display:none\">\r\n<!--\r\np\r\n\t{margin-top:0;\r\n\tmargin-bottom:0}\r\n-->\r\n</style>\r\n</head>\r\n<body dir=\"ltr\">\r\n<div id=\"divtagdefaultwrapper\" dir=\"ltr\" style=\"font-size:12pt; color:#000000; font-family:Calibri,Helvetica,sans-serif\">\r\n<p>Hello world!</p>\r\n</div>\r\n</body>\r\n</html>\r\n"
          },
          "sender": {
            "emailAddress": {
              "name": "MOD Administrator",
              "address": "admin@contoso.onmicrosoft.com"
            }
          },
          "from": {
            "emailAddress": {
              "name": "MOD Administrator",
              "address": "admin@contoso.onmicrosoft.com"
            }
          },
          "toRecipients": [
            {
              "emailAddress": {
                "name": "Adele Vance",
                "address": "AdeleV@contoso.onmicrosoft.com"
              }
            }
          ],
          "ccRecipients": [],
          "bccRecipients": [],
          "replyTo": []
        }
      }
    ]
  },
  "saveToSentItems": "false"
}

这篇关于发送包含与用户收件箱中的其他邮件不同的邮件作为附件的新邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆