question

24949452 avatar image
0 Votes"
24949452 asked ShwetaChoudhary-8869 edited

Using GRAPH API, I want to send mail with attachment that's over 10mb file size.

Hello. I'm trying to send email using graph api.
The reason I use graph api, instead of other options, is that it can send mail with attachment that's has more than 10mb file size.

After researching about this, I found out that I have to create an upload session(https://docs.microsoft.com/en-us/graph/outlook-large-attachments?tabs=http). and it's quite tricky to understand.

The goal is send an email with attachments that has more than 10mb file size.

so should I send two requests like this? and

first request :
POST https://graph.microsoft.com/v1.0/user/jaesung.park/messages/AAMkADI5MAAIT3drCAAA=/attachments/createUploadSession

POST https://graph.microsoft.com/v1.0/users/jaesung.park@streami.co/sendMail

How can tell 'sendMail' that it should refer createUploadSession before send email?

Following is my send email code, it will be very helpful, if you can be specific by giving detailed python code examples



98956-image.png






If there's a way that's more simple than this(https://docs.microsoft.com/en-us/graph/outlook-large-attachments?tabs=http), please let me know!


Thanks in advanace

microsoft-graph-mail
image.png (110.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ShwetaChoudhary-8869 avatar image
0 Votes"
ShwetaChoudhary-8869 answered ShwetaChoudhary-8869 edited

Try this if it helps

Create the upload session:

filename = 'Large File.txt'
result = requests.post(
f'{ENDPOINT}/createUploadSession',
headers={'Authorization': 'Bearer ' + access_token},
json={
'@microsoft.graph.conflictBehavior': 'replace',
'description': 'A large test file',
'fileSystemInfo': {'@odata.type': 'microsoft.graph.fileSystemInfo'},
'name': filename
}
)
upload_session = result.json()
upload_url = upload_session['uploadUrl']

This request will return information about the setting including the URL where we will send the file chunks.

Now calculate the number of chunks you will need to send:

st = os.stat(filename)
size = st.st_size
CHUNK_SIZE = 10485760
chunks = int(size / CHUNK_SIZE) + 1 if size % CHUNK_SIZE > 0 else 0
The documentation recommends a 10MB chunk size for most cases, but you may want to adjust this.

Now upload the chunks:

with open(filename, 'rb') as fd:
start = 0
for chunk_num in range(chunks):
chunk = fd.read(CHUNK_SIZE)
bytes_read = len(chunk)
upload_range = f'bytes {start}-{start + bytes_read - 1}/{size}'
print(f'chunk: {chunk_num} bytes read: {bytes_read} upload range: {upload_range}')
result = requests.put(
upload_url,
headers={
'Content-Length': str(bytes_read),
'Content-Range': upload_range
},
data=chunk
)
result.raise_for_status()
start += bytes_read

Follow Example 2 and 3 of this documentation to continue uploading byte ranges until the entire file has been uploaded.

Thanks!


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.