vbscript电子邮件问题,无法生成文本正文

时间:2015-07-16 12:01:01

标签: email vbscript

所以我正在尝试使用VBscripting发送电子邮件,否则它工作正常,但我尝试写入txt或html正文的任何​​内容都不会出现在邮件中。这是我正在使用的功能:

Function EMail( myFrom, myTo, mySubject, myTextBody, myHTMLBody, myAttachment, mySMTPServer, mySMTPPort )
' This function sends an e-mail message using CDOSYS
'
' Arguments:
' myFrom        = Sender's e-mail address ("John Doe <jdoe@mydomain.org>" or "jdoe@mydomain.org")
' myTo          = Receiver's e-mail address ("John Doe <jdoe@mydomain.org>" or "jdoe@mydomain.org")
' mySubject     = Message subject (optional)
' myTextBody    = Actual message (text only, optional)
' myHTMLBody    = Actual message (HTML, optional)
' myAttachment  = Attachment as fully qualified file name, either string or array of strings (optional)
' mySMTPServer  = SMTP server (IP address or host name)
' mySMTPPort    = SMTP server port (optional, default 25)
'
' Returns:
' status message
'

    ' Standard housekeeping
    Dim i, objEmail

    ' Use custom error handling
    On Error Resume Next

    ' Create an e-mail message object
    Set objEmail = CreateObject( "CDO.Message" )

    ' Fill in the field values
    With objEmail
        .From     = myFrom
        .To       = myTo
        ' Other options you might want to add:
        ' .Cc     = ...
        ' .Bcc    = ...
        .Subject  = mySubject
        .TextBody = myTextBody
        .HTMLBody = myHTMLBody
        If IsArray( myAttachment ) Then
            For i = 0 To UBound( myAttachment )
                .AddAttachment Replace( myAttachment( i ), "\", "\\" ),"",""
            Next
        ElseIf myAttachment <> "" Then
            .AddAttachment Replace( myAttachment, "\", "\\" ),"",""
        End If
        If mySMTPPort = "" Then
            mySMTPPort = 25
        End If
        With .Configuration.Fields
            .Item( "http://schemas.microsoft.com/cdo/configuration/sendusing"      ) = 2
            .Item( "http://schemas.microsoft.com/cdo/configuration/smtpserver"     ) = mySMTPServer
            .Item( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ) = mySMTPPort
            .Update
        End With
    End With
    ' Return status message
    If Err Then
        EMail = "ERROR " & Err.Number & ": " & Err.Description
        Err.Clear
    Else
        objEmail.send
        If Err Then
            EMail = "ERROR " & Err.Number & ": " & Err.Description
            Err.Clear
        Else
            EMail = "ok"
        End If
    End If

    ' Release the e-mail message object
    Set objEmail = Nothing
    ' Restore default error handling
    On Error Goto 0
End Function

以下是我如何称呼它:

MsgBox Email ( "address@sender.com", _
             address@receiver.com, _
             "This subject appears on the email correctly", _
             "But this txt body does not", _
             "", _
             "", _
             "mail.smtp-server.com", _
             25 )

就像我说的那样,电子邮件顺利通过,附件工作,主题工作,但无论出于何种原因,身体永远不会出现在消息中!有什么问题?

1 个答案:

答案 0 :(得分:1)

对于文字使用TextBody,对于HTML使用HTMLBody不要设置两者,目前您将HTMLBody设置为"",这将覆盖TextBody

您可以使用标志参数;

Function EMail( myFrom, myTo, mySubject, myBody, isHTML, myAttachment...

然后

if isHTML then 
    .HTMLBody = myBody
else
    .TextBody = myBody
end if