Home/8.5.3 / Domino 新郵件到達前/新郵件到達後代理程式範例

在企業 Notes/Domino 環境中,郵件的 Recipients 欄位可能會出現多餘字串或錯誤輸入,例如:

CN=mark/O=HQ @ test
CN=mark/O=HQ@test

同時也可能會有合法的網域:

mark@testcorp.com
mark@test.com

本教學示範如何建立 Domino 代理程式,自動清理 Recipients 欄位中 獨立出現的 @test 字樣(大小寫不拘)
且避免誤刪正常網域,例如 @testcorp.com

Part 1:Before New Mail Arrives(郵件進信箱前即時清理)

適用情境:
當你需要「一有郵件進來就立即清理 Recipients」,而不是等郵件存入信箱後再處理。

此代理程式會在郵件被寫入信箱之前直接修改 Recipients 欄位。


🧩 功能說明

  • 去除獨立出現的 @test
  • 不分大小寫(@Test, @TEST 也會被處理)
  • 修正常見錯誤空格(@ test @ test
  • 不會清除正常網域(如 @testcorp.com
  • 支援多值 Recipients 欄位

🧑‍💻 程式碼範例(Before new mail arrives)

<br />
Option Declare

‘ 清理 Recipients:僅移除獨立的 @test,不影響 @testcorp.com 等網域
Function CleanRecipient(ByVal s As String) As String
Dim t As String, lowerT As String, pos As Long, nextChar As String

t = Trim$(s)
‘ 修正常見空格錯誤
t = Replace(t, ” @ “, “@”)
t = Replace(t, “@ “, “@”)
t = Replace(t, ” @”, “@”)

lowerT = Lcase$(t)

Do
pos = Instr(lowerT, “@test”)
If pos = 0 Then Exit Do

‘ 取得 @test 後的字元
If pos + Len(“@test”) <= Len(lowerT) Then nextChar = Mid$(lowerT, pos + Len(“@test”), 1) Else nextChar = “” ‘ 結尾 End If ‘ 若後面是 . 則屬正常網域 → 保留 If nextChar = “” Or nextChar = ” ” Or nextChar = Chr$(10) Or nextChar = Chr$(13) Then t = Left$(t, pos – 1) lowerT = Lcase$(t) Else Exit Do End If Loop CleanRecipient = Trim$(t) End Function Sub Initialize Dim session As New NotesSession Dim doc As NotesDocument Dim recips As Variant Dim cleaned() As String Dim i As Integer, count As Integer Dim sVal As String Set doc = session.DocumentContext ‘ ⚡ 新郵件文件 If doc Is Nothing Then Exit Sub If Not doc.HasItem(“Recipients”) Then Exit Sub recips = doc.GetItemValue(“Recipients”) count = 0 For i = Lbound(recips) To Ubound(recips) sVal = Trim$(Cstr(recips(i))) If sVal <> “” Then
sVal = CleanRecipient(sVal)
If sVal <> “” Then
ReDim Preserve cleaned(0 To count)
cleaned(count) = sVal
count = count + 1
End If
End If
Next

If count > 0 Then
Call doc.ReplaceItemValue(“Recipients”, cleaned)
Else
Call doc.RemoveItem(“Recipients”)
End If
End Sub

設定流程(Before new mail arrives)

  1. 打開郵件資料庫(mail file)

  2. 建立 Agent:

    • TriggerBefore new mail arrives

    • RunLotusScript

  3. 貼上程式碼

  4. 啟用代理程式

  5. 寄測試信 → 確認 Recipients 已被即時清理

Part 2:After New Mail Arrives(郵件送達後批次清理)

適用情境:
公司政策不允許 Before New Mail Arrives,或郵件已存入信箱才要清理。

這個版本會逐筆處理 UnprocessedDocuments(未處理郵件),常用於批次清理。

程式碼範例(After new mail arrives)


Option Declare

Function CleanRecipient(ByVal s As String) As String
    Dim t As String, lowerT As String, pos As Long, nextChar As String
    
    t = Trim$(s)
    t = Replace(t, " @ ", "@")
    t = Replace(t, "@ ", "@")
    t = Replace(t, " @", "@")
    
    lowerT = Lcase$(t)
    
    Do
        pos = Instr(lowerT, "@test")
        If pos = 0 Then Exit Do
        
        If pos + Len("@test") <= Len(lowerT) Then
            nextChar = Mid$(lowerT, pos + Len("@test"), 1)
        Else
            nextChar = ""
        End If
        
        If nextChar = "" Or nextChar = " " Or nextChar = Chr$(10) Or nextChar = Chr$(13) Then
            t = Left$(t, pos - 1)
            lowerT = Lcase$(t)
        Else
            Exit Do
        End If
    Loop
    
    CleanRecipient = Trim$(t)
End Function

Sub Initialize
    Dim session As New NotesSession
    Dim db As NotesDatabase
    Dim col As NotesDocumentCollection
    Dim doc As NotesDocument
    Dim recips As Variant
    Dim cleaned() As String
    Dim i As Integer, count As Integer
    Dim sVal As String

    Set db = session.CurrentDatabase
    Set col = db.UnprocessedDocuments

    Set doc = col.GetFirstDocument
    While Not doc Is Nothing
        
        If doc.HasItem("Recipients") Then
            recips = doc.GetItemValue("Recipients")
            count = 0
            
            For i = Lbound(recips) To Ubound(recips)
                sVal = Trim$(Cstr(recips(i)))
                If sVal <> "" Then
                    sVal = CleanRecipient(sVal)
                    If sVal <> "" Then
                        ReDim Preserve cleaned(0 To count)
                        cleaned(count) = sVal
                        count = count + 1
                    End If
                End If
            Next
            
            If count > 0 Then
                Call doc.ReplaceItemValue("Recipients", cleaned)
            Else
                Call doc.RemoveItem("Recipients")
            End If
            Call doc.Save(True, False)
        End If
        
        Set doc = col.GetNextDocument(doc)
    Wend
End Sub

設定流程(After new mail arrives)

  1. 建立 Agent:
    • TriggerAfter new mail has arrived 或定時排程
    • RunLotusScript
  2. 貼上程式碼
  3. 啟用後即可自動批次處理新郵件

🧠 本文章與所附圖片部分內容為 AI 生成或 AI 輔助產製。文中提及之商標、品牌名稱、產品圖片及相關標識, 其著作權與商標權均屬原權利人所有,本網站僅作為資訊呈現與示意使用

最新文章

華碩推新韌體:修補八項重大路由器安全漏洞,護航 AiCloud 服務安全

華碩修補路由器8項漏洞 影響啟用 AiCloud 服務…

AI賦能:分解式儲存與超融合架構的智慧進化

AI 在分解式儲存與超融合架構中的新興應用脈絡
近一季…

即時惡意軟體分析的儲存革命—分解式叢集、FlashBlade 與超融合架構

攻擊與惡意軟體分析的資料儲存新視角:從分解式儲存叢集、…

破解高風險攻擊:近一年系統漏洞徹底分析與即時修補實務

系統漏洞分析與修補策略:針對近一年高風險攻擊的實務探討

中國部署Claude聊天機器人,掀起全新資訊安全風險

中國利用自己的Claude聊天機器人所引發的新型安全風…

Oracle EBS成攻擊焦點,Canon、Mazda美國分公司雙遭駭客侵入

Canon、Mazda 美國分公司遭駭!Oracle …

推薦文章
AI賦能:分解式儲存與超融合架構的智慧進化

AI 在分解式儲存與超融合架構中的新興

破解高風險攻擊:近一年系統漏洞徹底分析與即時修補實務

系統漏洞分析與修補策略:針對近一年高風險

中國部署Claude聊天機器人,掀起全新資訊安全風險

中國利用自己的Claude聊天機器人所

分析完成 ✔