Msgraph - 从 msgraph api 下载 .eml 作为 itemAttachment

Msgraph - download .eml as itemAttachment from msgraph api

我得到了一个名为 createObjectFromNotification 的方法,它根据通知创建电子邮件 resource.url样本https://graph.microsoft.com/v1.0/me/messages/AQMkAGFkYmM2YzJiLTM3OTItNDE0ZS1iMmIw

我们在同一个 api 调用中获取邮件附件,然后过滤文件附件、项目附件 我从 contentBytes 创建文件附件,但我额外调用了获取项目附件

GET /users/{id}/messages/{id}/attachments/{id}/$value

参考 https://docs.microsoft.com/en-us/graph/outlook-get-mime-message 但是我的文件被截断了我将在下面显示示例 和我的代码

private async createObjectFromNotification(resource: EmailResource) {
      const email = await this.repository.getEmail(resource.url);
   .... etc
  }

public async getEmail(url: string): Promise<IncomingEmailInfo> {
    const result: MSGraph.Message = await this.getResource(url, {
      select: [
        "uniqueBody",
        "body",
        "sender",
        "toRecipients",
        "ccRecipients",
        "bccRecipients",
        "createdDateTime",
        "subject",
        "conversationId",
        "hasAttachments",
      ],
      expand: ["attachments"],
    });

    var attachments =
      result?.attachments
        .filter((attachment) => MSGraph.isFileAttachment(attachment))
        .map(
          (attachment): EmailAttachment => {
            return {
              name: attachment.name,
              cid: attachment.contentId,
              content: (attachment as MSGraph.FileAttachment).contentBytes,
              contentType: attachment.contentType,
              isInline: attachment.isInline,
            };
          }
        ) ?? [];

    await Promise.all(
      result?.attachments
        .filter(
          (attachment) =>
            attachment["@odata.type"] === "#microsoft.graph.itemAttachment"
        )
        .map(async (attachment) => {
          const res = await this.getResource(
            `${url}/attachments/${attachment.id}/$value`,
            {}
          );
          console.log("result ", res);
          attachments.push({
            name: attachment.name,
            cid: attachment.id,
            content: res,
            contentType: attachment.contentType,
            isInline: attachment.isInline,
          });
        })
    );

    return {
      id: result.id,
      from: result.sender.emailAddress.address,
      to: result.toRecipients.map((to) => to.emailAddress.address),
      cc: result.ccRecipients.map((cc) => cc.emailAddress.address),
      bcc: result.bccRecipients.map((bcc) => bcc.emailAddress.address),
      subject: result.subject,
      body: result.body.content,
      uniqueBody: result.uniqueBody.content,
      bodyType: result.body.contentType,
      conversationId: result.conversationId,
      attachments,
    };
  }

public async getResource(
    resource: string,
    params: {
      select?: string[];
      expand?: string[];
      filter?: string[];
      top?: number;
    }
  ) {
    const selectString = (params.select ?? []).join(",");
    const expandString = (params.expand ?? []).join(",");
    const filterString = (params.filter ?? []).join(",");
    const top = params.top ?? 10;

    const result = await this.axios.get(resource, {
      params: {
        select: selectString,
        expand: expandString,
        filter: filterString,
        top: top,
      },
      headers: {
        ...(await this.getAuthHeaders()),
        Prefer: "outlook.body-content-type='html'",
      },
    });
    return result.data;
  }

Mime 文件 .eml

Received: from MN2PR12MB4453.namprd12.prod.outlook.com (2603:10b6:208:260::9)
 by DM5PR12MB2486.namprd12.prod.outlook.com with HTTPS; Wed, 7 Oct 2020
 14:25:58 +0000
Received: from DM6PR12CA0032.namprd12.prod.outlook.com (2603:10b6:5:1c0::45)
 by MN2PR12MB4453.namprd12.prod.outlook.com (2603:10b6:208:260::9) with
 Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3455.21; Wed, 7 Oct
 2020 14:25:56 +0000
Received: from DM6NAM11FT046.eop-nam11.prod.protection.outlook.com
 (2603:10b6:5:1c0:cafe::b1) by DM6PR12CA0032.outlook.office365.com
 (2603:10b6:5:1c0::45) with Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3433.37 via Frontend
 Transport; Wed, 7 Oct 2020 14:25:56 +0000
Authentication-Results: spf=pass (sender IP is 166.78.70.105)
 smtp.mailfrom=circleci.com; berwickinsurance.com; dkim=pass (signature was
 verified) header.d=circleci.com;berwickinsurance.com; dmarc=bestguesspass
 action=none header.from=circleci.com;compauth=pass reason=109
