You are in right path in creating a message in specified folder in outlook. when you create a message, by default it will be drafted in Drafts folder. You can move the message to specified folder using /messages/message-Id/move
endpoint. However, isDraft
value will be in true
state until you send the message using /messages/message-Id/send
and this is expected.
In my case, I used sample python code to convert MIME message to Base64-Encoded MIME String.
import base64
from email.message import EmailMessage
msg = EmailMessage()
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
msg['Subject'] = 'Test Email via Microsoft Graph API'
msg.set_content('Hi...This is the body of the email in MIME format. \n \n Thanks')
raw_bytes = msg.as_bytes()
base64_encoded = base64.b64encode(raw_bytes).decode('utf-8')
print("Base64-Encoded MIME String:\n")
print(base64_encoded)
I called https://graph.microsoft.com/v1.0/users/[email protected]/messages
endpoint to create a message using base64 encoded MIME string from previous step.
Request Body: base64 encode string
Request Header: Content type = text/plain
Now I called https://graph.microsoft.com/v1.0/users/[email protected]/messages/{message-id}/move
to move to specified folder.
Request Body: { "destinationId": "< Destination folder Id>"}
Request Header: Content type = application/json
sent
endpoint to send the message and read the email content using https://graph.microsoft.com/v1.0/me/messages?$search="<Subject mail>"
Successfully created message and sent