Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Implement custom encryption and decryption functionality in an Outlook add-in to secure email communications. The OnMessageDecrypt event lets your add-in automatically identify encrypted messages and handle decryption, content display, and error notifications.
Overview of the encryption and decryption workflows
Tip
- The encryption and decryption workflows implement the event-based activation feature. If you aren't familiar with event-based activation in Outlook add-ins, we recommend that you first learn about the feature and its implementation. To learn more, see Activate add-ins with events.
- The minimum requirement set and supported platforms may vary for each API recommended in this section. We recommend verifying any requirements against Outlook JavaScript API requirement sets and supplementing it with documentation for the specific API.
The following table provides an overview of the encryption and decryption workflows of an Outlook add-in. It also identifies whether a step requires a custom solution or is supported by the Office JavaScript (Office.js) API library.
| Step | Implementation |
|---|---|
| User composes a message and uses your add-in to apply encryption rules | You must implement your own encryption protocol so that the add-in can secure the contents of the message and its attachments. |
| User sends the message | Implement a handler for the OnMessageSend event so that your add-in can automatically run your encryption protocol when the user selects Send. To identify a message that was encrypted using your add-in during the decryption process, use the internet headers APIs to add a header to a message. The header key must match the value specified in the HeaderName attribute of the <LaunchEvent> element for the OnMessageDecrypt event in the add-in's manifest. For more information, see Implement decryption using event-based activation. |
| Recipient receives the encrypted message and opens it | If the recipient has the same add-in that was used to encrypt the message installed in Outlook, the add-in checks whether the header key included in the message matches the value specified for the OnMessageDecrypt event in the manifest. This operation is automatically done by an add-in that handles the OnMessageDecrypt event, so that you don't have to manually implement the check. If the headers match, the OnMessageDecrypt event occurs and its handler runs. For more information, see Implement decryption using event-based activation. |
| Add-in decrypts the message | You must implement your own decryption protocol in the OnMessageDecrypt event handler. While your add-in decrypts the message and its attachments, a notification is shown to the user to alert them that their message is being processed by the add-in. This notification is automatically shown by an add-in that handles the OnMessageDecrypt event, so that you don't have to manually create one. |
| Recipient views the decrypted message and its attachments, if any | Once the decryption operation is complete, a notification is automatically shown to the user to alert them that the add-in has finished processing the message. In your OnMessageDecrypt handler, call the event.completed method and pass it a MessageDecryptEventCompletedOptions object. With the MessageDecryptEventCompletedOptions object, you can specify whether to display the decrypted content to the recipient. For more information, see Implement event handling. |
Try out a completed add-in
To immediately see a completed encryption add-in in action, try out the Encrypt and decrypt messages in Outlook sample.
Implement decryption using event-based activation
You must implement your own encryption and decryption protocols. The add-in must also be configured to handle the OnMessageDecrypt event to conveniently determine when your add-in can decrypt a message and display the decrypted contents. To implement the OnMessageDecrypt event, you must:
Supported environments
The OnMessageDecrypt event is supported on the Message Read surface. Support varies by client and Exchange environment, as shown in the following table.
| Client | Exchange Online | Exchange Subscription Edition (SE) | Exchange Server 2019 | Exchange Server 2016 |
|---|---|---|---|---|
| Web browser | Supported | Not available | Not available | Not available |
| Windows (new) | Supported | Not available | Not available | Not available |
| Windows (classic) Version 2602 (Build 19725.20126) and later |
Supported | Not available | Not available | Not available |
| Mac | Not available | Not available | Not available | Not available |
| Android | Not available | Not available | Not available | Not available |
| iOS | Not available | Not available | Not available | Not available |
Configure the manifest
Note
The OnMessageDecrypt event and "extensions.autoRunEvents.events.options.headerName" property are in preview with the unified manifest. Don't use the decryption feature with the unified manifest in a production add-in.
In your add-in's manifest.json file, you must configure the "extensions.runtimes" array and add the "extensions.autoRunEvents" array to enable event-based activation in your add-in.
Add the following object to the
"extensions.runtimes"array. Note the following about this markup.- The
"id"of the runtime is set to the descriptive name"autorun_runtime". - The
"code"property has a child"page"property that is set to an HTML file and a child"script"property that is set to a JavaScript file. Office uses one of these values depending on the platform.- Outlook on the web and the new Outlook on Windows execute the handler in a browser runtime, which loads an HTML file. That file, in turn, contains a
<script>tag that loads the JavaScript file. - Classic Outlook on Windows executes the event handler in a JavaScript-only runtime, which loads a JavaScript file directly. For more information, see Runtimes in Office Add-ins.
- Outlook on the web and the new Outlook on Windows execute the handler in a browser runtime, which loads an HTML file. That file, in turn, contains a
- The
"lifetime"property is set to"short", which means that the runtime starts up when the event is triggered and shuts down when the handler completes. - Actions map JavaScript handlers to the
OnMessageSendandOnMessageDecryptevents.
"runtimes": [ { "requirements": { "capabilities": [ { "name": "Mailbox", "minVersion": "1.16" } ] }, "id": "autorun_runtime", "type": "general", "code": { "page": "https://localhost:3000/launchevents.html", "script": "https://localhost:3000/launchevents.js" }, "lifetime": "short", "actions": [ { "id": "onMessageSendHandler", "type": "executeFunction" }, { "id": "onMessageDecryptHandler", "type": "executeFunction" } ] } ],- The
Add the following
"autoRunEvents"array as a property of the object in the"extensions"array. Note the following about this markup.- An event object is created for each event that the add-in handles. In this sample, one event object is created for
OnMessageSendand another forOnMessageDecrypt. Both events use their unified manifest event name,"messageSending"and"messageDecrypt", as described in the supported events table. - To ensure that the appropriate handler runs when an event occurs, the function name provided in
"actionId"must match the name used in the"id"property of the applicable object in the"runtimes.actions"array from an earlier step. - The "options" property provides additional configuration for the
OnMessageSendandOnMessageDecryptevents.- For
OnMessageSend, the "sendMode" option specifies whether a user is able to send their message if it doesn't meet an add-in's conditions. In this sample, the"softBlock"option is specified. To learn more about send mode options, see the "Available send mode options" section of Handle OnMessageSend and OnAppointmentSend events in your Outlook add-in with Smart Alerts. - For
OnMessageDecrypt, the "headerName" option specifies the internet header name used to identify whether a message was encrypted by the add-in. The same header is added to a message that's encrypted by the add-in.
- For
"autoRunEvents": [ { "events": [ { "type": "messageSending", "actionId": "onMessageSendHandler", "options": { "sendMode": "softBlock" } }, { "type": "messageDecrypt", "actionId": "onMessageDecryptHandler", "options": { "headerName": "contoso-encrypted" } } ] } ]- An event object is created for each event that the add-in handles. In this sample, one event object is created for
Implement event handling
The OnMessageDecrypt event handler is used to run the decryption operation and determine whether to display the decrypted contents of a message.
- To ensure your handler runs when the
OnMessageDecryptevent occurs, callOffice.actions.associatein the JavaScript file where the handler is implemented. This maps the handler name specified in theFunctionNameattribute of the<LaunchEvent>element in the manifest to its JavaScript counterpart. - Once the decryption operation finishes, you must call
event.completedto signal to the client that your add-in has completed processing theOnMessageDecryptevent. To display the decrypted contents of a message and its attachments, pass a MessageDecryptEventCompletedOptions object to theevent.completedcall and set its allowEvent property totrue. Then, specify the decrypted contents of the message in the object's emailBody and attachments properties. You can also specify any data that your add-in may need for processing in the contextData property. For example, you can store custom internet headers to decrypt messages in reply and forward scenarios.
Note
Be mindful of the following when creating an event-based add-in for classic Outlook on Windows.
- Imports aren't currently supported in the JavaScript file containing the event handler.
- When the JavaScript function specified in the manifest to handle an event runs, code in
Office.onReady()andOffice.initializeisn't run. We recommend adding any startup logic needed by the event handler, such as checking the user's Outlook version, to the event handler instead.
The following is an example of an OnMessageDecrypt event handler.
function onMessageDecryptHandler(event) {
// Your code to decrypt the contents of a message would appear here.
...
// Use the results from your decryption process to display the decrypted contents of the message body and attachments.
const decryptedBodyContent = "<p>Please find attached the recent report and its supporting documentation.</p>";
const decryptedBody = {
coercionType: Office.CoercionType.Html,
content: decryptedBodyContent
};
// Decrypted content and properties of a file attachment.
const decryptedPdfFile = "JVBERi0xLjQKJeLjz9MKNCAwIG9i...";
const pdfFileName = "Fabrikam_Report_202509";
// Decrypted properties of a cloud attachment.
const cloudFilePath = "https://contosostorage.com/reports/weekly_forecast.xlsx";
const cloudFileName = "weekly_forecast.xlsx";
// Decrypted content and properties of an inline image.
const decryptedImageFile = "iVBORw0KGgoAAAANSUhEUgAA...";
const imageFileName = "banner.png";
const imageContentId = "image001.png@01DC1DD9.1A4AA300";
const decryptedAttachments = [
{
attachmentType: Office.MailboxEnums.AttachmentType.File,
content: decryptedPdfFile,
isInline: false,
name: pdfFileName
},
{
attachmentType: Office.MailboxEnums.AttachmentType.Cloud,
isInline: false,
name: cloudFileName,
path: cloudFilePath
},
{
attachmentType: Office.MailboxEnums.AttachmentType.File,
content: decryptedImageFile,
contentId: imageContentId,
isInline: true,
name: imageFileName
}
];
event.completed({
allowEvent: true,
emailBody: decryptedBody,
attachments: decryptedAttachments,
contextData: { messageType: "ReplyFromDecryptedMessage" }
});
}
// IMPORTANT: To ensure your add-in is supported in Outlook, remember to map the event handler name specified in the manifest to its JavaScript counterpart.
Office.actions.associate("onMessageDecryptHandler", onMessageDecryptHandler);
Tip
When images are added to a message as inline attachments, they're automatically assigned a content ID. In the body of a message, the content ID of an inline attachment is specified in the src attribute of the <img> element similar to the following example.
<img width=96 height=96 id="Picture_1" src="cid:image001.png@01DC1E6F.FC7C7410">
To easily identify and provide these inline attachments during decryption, we recommend saving the content IDs of inline attachments to the message header during encryption. Call Office.context.mailbox.item.getAttachmentsAsync to get the content ID of an inline attachment. Then, call Office.context.mailbox.item.internetHeaders.setAsync to save the ID to the header of the message.
Decrypt Outlook item attachments (preview)
Support for decrypting Outlook item attachments (Office.MailboxEnums.AttachmentType.Item), particularly email attachments, is available for preview in Outlook on the web and on Windows (new and classic). To preview this feature in classic Outlook on Windows, you must install Version 2606 (Build 20114.15110) or later. Then, join the Microsoft 365 Insider program and select the Beta Channel option to access Office beta builds. To test this feature using the sample code in this article, update the onMessageDecryptHandler function with the following code.
// Decrypted content and properties of an email attachment.
const decryptedEmailFile = "VGhpcyBpcyBhIHRleHQgZmlsZS4=...";
const emailFileName = "Fabrikam_Report_202508.eml";
const decryptedAttachments = [
...
{
attachmentType: Office.MailboxEnums.AttachmentType.Item,
content: decryptedEmailFile,
name: emailFileName
}
];
...
Customize error messages for the decryption operation (preview)
Custom error messages for failed decryption operations are available for preview in Outlook on the web and on Windows (new and classic). To preview this feature in classic Outlook on Windows, you must install Version 2606 (Build 20114.15110) or later. Then, join the Microsoft 365 Insider program and select the Beta Channel option to access Office beta builds.
If the decryption operation fails, the allowEvent property of the event.completed call is set to false, and Outlook shows the following default notification to the user: "<Add-in name> failed to process your message." To specify a custom error message, set the errorMessage property of your add-in's event.completed call. Your custom message is prefixed with "Error from <add-in name>:". If your custom message can't be shown, the default notification is shown instead.
The following code sample shows how to specify a custom error message for your decryption add-in.
event.completed({
allowEvent: false,
errorMessage: "This message couldn't be decrypted. Contact the Contoso IT team for further assistance."
});
Manage distribution of decrypted content (preview)
To help prevent unauthorized distribution of decrypted content, access control options are available for preview in Outlook on the web and on Windows (new and classic). To preview this feature in classic Outlook on Windows, you must install Version 2606 (Build 20114.15110) or later. Then, join the Microsoft 365 Insider program and select the Beta Channel option to access Office beta builds.
To limit printing, copying, or saving of decrypted content, include the accessControls property of the event.completed call. Then, set the allowPrint, allowCopyPaste, and allowSave properties to false. If the accessControls property isn't specified, access controls default to true.
To test this feature using the sample code in this article, update the event.completed call of the onMessageDecryptHandler function with the following code.
event.completed({
allowEvent: true,
emailBody: decryptedBody,
attachments: decryptedAttachments,
contextData: { messageType: "ReplyFromDecryptedMessage" },
accessControls: {
allowPrint: false,
allowCopyPaste: false,
allowSave: false
}
});
Note
- In Outlook on the web, setting the
allowCopyPasteproperty tofalsealso prevents users from capturing their screen in the form of screenshots or recordings. The screen capture policy remains in effect until the user reloads the Outlook browser tab. - In Outlook on the web and the new Outlook on Windows, setting the
allowPrintproperty tofalsedisables the context menu (which provides options such as Copy, Select all, and Print). If theallowCopyPasteproperty is set totrue, the user can still copy content by pressing Ctrl+C, but the Copy option in the context menu isn't available.
Behavior and limitations
Be aware of the behaviors and limitations of event-based add-ins. To learn more, see Activate add-ins with events.
Since each add-in uses its own encryption protocol, a message can only be decrypted by the same add-in that encrypted it. When a user doesn't have the required add-in installed to decrypt a message, a notification alerts them that the message is encrypted. To guide the user through the decryption process, customize a placeholder message for the body of the encrypted message. The placeholder message can include information on how to install your add-in. To set the message body during the encryption process, call Office.context.mailbox.item.body.setAsync.
To ensure data security and confidentiality, decrypted content isn't stored on the Outlook client. The contents of an encrypted message are decrypted every time a user opens it.
An encrypted message must first be decrypted before a user can reply or forward it. A user can't reply or forward an encrypted message while it's being decrypted.
If a user navigates to another mail item while an encrypted message is being decrypted, the decryption process stops running. The user must select or open the message again to activate the decryption process.
When replying to or forwarding encrypted messages, drafts are saved unencrypted in the Drafts folder.
The
attachmentsproperty of theevent.completedmethod doesn't support attachments of typeOffice.MailboxEnums.AttachmentType.Item, except for preview in Outlook on the web and on Windows (new and classic). To learn more, see Decrypt Outlook item attachments (preview).Custom encryption add-ins can't encrypt messages that are already protected by DRM or S/MIME.
In Outlook on the web and the new Outlook on Windows, when encrypted messages are grouped by conversation, only the currently selected message from the conversation thread is decrypted. The other messages in the conversation thread remain encrypted until they're selected.
In Outlook on the web and the new Outlook on Windows, users can only download a decrypted message in the EML format. The option to download in the MSG format is unavailable.
Decryption notifications
Add-ins that handle the OnMessageDecrypt event automatically display notifications in certain decryption scenarios as described in the following table.
| Notification | Scenario |
|---|---|
| <Add-in name> is unavailable and can't process your message at this time. | Applies to classic Outlook on Windows only. This notification is shown when the add-in fails to load because an error prevented the add-in from loading or the user's client or machine is offline. |
| <Add-in name> failed to process your message. | An error was encountered while the add-in was decrypting the message. To retry the decryption operation, the recipient must switch to another message, then open the encrypted message again to invoke the OnMessageDecrypt event. |
| <Add-in name> add-in is decrypting your message. | The add-in is handling the OnMessageDecrypt event to decrypt the message. |
| This message is encrypted by <add-in name> add-in. | This notification is shown to recipients who don't have the necessary encryption add-in installed. To provide guidance on how to decrypt the message, include a placeholder message in the body of the encrypted message. For more information, see Behavior and limitations. |
| <Add-in name> add-in has decrypted your message. | The add-in successfully decrypted the contents of the message. The user can now view the message and its attachments. |
| <Add-in name> is taking longer than expected to process your message. | The add-in has been running for more than five seconds, but less than five minutes. |
| <Add-in name> timed out. To retry, select another email and then return to this message. | The add-in times out after running for five minutes. To retry the decryption operation, the recipient must switch to another message, then open the encrypted message again to invoke the OnMessageDecrypt event. |
| <Add-in name> timed out. (preview) | The add-in times out after running for five minutes. This notification includes a Retry action so the recipient can retry the decryption operation without switching to another message. This retry feature is available for preview in Outlook on the web and on Windows (new and classic). To preview this feature in classic Outlook on Windows, you must install Version 2606 (Build 20114.15110) or later. Then, join the Microsoft 365 Insider program and select the Beta Channel option to access Office beta builds. |
| <Add-in name> can't process this message because it's protected by a built-in security feature. | The add-in tries to process a message that's already protected by DRM or S/MIME. |
| Custom error message (preview) | An error was encountered while the add-in was decrypting the message. To retry the decryption operation, the recipient must switch to another message, then open the encrypted message again to invoke the OnMessageDecrypt event. For guidance on how to customize an error message for the decryption operation, see Customize error messages for the decryption operation (preview). |
See also
- Sample: Encrypt and decrypt messages in Outlook
- Privacy and security for Office Add-ins
- Activate add-ins with events
- Troubleshoot event-based and spam-reporting add-ins
- Get and set internet headers on a message in an Outlook add-in
- Manage the sensitivity label of your message or appointment in compose mode