web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / How to retrieve an att...
Power Automate
Unanswered

How to retrieve an attachment from an incoming email to Dynamics 365

(0) ShareShare
ReportReport
Posted on by 7

Hi,

 

I'm trying to figure out how to retrieve an attachment from an e-mail into Dynamics 365.

 

I setup a simple flow that listens for an email arriving in Dynamics 365, then forwards the email to support.

 

I am using the Common Data Service, 'When a record is created' event to listen to an e-mail arrival.  The Entity set is Email Message.

 

The next step is to email to support.  However, the attachment is not sent.  Troubleshooting the issue, I notice that attachmentcount is 0 when there is an attachment.  How do you get the attachment to the email?  Here's the output:

 

{
"notifications": 0,
"_notifications_label": "None",
"isunsafe": 0,
"correlationmethod": 0,
"_correlationmethod_label": "None",
"_owningbusinessunit_value": "6d92828b-e7ec-e111-930b-78e7d1622fc2",
"_owningbusinessunit_type": "businessunits",
"_sendersaccount_value": "56e16ff1-063b-e211-b5f5-78e7d162ee7d",
"_sendersaccount_type": "accounts",
"description": "<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n</head>\n<body>\n<div dir=\"ltr\">Test attachment</div>\n</body>\n</html>\n",
"isemailfollowed": false,
"createdon": "2021-04-16T14:54:28Z",
"attachmentcount": 0,
"followemailuserpreference": false,
"statecode": 1,
"_statecode_label": "Completed",
"isbilled": false,
"subject": "Attachment Test",
"deliveryreceiptrequested": false,
"directioncode": false,
"_ownerid_value": "8862e19e-b7b9-e211-af97-78e7d162dec2",
"_ownerid_type": "systemusers",
"modifiedon": "2021-04-16T14:54:28Z",
"readreceiptrequested": false,
"prioritycode": 1,
"_prioritycode_label": "Normal",
"compressed": false,
"timezoneruleversionnumber": 0,
"isregularactivity": true,
"_modifiedby_value": "f6fb6c97-71af-432f-8452-c8e375547010",
"_modifiedby_type": "systemusers",
"isemailreminderset": false,
"statuscode": 4,
"_statuscode_label": "Received",
"_modifiedonbehalfby_value": "f6fb6c97-71af-432f-8452-c8e375547010",
"_modifiedonbehalfby_type": "systemusers",
"_emailsender_value": "0cb98524-073b-e211-b5f5-78e7d162ee7d",
"_emailsender_type": "contacts",
"activitytypecode": "email",
"messageid": "<CAO=2aWauvqJoviFPkeRTg8yMVda-mfMcoHf3FH_EJUzymtyJ0g@mail.gmail.com>",
"submittedby": "\"Support\" support@notrealemail.com",
"sender": "support@notrealemail.com",
"isworkflowcreated": false,
"deliveryprioritycode": 1,
"_deliveryprioritycode_label": "Normal",
"_createdonbehalfby_value": "f6fb6c97-71af-432f-8452-c8e375547010",
"_createdonbehalfby_type": "systemusers",
"_createdby_value": "f6fb6c97-71af-432f-8452-c8e375547010",
"_createdby_type": "systemusers",
"actualend": "2021-04-16",
"emailreminderstatus": 0,
"_emailreminderstatus_label": "NotSet",
"emailremindertype": 0,
"_emailremindertype_label": "If I do not receive a reply by",
"torecipients": "support@notrealemail.com;",
"posp_processed": false,
"baseconversationindexhash": -86706446,
"activityid": "3d21fea1-c39e-eb11-b1ac-000d3a8f7bda",
"conversationindex": "0101D732CFD0C81AE297FD0B1F409AFB58789C5E3304",
"ItemInternalId": "3d21fea1-c39e-eb11-b1ac-000d3a8f7bda",
"SdkMessage": "Create",
"RunAsSystemUserId": "f6fb6c97-71af-432f-8452-c8e375547010",
"RowVersion": "45890156"
}

 

Thanks in advance.

 

 

