Search code examples
asp-classiccdo.message

Specify my own message-id header when sending email with cdo.message


Is there a way to specifty my own message-id with CDO ?

Using the following configuration, the message-id is still generated by the cdo component and ignores the one I specified.

<%
Const cdoSendUsingPort = 2

Dim iMsg, iConf, Flds
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields

' set the CDOSYS configuration fields to use port 25 on the SMTP server
With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "[email protected]"

    .Item("urn:schemas:mailheader:message-id") = "[email protected]"
.Update
End With

With iMsg
    Set .Configuration = iConf

    .From = "[email protected]"
    .Subject = "test"
    .To = "[email protected]"
    .HTMLBody = "test"
    .Send
End With
Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing
%>

Solution

  • The configuration object iConf is irrelevant to the message object iMsg, you don't need to create it.

    So, remove Set iConf = CreateObject("CDO.Configuration") and replace Set Flds = iConf.Fields with Set Flds = iMsg.Fields.

    You need to use iMsg.Fields to set headers for the message.

    Setting Message Header Fields

    Dim iMsg, iConf, Flds
    Set iMsg = CreateObject("CDO.Message")
    Set Flds = iMsg.Fields
    Set iConf = CreateObject("CDO.Configuration")
    Set Flds = iConf.Fields
    
    ' set the CDOSYS configuration fields to use port 25 on the SMTP server
    With Flds
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.com"
        .Update
    End With
    
    With iMsg.Fields
        .Item("urn:schemas:mailheader:message-id") = "[email protected]"
        .Update
    End With
    
    With iMsg
        Set .Configuration = iConf
    
        .From = "[email protected]"
        .Subject = "test"
        .To = "[email protected]"
        .HTMLBody = "test"
        .Send
    End With