cancel
Showing results for 
Search instead for 
Did you mean: 

Outlook Graph API - Add attachment

SebT
Level 4
Hi all,

Im trying to modify the Outlook Graph API's to include an action that sends an email with an attachment.
From what i can gather from MS own websites, you need to use the following request :

"attachments":
{
"@odata.type": "#microsoft.graph.fileAttachment",
"name": "[Attachment Name]",
"contentBytes": "[ContentBytes]"
}

So what i've attempted to do is add the above to the sendMail API request, getting the following:

{
"message": {
"subject": "Test",
"body": {
"contentType": "Text",
"content": "Test"
},
"attachments": [
{
"@odata.type": "#microsoft.graph.fileAttachment",
"name": "TEST.xlsx",
"contentBytes": "[ContentBytes]"
}
]
},
"toRecipients": [
{
"emailAddress": {
"address": "[EmailRecipient]"
}
}
]
}

However, I keep getting an error "InvalidRecipient", even though im using my own work email as recipient, which works when using the standard sendMail API.
For easier viewability, i've added an attachment of how it looks in graph explorer.

Anyone with any experience doing this?
Thanks.

Br,
Seb
1 BEST ANSWER

Helpful Answers

ewilson
Staff
Staff
Hi @Seb T,

Here's an example body template for a version of the Send Mail action that incudes the option for an attachment:

{
	"message": {
		"subject": "[Subject]",
		"body": {
			"contentType": "[Email Body Content Type]",
			"content": "[Email Body Content]"
		},
		"toRecipients": [
			{
				"emailAddress": {
					"address": "[Email Address]"
				}
			}
		],
		"ccRecipients": [
			{
				"emailAddress": {
					"address": "[Email Address]"
				}
			}
		],
		"attachments": [
			{
				"@odata.type": "#microsoft.graph.fileAttachment",
				"name": "[Attachment Name]",
				"contentType": "[Attachment Content Type]",
				"contentBytes": "[Attachment Contents]"
			}
		]
	},
	"saveToSentItems": "[Save to Sent Items Folder]"
}​


I believe Microsoft expect to see the recipient information before any attachment sections.

And here's a screenshot of how I set up the input parameters:

34922.png

Cheers,
Eric

View answer in original post

4 REPLIES 4

ewilson
Staff
Staff
Hi @Seb T,

Here's an example body template for a version of the Send Mail action that incudes the option for an attachment:

{
	"message": {
		"subject": "[Subject]",
		"body": {
			"contentType": "[Email Body Content Type]",
			"content": "[Email Body Content]"
		},
		"toRecipients": [
			{
				"emailAddress": {
					"address": "[Email Address]"
				}
			}
		],
		"ccRecipients": [
			{
				"emailAddress": {
					"address": "[Email Address]"
				}
			}
		],
		"attachments": [
			{
				"@odata.type": "#microsoft.graph.fileAttachment",
				"name": "[Attachment Name]",
				"contentType": "[Attachment Content Type]",
				"contentBytes": "[Attachment Contents]"
			}
		]
	},
	"saveToSentItems": "[Save to Sent Items Folder]"
}​


I believe Microsoft expect to see the recipient information before any attachment sections.

And here's a screenshot of how I set up the input parameters:

34922.png

Cheers,
Eric

SebT
Level 4
Works like a charm! Thanks alot Eric.

Br,
Seb

RaviSingh
Level 3
Hello @Seb T and @ewilson,

May I ask what should be the ​​​
				"contentType": "[Attachment Content Type]",
				"contentBytes": "[Attachment Contents]"​

Do I need some code to get the contentBytes of an attachment?

Also, is this api can upload attachment of more than 4MB size?

Thanks in advance 🙂

If I was of assistance, please vote for it to be the "Best Answer". Thanks & Regards, Ravi Singh Sr. Software Engineer RPA Labcorp

ewilson
Staff
Staff
Hello @RaviSingh,

Content-Type is the MIME type of the file you’re trying to attach to the email. As an example, if I wanted to attach a PDF to the email I would set the content type to “application/pdf”. You can find a list of common MIME types here.

Content Bytes is the actual binary content of the file attachment. You can get the bytes by using the Calculation stage.

Cheers,
Eric​