Categories:
I have the same question (0)
  • Verified answer
    Mira Ghaly Profile Picture
    11,413 Moderator on at

    @Denny888 

    To get email attachments please use the List Rows action on table attachments as below

    Mira_Ghaly_0-1618745592185.png

    Please note that there is 2 attachments entities when you select List Rows , Make sure you are using ActivityMimeAttachments:

    Mira_Ghaly_1-1618745684297.png

    Then you need to loop on the attachments and add it to an array ,so after list rows Initialize a variable called attachments of Type array

    Mira_Ghaly_2-1618747956673.png

    Then add an apply to each Loop and a compose data operation as below:

    Mira_Ghaly_3-1618748027049.png

     

     

    The expression for "ContentBytes"

    base64ToBinary(items('Apply_to_each_2')?['body'])

    Then Send an email as below:

    Mira_Ghaly_4-1618748163296.png

     

  • Denny888 Profile Picture
    7 on at

    Hi Mira,

     

    Thank you for the detail response.  I am very grateful for your help.  But I have been struggling for the last couple of days to find the answer.  I am still having trouble trying to get the actual attachment from the activitymimeattachment entity.

     

    So here are the steps I followed:

    I query for the attachment (activitymimeattachment)

     

    attachmentList.png

     

    Go through and compose the data to name and ContentType and then create the same file in sharepoint:

    sendattachmenttosharepoint.png

     

    The problem is that body in base64ToBinary call is not binary and is a string result returning.   I've captured the resulting output (I'm adding the input as well just in case):

     

    Raw Input

    {
    "host": {
    "connectionReferenceName": "shared_commondataservice",
    "operationId": "GetItems_V2"
    },
    "parameters": {
    "dataset": "fakecompany.crm",
    "table": "activitymimeattachments",
    "$filter": "_objectid_value eq 1e0a5e84-f99d-eb11-b1ac-111d111f745d"
    }
    }

     

    Output Raw:

     

    {
    "statusCode": 200,
    "headers": {
    "Pragma": "no-cache",
    "Transfer-Encoding": "chunked",
    "Vary": "Accept-Encoding",
    "x-ms-request-id": "fc4a4319-b389-411e-a01b-6e45fd3f9f7e",
    "Strict-Transport-Security": "max-age=31536000; includeSubDomains",
    "X-Content-Type-Options": "nosniff",
    "X-Frame-Options": "DENY",
    "Cache-Control": "no-store, no-cache",
    "Set-Cookie": "ARRAffinity=9a53c9d953bbdfc85fcb9bc388522492211961687fcde0ee871a9a717de0c346;Path=/;HttpOnly;Secure;Domain=commondataservice-cus.azconn-cus.p.azurewebsites.net,ARRAffinitySameSite=9a53c9d953bbdfc85fcb9bc388522492211961687fcde0ee871a9a717de0c346;Path=/;HttpOnly;SameSite=None;Secure;Domain=commondataservice-cus.azconn-cus.p.azurewebsites.net",
    "Timing-Allow-Origin": "*",
    "x-ms-apihub-cached-response": "true",
    "Date": "Tue, 20 Apr 2021 17:19:52 GMT",
    "Content-Type": "application/json; charset=utf-8; odata.metadata=minimal",
    "Expires": "-1",
    "Content-Length": "1090"
    },
    "body": {
    "@odata.context": "https://flow-apim-msmanaged-na-eastus2-01.azure-apim.net/apim/commondataservice/79b2b97d12dd4e2db37df9436d3c9f79/$metadata#datasets('fakecompany.crm')/tables('activitymimeattachments')/items",
    "value": [
    {
    "@odata.id": "https://fakecompany.crm.dynamics.com/api/data/v9.0/activitymimeattachments(ddfb9efa-28c2-4328-b8a6-0d5312326b50)",
    "@odata.etag": "",
    "ItemInternalId": "ddfb9efa-12c2-4328-b8a6-0d5312326b50",
    "overwritetime": "1900-01-01T00:00:00Z",
    "attachmentnumber": 1,
    "attachmentcontentid": "f_knizr6o30",
    "solutionid": "25a01723-9f63-4449-a3e0-046cc23a2902",
    "_objectid_value": "1e0a5e84-f99d-eb33-b1ac-111d111f745d",
    "_objectid_type": "emails",
    "activitymimeattachmentidunique": "6ba338ed-9dde-48ef-890c-1f88e1c15b72",
    "isfollowed": false,
    "activitymimeattachmentid": "ddfb9efa-28c2-4328-b8a6-0d5312389b50",
    "_attachmentid_value": "3f0a5e84-f99d-eb11-b1ac-000d3a8f745d",
    "_attachmentid_type": "attachments",
    "ismanaged": false,
    "objecttypecode": "email",
    "componentstate": 0,
    "_componentstate_label": "Published",
    "versionnumber": 45881876
    }
    ]
    }
    }

     

    So the raw output doesn't seem to have an associated base64 string that represents the attachment.  Am I missing something?

  • Denny888 Profile Picture
    7 on at

    I realized what I was missing.  After ActivityMimeAttachment, I had to list the other Attachments and retrieve the body of that to get the actual attachment.  Again thanks Mira for your guidance!

  • OlaAde Profile Picture
    19 on at

    Hi Mira,

     

    I know this is a very old post, but this is so relating to a flow I just did which works perfectly fine. However, I am having a slight issue; every attachment received in the email looks OK, but unable to preview them, and also, when downloaded, it won't open.

     

    I get the error it appear we don't support this file format 

  • OlaAde Profile Picture
    19 on at

    Never mind - I have managed to resolve the issue. 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 503 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 321 Moderator

#3
abm abm Profile Picture

abm abm 237 Most Valuable Professional

Last 30 days Overall leaderboard