Search code examples
vbscript

Send Email Using VBScript


I want to send html table in email using the code below. It is working properly with smalls html tables. When a large html table is sent, it throws an error.

"The message could not be sent to the SMTP server. The transport error code was 0x800ccc63. The server response was 501 Syntax error - line too long"

How can I send large html table content using vbscript?

 Set objMessage = CreateObject("CDO.Message") 

    objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True

    objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 

    objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp server name" 

    objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465    

    objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 

    objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "email address" 

    objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password" 

    objMessage.Configuration.Fields.Update

    objMessage.Subject = "subject"
    objMessage.From = "from email"    
    objMessage.To = "to email"  
    objMessage.HTMLBody = "html body that contains large table"

    objMessage.Send
    If Err Then
        rtmsg = "ERROR " & Err.Number & ": " & Err.Description
        Err.Clear()
    Else
        rtmsg = "Message sent"
    End If

    MsgBox(rtmsg)

Solution

  • This can happen if you have too many consecutive characters without line breaks or white spaces. Some mail servers reject mails containing such content as spam (this is not exactly about vbscript, but explains what happens):

    Some e-mail systems use an unsolicited commercial e-mail (UCE) filter that may be configured to reject e-mail activities that have more than 999 consecutive characters in the body of the e-mail message. An (...) e-mail activity may be rejected by the UCE filter in the recipient's organization if either of the following conditions is true:

    •The message body contains more than 999 consecutive characters.

    •The message contains no line feeds or carriage returns.

    Note: Unsolicited commercial e-mail is also known as spam.

    (http://support.microsoft.com/kb/896997/en-us)

    Try adding White spaces or new lines. When building an html table try adding an vbcrlf (new line) after a </tr> or </td> for example.