Received-SPF: Pass (protection.outlook.com: domain of circleci.com designates
 166.78.70.105 as permitted sender) receiver=protection.outlook.com;
 client-ip=166.78.70.105; helo=m70-105.mailgun.net;
Received: from m70-105.mailgun.net (166.78.70.105) by
 DM6NAM11FT046.mail.protection.outlook.com (10.13.172.121) with Microsoft SMTP
 Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id
 15.20.3455.23 via Frontend Transport; Wed, 7 Oct 2020 14:25:56 +0000
DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=circleci.com; q=dns/txt;
 s=mailo; t=1602080756; h=List-Id: Content-Type: Mime-Version: Subject:
 Message-Id: To: From: Date: Sender;
 bh=qr9OzMq9dB7UxtaNoMl9iKG++HMSXYGSWMqbAR0B2Ko=; b=cGFIguoJnU3pxWWY99Ts6CYx5yfpd2BB+heNB7EdwurGUQ3T1sQ09ISHrnpmpEtzohF4X33X
 lwpOgykgclZWdalzxRx/i3Y3kim8eyiRnzXkUsNvkSgnE5NsKOyrENgZ0GZKfmHYla8VDFxr
 h3FXZ1SHhDARw1xY84s5Xz3TqbI=
X-Mailgun-Sending-Ip: 166.78.70.105
X-Mailgun-Sid: WyJkZjVlMCIsICJtaW5hLmZhd3p5QGJlcndpY2tpbnN1cmFuY2UuY29tIiwgIjg2OTgiXQ==
Received: from circle-legacy-notifier-v1-d744c6cc9-wxpzz
 (ec2-34-197-216-176.compute-1.amazonaws.com [34.197.216.176]) by
 smtp-out-n01.prod.us-west-2.postgun.com with SMTP id
 5f7dcff357b88ccb562522f3 (version=TLS1.2,
 cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Wed, 07 Oct 2020 14:25:55
 GMT
Sender: builds@circleci.com
Date: Wed, 7 Oct 2020 14:25:54 +0000 (UTC)
From: CircleCI Builds <builds@circleci.com>
To: Bill Smith <billysmithsolutions@gmail.com>, Mina
 <mina.fawzy@berwickinsurance.com>, James Taylor <james77taylor@gmail.com>,
 elilondon <eli.london@yahoo.com>, Keith Gonyon <kgonyon@gmail.com>
Message-Id: <pruJDCxRI6yvrHRe4rhTQ.1602080754888@postal.circle-legacy-notifier-v1-d744c6cc9-wxpzz>
Subject: [Workflow] Failed: berwickinsurance/BerwickAgencyDashboards on
 master / build-deploy-staging (89847f4)
Content-Type: multipart/alternative; boundary="----=_Part_766_1718740841.1602080754887"
User-Agent: postal/2.0.3
List-Id: <berwickinsurance.BerwickAgencyDashboards.notifications.circleci.com>
X-Mailgun-Track-Clicks: htmlonly
X-Mailgun-Track-Opens: yes
Return-Path: bounce+3ffd89.8698-mina.fawzy=berwickinsurance.com@circleci.com
X-MS-Exchange-Organization-ExpirationStartTime: 07 Oct 2020 14:25:56.4288
 (UTC)
X-MS-Exchange-Organization-ExpirationStartTimeReason: OriginalSubmit
X-MS-Exchange-Organization-ExpirationInterval: 1:00:00:00.0000000
X-MS-Exchange-Organization-ExpirationIntervalReason: OriginalSubmit
X-MS-Exchange-Organization-Network-Message-Id:
 a3204c6e-0b22-4756-e379-08d86acce797
X-EOPAttributedMessage: 0
X-EOPTenantAttributedMessage: e65bc842-237f-4291-9d3f-746dd87042eb:0
X-MS-Exchange-Organization-MessageDirectionality: Incoming
X-MS-PublicTrafficType: Email
X-MS-Exchange-Organization-AuthSource:
 DM6NAM11FT046.eop-nam11.prod.protection.outlook.com
X-MS-Exchange-Organization-AuthAs: Anonymous
X-MS-Office365-Filtering-Correlation-Id: a3204c6e-0b22-4756-e379-08d86acce797
X-MS-TrafficTypeDiagnostic: MN2PR12MB4453:
X-MS-Oob-TLC-OOBClassifiers: OLM:114;
X-MS-Exchange-Organization-SCL: 1
X-Microsoft-Antispam: BCL:1;
X-Forefront-Antispam-Report:
 CIP:166.78.70.105;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:m70-105.mailgun.net;PTR:m70-105.mailgun.net;CAT:NONE;SFS:(4636009)(286005)(2160300002)(966005)(7846003)(1096003)(9686003)(26005)(110136005)(166002)(83380400001)(956004)(356005)(19810500001)(336012)(33964004)(8676002)(7636003)(83080400001)(7596003)(7066003)(131040200001);DIR:INB;
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Oct 2020 14:25:56.0321
 (UTC)
X-MS-Exchange-CrossTenant-Network-Message-Id: a3204c6e-0b22-4756-e379-08d86acce797
X-MS-Exchange-CrossTenant-Id: e65bc842-237f-4291-9d3f-746dd87042eb
X-MS-Exchange-CrossTenant-AuthSource:
 DM6NAM11FT046.eop-nam11.prod.protection.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Anonymous
X-MS-Exchange-CrossTenant-FromEntityHeader: Internet
X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4453
X-MS-Exchange-Transport-EndToEndLatency: 00:00:02.3920579
X-MS-Exchange-Processed-By-BccFoldering: 15.20.3433.045
X-Microsoft-Antispam-Mailbox-Delivery:
 ucf:0;jmr:0;auth:0;dest:I;ENG:(20160514016)(750128)(520011016)(944506458)(944626604);
X-Microsoft-Antispam-Message-Info:
 =?utf-8?B?cEsyYmkwWm1WS3NLK2ViWHVpVjdWYmlhRUU1WXE4Q0FXWlNlYUpXZlpITHp5?=
 =?utf-8?B?Z2NZSk1KSDFxOUxmaTR2NExRTEtzQnIvS1JQa29FNUxTeHRURkhWcGhURkZW?=
 =?utf-8?B?bTRnNjdMYTNJQ3doSTdUTkFUK0VtaFU2VWRBS0F2dHBhRHBFUHVVVjduckYx?=
 =?utf-8?B?UExXV09yRk13dE5mYzFVZENWcml2TnhIU0llYWIrNWFXOUZ5dytTN0xjbE9p?=
 =?utf-8?B?eGtsaW1uUTRsZ3RLYlJzVGNhMUE0Q2ZFVmNoM0dpMEp3YWFlcUE4WG11QVlr?=
 =?utf-8?B?eCtLNVI5amdQdzZMblhJajRhQnFjMTg3M3dSWUc5TS9mVDZOOXdwbkF5d0FB?=
 =?utf-8?B?TWVXd2VEaE52Z2FYNmNHcEhWWitNOERhLzNLU2MzMkNMeUFtdVk4dW5DbXNO?=
 =?utf-8?B?cjVtMGtpdksyN1lPSWt4Y0tyUWJTU3NDQnh5VHIzYUoxbGV0cU9lK0FOcFZI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nowbDlsa0Faa3ZobHVxbThIK1luVmxOSmVxQW9WN1N4TFpSMHVHZ0hvSmd2?=
 =?utf-8?B?dFRXSDZoMlJRUUtoQzVvS1pwNGhXUHArQk4zOERJVmQ5RlVKaTkrZ1VvdXB6?=
 =?utf-8?B?RTJDTHdXeUtpUWw3STIzaHk3T3BQNFU3OTEvU2I0c1RnN2Y2MHdsalIyTmtz?=
 =?utf-8?B?MVpmMGxET1VwaUt3Q3o0YitKSmY3NWMrQzIwcU04U09acm5DQXR3REhOK2Y4?=
 =?utf-8?Q?1MhExypur/B6Jn/6pv3uQcBZs=3D?=
MIME-Version: 1.0

------=_Part_766_1718740841.1602080754887
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Author: Bill Smith <billy.smith@berwickinsurance.com>
Triggerer: Ghostdog6
Status: Failed
Project: https://circleci.com/gh/berwickinsurance/workflows/BerwickAgencyDashboards
Workflow: https://circleci.com/workflow-run/aba23660-f46e-4ec1-8b7a-e1c2dc9946aa
Branch:
https: //circleci.com/gh/berwickinsurance/workflows/BerwickAgencyDashboards/tree/master
Commits:
build-deploy-1: Failed
https: //circleci.com
https: //twitter.com/circleci


------=_Part_766_1718740841.1602080754887
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
=3D"box-sizing: border-box; font-size:14px; margin:0"><head style=3D"box-siz=
ing: border-box; font-size:14px; margin:0">
margin: 0">@media only screen and (max-width: 640px) {
    body {
        padding: 0 !important
        }
    h1, h2, h3, h4 {
        font-weight: 800 !important;
        margin: 20px 0 5px !important
        }
    h1 {
        font-size: 22px !important
        }
    h2 {
        font-size: 18px !important
        }
    h3 {
        font-size: 16px !important
        }
    .container {
        padding: 0 !important;
        width: 100% !important
        }
    .content {
        padding: 0 !important
        }
    .content-wrap {
        padding: 10px !important
        }
    .invoice {
        width: 100% !important
        }
    }</style></head>
