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

为何SMTP的CDO Object无法接受Mail.Sender.SMTPAddress作为.to属性?

问题:Redemption获取的EX类型发件人邮箱导致CDO SMTP发送失败

我使用Redemption库读取邮件发件人信息,再通过CDO.Object的SMTP协议发送邮件。当通过Mail.Sender.SMTPAddress获取EX类型的发件人邮箱时,会触发以下错误:

CDO.Message.1: The transport lost its connection to the server.

关键现象:

  • 当mailReceiver参数来自Redemption的Mail.Sender.SMTPAddress(即使通过cStr()转换为字符串)时,必现错误
  • 直接给mailReceiver赋值为相同邮箱的字符串(例如mailReceiver = "xx@like.com"),发送完全正常

以下是我的代码片段:

sub sendMailSMTP(mailSubject, mailSender, mailReceiver, CC, BCC, replyTo, HTMLBody, attachmentsPath_List, REMOTE_SMTP, SMTP, PORT_SMTP, SCHEMAS)
    HTML_CID = ""
    set CDOObject = CreateObject("CDO.Message")
    with CDOObject
        .subject = mailSubject
        .from = mailSender
        .to = mailReceiver
        .CC = CC
        .BCC = BCC
        .replyTo = replyTo
        .HTMLBody = HTMLBody & HTML_CID
        .TextBodyPart.Charset = "utf-8"
        .AddAttachment LOGO_MAIL
        .Attachments(1).Fields.Item("urn:schemas:mailheader:content-disposition") ="attachment;filename="& replace(split(LOGO_MAIL, "\")(ubound(split(LOGO_MAIL, "\"))),".jpg","")
        .Attachments(1).Fields.Update
        for each attachmentPath in attachmentsPath_List
            .AddAttachment attachmentPath
        next
    end with

    with CDOObject.Configuration.Fields
        .Item(SCHEMAS & "sendusing") = REMOTE_SMTP
        .Item(SCHEMAS & "smtpserver") = SMTP
        .Item(SCHEMAS & "smtpserverport") = PORT_SMTP 
        .Item(SCHEMAS & "smtpconnectiontimeout") = 60
        .Update
    end with 
    CDOObject.Send
    set CDOObject = Nothing
end sub 

可能的原因及解决办法

  • 隐藏不可见字符:Redemption返回的SMTPAddress可能包含零宽空格、换行符这类不可见控制字符,表面和手动输入的字符串一致,但CDO解析时会判定为无效邮箱格式,进而导致SMTP连接异常。
  • EX地址格式残留:EX类型是Exchange专属地址格式,Redemption的SMTPAddress虽然返回了SMTP格式字符串,但可能附带Exchange相关的隐藏元数据,转字符串时这些数据没被完全清除,CDO无法正确解析。
  • 编码不兼容:Redemption返回的字符串编码和VBScript默认编码不匹配,导致CDO读取到乱码或无效的邮箱内容。

验证与修复步骤

  1. 检查字符串差异:添加Debug代码对比Redemption获取的地址和手动输入的地址:

    Dim redemptionAddr : redemptionAddr = cStr(Mail.Sender.SMTPAddress)
    Dim manualAddr : manualAddr = "xx@like.com"
    WScript.Echo "Redemption地址长度:" & Len(redemptionAddr) & ",内容:" & redemptionAddr
    WScript.Echo "手动输入地址长度:" & Len(manualAddr) & ",内容:" & manualAddr
    

    如果长度不一致,说明存在隐藏字符。

  2. 清理字符串内容:用正则表达式过滤掉所有非打印字符:

    Dim regEx : Set regEx = New RegExp
    regEx.Pattern = "[^\x20-\x7E]" ' 仅保留可打印ASCII字符
    regEx.Global = True
    mailReceiver = regEx.Replace(cStr(Mail.Sender.SMTPAddress), "")
    
  3. 直接获取纯SMTP地址:改用Redemption的GetExchangeUser.PrimarySmtpAddress属性获取地址,这个属性会直接返回无残留的纯SMTP格式字符串:

    mailReceiver = Mail.Sender.AddressEntry.GetExchangeUser.PrimarySmtpAddress
    

内容的提问来源于stack exchange,提问作者Nik Ohler

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 22:01:01