Search code examples
pythonjsonoutlookmicrosoft-graph-apiemail-client

Microsoft Graph- SendMail for a scheduled time using PidTagDeferredSendTime


I am new to working with the Microsoft Graph API, and am currently working through an example of the sample code they provide on their website for python implementation. I have a simple function that connects to outlook mail and sends an email to a specified user. I am hoping to schedule a time to send the email for some time in the future rather than sending it immediately. I found a previous post that recommended using PidTagDeferredSendTime attribute within the extendedProperties options, but I can't seem to get it to work. Everything in the code works and sends fine until I add the "singleValueExtendedProperties" lines and then it never delivers but says it sent. Has anyone got this to work? Attached is the send mail function where I am having the issues.

def send_mail(subject: str, body: str, recipient: str):
print(recipient)
request_body = {
    'message': {
        'subject': subject,
        'body': {
            'contentType': 'text',
            'content': body
        },
        'toRecipients': [
            {
                'emailAddress': {
                    'address': recipient

                }

            }

        ],
        "singleValueExtendedProperties":
            [
                {
                    "id": "PtypTime 0x3FEF",
                    "value": "2022-08-01T13:48:00"
                }
            ]
    }

}

Solution

  • Your property definition PidTagDeferredSendTime isn't correct for the datatype it should be SystemTime eg

    {
        "message": {
            "subject": "Meet for lunch?",
            "body": {
                "contentType": "Text",
                "content": "The new cafeteria is open."
            },
            "toRecipients": [
                {
                    "emailAddress": {
                        "address": "[email protected]"
                    }
                }
            ],
            "singleValueExtendedProperties": [
                {
                    "id": "SystemTime 0x3FEF",
                    "value": "2022-08-01T23:39:00Z"
                }
            ]
        }
    }

    Also make sure the datetime you want to send it is converted to UTC, what you should see is the message saved into the drafts folder (as a draft) and then it will be sent at the time you specify. eg the above request works for me in the Graph explorer