Can not get Item Attachment using Office 365 REST API

1.3k Views Asked by At

I'm using Office 365 REST API to get mail's attachments. I can get File Attachments normaly, but if the attachment has type : "#Microsoft.OutlookServices.ItemAttachment". I get this ItemAttachment's ID, then use this request to get File Attachment:
https://outlook.office.com/api/v1.0/me/messages/MESSAGE_ID/attachments/FILE_ITEM_ID?$expand=Microsoft.OutlookServices.ItemAttachment/Item

Response return has new attribute "Item", contains a "#Microsoft.OutlookServices.Message" with HasAttachments = false. But try using the Web Outlook version, I can get this Item Attachment (In this case is a .eml file, exec it will open a new mail form). Here is a example :

{"@odata.context"=>
  "https://outlook.office.com/api/v1.0/$metadata#Me/Messages('AAMkAGRiMjE2MTk3LTg3ZWUtNDg5ZC04YTMwLWYzNmU1MGY0Y2VlNgBGAAAAAADkAZdAt2CKRb7qeDvUo2cCBwAM_24JjVmvQpKtlDshSR9tAAAAAAEMAAAM_24JjVmvQpKtlDshSR9tAADa_spkAAA%3D')/Attachments/$entity",
 "@odata.type"=>"#Microsoft.OutlookServices.ItemAttachment",
 "@odata.id"=>
  "https://outlook.office.com/api/v1.0/Users('[email protected]')/Messages('AAMkAGRiMjE2MTk3LTg3ZWUtNDg5ZC04YTMwLWYzNmU1MGY0Y2VlNgBGAAAAAADkAZdAt2CKRb7qeDvUo2cCBwAM_24JjVmvQpKtlDshSR9tAAAAAAEMAAAM_24JjVmvQpKtlDshSR9tAADa_spkAAA=')/Attachments('AAMkAGRiMjE2MTk3LTg3ZWUtNDg5ZC04YTMwLWYzNmU1MGY0Y2VlNgBGAAAAAADkAZdAt2CKRb7qeDvUo2cCBwAM_24JjVmvQpKtlDshSR9tAAAAAAEMAAAM_24JjVmvQpKtlDshSR9tAADa_spkAAABEgAQAC0OcoIdL8lNgC520X2uWrg=')",
 "Id"=>"AAMkAGRiMjE2MTk3LTg3ZWUtNDg5ZC04YTMwLWYzNmU1MGY0Y2VlNgBGAAAAAADkAZdAt2CKRb7qeDvUo2cCBwAM_24JjVmvQpKtlDshSR9tAAAAAAEMAAAM_24JjVmvQpKtlDshSR9tAADa_spkAAABEgAQAC0OcoIdL8lNgC520X2uWrg=",
 "DateTimeLastModified"=>"2016-02-20T08:49:48Z",
 "Name"=>"[nhung]  mail  dùng chung cho company",
 "ContentType"=>"message/rfc822",
 "Size"=>18619,
 "IsInline"=>false,
 "[email protected]"=>
  "https://outlook.office.com/api/v1.0/$metadata#Me/Messages('AAMkAGRiMjE2MTk3LTg3ZWUtNDg5ZC04YTMwLWYzNmU1MGY0Y2VlNgBGAAAAAADkAZdAt2CKRb7qeDvUo2cCBwAM_24JjVmvQpKtlDshSR9tAAAAAAEMAAAM_24JjVmvQpKtlDshSR9tAADa_spkAAA%3D')/Attachments('AAMkAGRiMjE2MTk3LTg3ZWUtNDg5ZC04YTMwLWYzNmU1MGY0Y2VlNgBGAAAAAADkAZdAt2CKRb7qeDvUo2cCBwAM_24JjVmvQpKtlDshSR9tAAAAAAEMAAAM_24JjVmvQpKtlDshSR9tAADa_spkAAABEgAQAC0OcoIdL8lNgC520X2uWrg%3D')/Microsoft.OutlookServices.ItemAttachment/Item/$entity",
 "Item"=>
  {"@odata.type"=>"#Microsoft.OutlookServices.Message",
   "Id"=>nil,
   "DateTimeCreated"=>"2016-02-20T08:49:48Z",
   "DateTimeLastModified"=>"2016-02-20T08:49:48Z",
   "DateTimeReceived"=>"2016-02-20T08:49:42Z",
   "DateTimeSent"=>"2016-02-20T08:49:39Z",
   "HasAttachments"=>false,
   "Subject"=>"[nhung]  mail  dùng chung cho company",
   "Body"=>
    {"ContentType"=>"HTML",
     "Content"=>
      "<html><head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>\r\n<p>--&nbsp;cand_rep</p>\r\n<p>相川 真里和 --comp_name</p>\r\n<p>---&nbsp;comp_established</p>\r\n<p>---&nbsp;comp_address</p>\r\n<p>&nbsp;--&nbsp;comp_business</p>\r\n<p>2110036 --&nbsp;comp_employee</p>\r\n</body>\r\n</html>\r\n"},
   "Sender"=>{"EmailAddress"=>{"Name"=>"framgia 個別アカウント01", "Address"=>"[email protected]"}},
   "From"=>{"EmailAddress"=>{"Name"=>"framgia 個別アカウント01", "Address"=>"[email protected]"}},
   "ToRecipients"=>[{"EmailAddress"=>{"Name"=>"framgia 個別アカウント01", "Address"=>"[email protected]"}}],
   "ConversationId"=>"AAQkAGRiMjE2MTk3LTg3ZWUtNDg5ZC04YTMwLWYzNmU1MGY0Y2VlNgAQAAjK6iM-EvcpiBrx4E7Ogl4=",
   "IsReadReceiptRequested"=>false,
   "IsRead"=>true,
   "IsDraft"=>false,
   "WebLink"=>"https://outlook.office365.com/owa/?ItemID=AAMkAGRiMjE2MTk3LTg3ZWUtNDg5ZC04YTMwLWYzNmU1MGY0Y2VlNgAAAA%3D%3D&exvsurl=1&viewmodel=ReadMessageItem"}}

So, how to get this .eml file by using Office 365 REST API? Could someone can help me ? Thank you !

1

There are 1 best solutions below

3
On

The Office 365 apis will not serve your item attachment as an *.eml file. If you need the ItemAttachment as an *.eml file you will have to build it yourself. Here are the steps: you do have access to all the information to this mail (see the new Item entry) so you can create MailMessage instance then, call one the technique proposed here to save a MailMessage to an *.eml file How to save MailMessage object to disk as *.eml or *.msg file

Note: you did not mentioned the technical stack you use to request the Office365 apis, I assumed it is C#.