You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Outlook Office Add-In开发:邮件发送前强制分类及面板弹出求助

Outlook邮件分类强制拦截实现方案

核心实现逻辑

  • 通过ItemSend事件拦截邮件发送动作,前置检查分类状态
  • 用customProperties持久化存储邮件分类标记,避免状态丢失
  • 未分类时阻止发送并弹出分类面板,完成分类后允许发送

完整修改代码

<!-- Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Email Classification Prompt</title>
    <script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>
    <style>
        /* styles.css */
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f0f0f0;
        }
        .container {
            width: 400px;
            margin: 50px auto;
            padding: 20px;
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
        h2 {
            font-size: 24px;
            margin-bottom: 20px;
        }
        .input-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 14px;
        }
        button {
            display: block;
            width: 100%;
            padding: 10px;
            background-color: #0078d4;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>NMI Email Classification</h2>
        <div class="input-group">
            <label for="classification">Select Classification:</label>
            <select id="classification">
                <option value="">请选择分类</option>
                <option value="Confidential">Confidential</option>
                <option value="Internal">Internal</option>
                <option value="Public">Public</option>
            </select>
        </div>
        <button id="ClassifyButton">Classify Email</button>
    </div>

    <script>
        Office.onReady((info) => {
            if (info.host === Office.HostType.Outlook) {
                document.getElementById("ClassifyButton").onclick = ClassifyEmail;
                // 注册邮件发送拦截事件
                Office.context.mailbox.addHandlerAsync(Office.EventType.ItemSend, onItemSend);
            }
        });

        // 发送拦截逻辑
        function onItemSend(event) {
            Office.context.mailbox.item.customProperties.getAsync(function(asyncResult) {
                if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
                    const props = asyncResult.value;
                    const hasClassification = props.get('email_classification');
                    
                    if (!hasClassification) {
                        // 未分类,阻止发送并弹出分类面板
                        event.completed({ allowEvent: false });
                        showClassificationPanel();
                    } else {
                        // 已分类,允许发送
                        event.completed({ allowEvent: true });
                    }
                } else {
                    event.completed({ allowEvent: false });
                    alert('无法验证邮件分类状态,请重试');
                }
            });
        }

        // 弹出分类面板
        function showClassificationPanel() {
            Office.context.ui.displayDialogAsync(
                window.location.href,
                { height: 300, width: 400, title: '邮件分类要求' },
                function(dialogResult) {
                    if (dialogResult.status === Office.AsyncResultStatus.Failed) {
                        alert('无法打开分类面板:' + dialogResult.error.message);
                    }
                }
            );
        }

        // 分类保存逻辑
        function ClassifyEmail() {
            var classification = document.getElementById("classification").value;
            if (!classification) {
                alert("请先选择邮件分类");
                return;
            }

            // 保存分类到自定义属性
            Office.context.mailbox.item.customProperties.setAsync(
                { 'email_classification': classification },
                function(setResult) {
                    if (setResult.status === Office.AsyncResultStatus.Succeeded) {
                        // 获取原有主题并添加分类前缀
                        Office.context.mailbox.item.subject.getAsync(function(subjectResult) {
                            if (subjectResult.status === Office.AsyncResultStatus.Succeeded) {
                                const originalSubject = subjectResult.value || '';
                                // 避免重复添加分类前缀
                                const prefix = `[${classification}] `;
                                const newSubject = originalSubject.startsWith(prefix) ? originalSubject : prefix + originalSubject;
                                
                                Office.context.mailbox.item.subject.setAsync(
                                    newSubject,
                                    function(updateResult) {
                                        if (updateResult.status === Office.AsyncResultStatus.Succeeded) {
                                            alert('邮件已分类,现在可以发送');
                                            // 关闭对话框
                                            Office.context.ui.closeContainer();
                                        } else {
                                            console.error(updateResult.error.message);
                                            alert('更新主题失败,请重试');
                                        }
                                    }
                                );
                            } else {
                                console.error(subjectResult.error.message);
                            }
                        });
                    } else {
                        console.error('保存分类失败:' + setResult.error.message);
                        alert('保存分类失败,请重试');
                    }
                }
            );
        }
    </script>
</body>
</html>

关键代码说明

  1. 发送拦截事件注册:在Office.onReady中注册ItemSend事件,每次用户点击发送时触发检查逻辑
  2. 分类状态检查:通过customProperties读取邮件的分类标记,判断是否允许发送
  3. 面板弹出:使用displayDialogAsync弹出分类页面,强制用户完成分类
  4. 分类持久化:将分类信息保存到邮件的自定义属性中,确保状态在邮件编辑过程中不会丢失
  5. 主题更新优化:避免重复添加分类前缀,保留原有主题内容

内容的提问来源于stack exchange,提问作者Aubrey Xaba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 18:22:36