othing: antialiased; -webkit-text-size-adjust:none; color:#898989; font-fami=
ly: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; height:100%; l=
ine-height: 1.6em; background-color:#f6f6f6; width:100% !important" height=
cal-align: top" valign=3D"top"></td>
        <td class=3D"container" width=3D"600" style=3D"box-sizing:border-bo=
ont-size: 20px; margin:0; vertical-align:top; line-height:20px" valign=3D"to=
g: border-box; font-size:80px; margin:0; vertical-align:top; line-height:80p=
ont-size: 30px; margin:0; vertical-align:top; line-height:30px" valign=3D"to=
und-color: #fff; border:1px solid #e9e9e9; border-radius:3px; max-width:560p=
size: 14px; margin:0">
                <td class=3D"alert bad" style=3D"box-sizing:border-box; fon=
t-size: 16px; margin:0; vertical-align:top; border-radius:3px 3px 0 0; color=
font-size: 14px; margin:0; vertical-align:top; padding:20px; width:568px" va=
x-sizing: border-box; font-size:14px; margin:0; color:#337ab7; text-decorati=
on: none">Bill Smith</a>
          </td>
        </tr>
       =20
        <tr class=3D"project" style=3D"box-sizing:border-box; font-size:14p=
e: 14px; margin:0">
          <td class=3D"name" style=3D"box-sizing:border-box; font-size:14px=
