| title | Encrypt and decrypt messages in Outlook | |||||||
|---|---|---|---|---|---|---|---|---|
| page_type | sample | |||||||
| urlFragment | outlook-encrypt-decrypt-messages | |||||||
| products |
|
|||||||
| languages |
|
|||||||
| extensions |
|
|||||||
| description | Learn how to create an Outlook add-in that encrypts and decrypts messages. |
Applies to: Outlook on Windows (classic)
Implement custom encryption and decryption functionality in an Outlook add-in to secure email communications. The sample shows how to:
- Encrypt message content and attachments before sending using the
OnMessageSendevent. - Automatically identify and decrypt encrypted messages when the recipient opens them using the
OnMessageReadevent.
To learn about key components of this sample, see Create an encryption Outlook add-in.
Note
- The
OnMessageReadevent is in preview. Features in preview shouldn't be used in production add-ins. - This sample uses simplified encryption and decryption protocols for demonstration purposes only. Don't use these protocols in production add-ins.
- Classic Outlook on Windows starting in Version 2510 (Build 19312.20000)
-
A Microsoft 365 subscription.
Note: If you don't have a Microsoft 365 subscription, you might qualify for a free developer subscription that's renewable for 90 days and comes configured with sample data. For details, see the Microsoft 365 Developer Program FAQ.
-
To preview the
OnMessageReadevent in classic Outlook on Windows:- Join the Microsoft 365 Insider program and choose the Beta Channel in the Outlook client. Your client must be on Version 2510 (Build 19312.20000) or later.
- Configure your computer's registry to reference the local beta copy of the Office.js API:
- In the registry, navigate to
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\16.0\Outlook\Options\WebExt\Developer. If the key doesn't exist, create it. - Create an entry named
EnableBetaAPIsInJavaScriptand set its value to1.
- In the registry, navigate to
-
Install a recent version of npm and Node.js on your computer if you want to run the web server on localhost. To check if you've already installed these tools, from a command prompt, run the following commands.
node -v npm -v
To host the web server on localhost, follow these steps.
-
Clone or download this repository.
-
From a command prompt, go to the root of the project folder /samples/outlook-encrypt-decrypt-messages.
-
Run the following commands.
npm install npm run build npm start
This starts the web server on localhost and sideloads the manifest.xml file to Outlook.
-
Follow the steps in Try it out to test the sample.
-
To stop the web server and uninstall the add-in from Outlook, run the following command.
npm stop
Once the add-in is loaded in Outlook, use the following steps to try out its functionality.
-
In classic Outlook on Windows, create a new message.
-
Add recipients, a subject, and content to the message body.
Note: The recipient must also have the add-in installed to decrypt the message. To test this sample, send the message to yourself.
-
Optionally, add attachments to the message.
Note: The decryption API doesn't currently support attachments of type
Office.MailboxEnums.AttachmentType.Item. -
Select Send.
When the OnMessageSend event occurs, the encrypted message body and attachments are added as .txt files to the message. The body is replaced with a placeholder message and the original attachments are removed from the message.
Select the encrypted message from the inbox.
The OnMessageRead event occurs and decrypts the message content and its attachments. The following notification is displayed: "Office Add-ins Sample Encryption Add-in is decrypting your message".
Once decryption is complete, the message body and attachments are shown and the following notification is displayed: "Office Add-ins Sample Encryption Add-in has decrypted your message".
- Did you experience any problems with the sample? Create an issue and we'll help you out.
- We'd love to get your feedback about this sample. Go to our Office samples survey to give feedback and suggest improvements.
- For general questions about developing Office Add-ins, go to Microsoft Q&A using the office-js-dev tag.
Copyright (c) 2026 Microsoft Corporation. All rights reserved.
This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
| Solution | Author(s) |
|---|---|
| Encrypt and decrypt messages in Outlook | Microsoft |
| Version | Date | Comments |
|---|---|---|
| 1.0 | January 27, 2026 | Initial release |

