how to add OneDrive Files to Mail Attatchment

Denny 1 Reputation point
2021-12-30T13:58:56.34+00:00

Hello,

i have a Question about the Graph API in combination with the C# SDK. I want to add some oneDrives files to an new mailItem. My Soloution is about 3 steps:

  1. I create a new mailItem and upload it via GraphAPI:

var Mail = new Message
{
Subject = "Test",
Importance = Importance.Normal,
Body = new ItemBody
{
ContentType = BodyType.Html,
Content = "Test"
},
ToRecipients = Recipients
};

            newMail = await graphClient.Users[$"{data.AzureUserID}"].Messages.Request().AddAsync(Mail);
  1. I want to upload new referenceAttatchments via Graph API, but how can I assign an ondriveItem to the reference object?
    On the internet I found the following solution:

{
"@odata.type": "#microsoft.graph.referenceAttachment",
"name": "AttachmentName",
"sourceUrl": "https://1drv.ms/u/s!ASDLKASDLASHDLASKDLJAXCXZ_DASD",
"providerType": "oneDriveConsumer",
"isFolder": false
}

But in C# SDK there isn't a sourceUrl proberty ?! How can i do this?

  1. send the mailItem : There isn't a problem ;)
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,582 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JanardhanaVedham-MSFT 3,536 Reputation points
    2021-12-30T23:19:07.547+00:00

    Hi @Denny ,

    For adding OneDrive file link as an attachement to a mail, SourceUrl and ProviderType properties must to be set as part the request body. According to this documentation, currently these properties are only available in Microsoft Graph Beta API but not in V1.0 API. In order to call the beta API, you would have to install & use the Microsoft.Graph.Beta package. You can refer this link on Microsoft Graph Beta .NET Client Library (SDK) installation and other details.

    Below Microsoft Graph Beta API endpoint is used to attach OneDrive file link to a mail by setting SourceUrl and ProviderType properties in request body :

    POST https://graph.microsoft.com/beta/users/{user-id}/messages/{message-id}/attachments  
    

    Important Note :

    APIs under the /beta version in Microsoft Graph are subject to change. Use of these APIs or relevant Beta SDK client libaries in production applications is not supported.

    Here is the sample code snippet for attaching OneDrive file link to reference object using Beta API or Microsoft.Graph.Beta package & C#.NET SDK :

    GraphServiceClient graphClient = new GraphServiceClient( authProvider );  
      
    var attachment = new ReferenceAttachment  
    {  
    	Name = "OneDriveAttachment",  
    	SourceUrl = "https://o365-my.sharepoint.com/personal/jxxxx_o365_onmicrosoft_com/Documents/Test.docx",  
    	ProviderType = ReferenceAttachmentProvider.OneDriveConsumer,  
    	IsFolder = false  
    };  
      
    await graphClient.Users["abcde-90f8-4088-ad78-38d45cbf3f1a"].Messages["AAMkADMyMDI2MjY5LTc1MzgtNDhiNi1iM2E0LTk0MmUzNzhhMmQyMgBGAAAAAAB8OAEgBrpTSrTOnU1piS01BwBmL_k7qYXPRZQMwo9Nyp1dAAAAAAEPAABmL_k7qYXPRZQMwo9Nyp1dAAGyabceAAA="].Attachments  
    	.Request()  
    	.AddAsync(attachment);  
    

    I did also verified the above 3 step solution approach using Graph API and it worked with Beta endpoint to attach OneDrive file link attachment with a message / mail. Here are my testing screenshots :

    POST  https://graph.microsoft.com/v1.0/users/{user-id}/messages  
    

    161522-onedrive-file-attachment-step1-output.jpg

      POST https://graph.microsoft.com/beta/users/{user-id}/messages/{message-id}/attachments  
    

    161446-onedrive-file-attachment-step-2-output.jpg

     POST https://graph.microsoft.com/v1.0/users/{user-id}/messages/{message-id}/send  
    

    161466-onedrive-file-attachment-step-3-output.jpg

    Final Output of Sent Email (with OneDrive file link attachement) :

    161467-onedrive-file-attachment-final-output.jpg

    You can refer below addtional documentation link for more information and quick comparision :

    Reference attachment type (V1.0 API) : https://learn.microsoft.com/en-us/graph/api/resources/referenceattachment?view=graph-rest-1.0
    Reference attachment type (Beta API) : https://learn.microsoft.com/en-us/graph/api/resources/referenceattachment?view=graph-rest-beta
    Microsoft Graph Beta .NET Client Library (SDK) : https://github.com/microsoftgraph/msgraph-beta-sdk-dotnet

    Hope this helps.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have further questions about this answer, please click "Comment".

    1 person found this answer helpful.
    0 comments No comments