t-size: 14px; margin:0; color:#337ab7; text-decoration:none"></a>
          </td>
        </tr>
       =20
      </tbody>
    </table></td>
              </tr>
            </table>
            <table class=3D"secondary" cellpadding=3D"0" cellspacing=3D"0" =
style=3D"box-sizing: border-box; font-size:14px; margin:0; margin-top:20px; =
background-color: #fff; border:1px solid #e9e9e9; border-radius:3px; max-wid=
th: 560px; display:block" bgcolor=3D"#ffffff">
              <tr style=3D"box-sizing:border-box; font-size:14px; margin:0"=
font-size: 14px; margin:0; vertical-align:top; padding:20px; width:568px" va=
izing: border-box; font-size:14px; margin:0; width:100%" width=3D"100%">
      <tbody style=3D"box-sizing:border-box; font-size:14px; margin:0">
        <tr style=3D"box-sizing:border-box; font-size:14px; margin:0">
          <td class=3D"name" style=3D"box-sizing:border-box; font-size:14px=
ng: border-box; font-size:14px; margin:0">
                <tr style=3D"box-sizing:border-box; font-size:14px; margin:=
ng: border-box; font-size:12px; margin:0; vertical-align:top; padding:0 0 20=
=3D"http: //email.circleci.com/c/eJxdUMFugzAM_Rq4gdIQQjhw6IaqbbdVu0-JEyAqSVA=
yle=3D"box-sizing: border-box; font-size:12px; margin:0; color:#999; text-de=
coration: underline">CircleCI</a>.
                    <br style=3D"box-sizing:border-box; font-size:14px; mar=
gin: 0">Unsubscribe by
                      <a href=3D"http://email.circleci.com/c/eJxdkMFugzAMhp=
n: 0; color:#999; text-decoration:underline">changing your email notificatio=
n: 0">
                    <td class=3D"aligncenter content-block" style=3D"box-si=
zing: border-box; font-size:12px; margin:0; vertical-align:top; padding:0 0 =
argin: 0">
                      <a href=3D"http://email.circleci.com/c/eJwtj8tugzAURL=
ox-sizing: border-box; font-size:12px; margin:0; color:#999; text-decoration=
ref=3D"http: //email.circleci.com/c/eJwljkFugzAQRU8DOyOTGHu88KJqFLXZJcoFhokp=
cal-align: top" valign=3D"top"></td>
        </tr>
      </table>
    <img width=3D"1px" height=3D"1px" alt=3D"" src=3D"http://email.circleci=


------=_Part_766_1718740841.1602080754887--

更新 我和邮递员测试了同样的结果

我发现我正在使用 Mac Mailer 应用程序创建 EML,当它上传文件时会截断文件,但在 Outlook 中它工作正常