Send Email Using VBScript - 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)

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.

Related

Spaces and carriage returns are being added to HTML email

We have a server generated HTML file (myFile.html) that we embed in emails that get sent to our clients. We've been using this method for years with minimal issues. We use Windows Server 2012 with smtp server via II6. Recently the HTML is getting skewed in the email. When checking the source file, all looks well. Directly opening the HTML file for viewing in a browser works as you'd expect. Here is the code we're using to read the file into memory to prepare for emailing:
Set objFile = objFSO.OpenTextFile(strFilePath)
Do While objFile.AtEndOFStream <>True
line = objFile.ReadLine
If Instr(1, line, "<table") > 0 And strHeaderWritten = "N" Then
strHeaderWritten = "Y"
strFileContent=strFileContent & strHeader
End If
strFileContent=strFileContent & line
Loop
set objFile = Nothing
And then we add the content to the email and send:
strBody = strFileContent
Set objMail = CreateObject("CDO.Message")
Set objMail.Configuration = cdoConfig
objMail.From = strFrom
objMail.ReplyTo = strReplyTo
objMail.To = strTo
objMail.Subject = strSubject
objMail.HTMLBody = strBody
objMail.Fields("urn:schemas:httpmail:importance").Value = strImportance
objMail.Send
And here are examples of what it spits out in the email. There are no errors in the source:
Has anyone else had this happen to them?
Been toiling over this for hours looking for an explanation. Thank so much for reading!
I tried using the ADO Stream method for the email, but it is still coming out the same:
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 2 'adTypeText
objStream.CharSet = Application("CharacterSet")
objStream.Open
objStream.LoadFromFile strFilePath
Do While Not objStream.EOS
line = objStream.ReadText(-2)
If Instr(1, line, "<table") > 0 And strHeaderWritten = "N" Then
strHeaderWritten = "Y"
strFileContent=strFileContent & strHeader
End If
If Instr(1, line, "< table") > 0 Then
strFileContent=strFileContent & "<h3>Broken HTML</h3>"
End If
strFileContent=strFileContent & line
Loop
objStream.Close
Set objStream = Nothing
As you can see, I also added a check for one of the persistent errors I'm seeing where there has been a space inserted between < and table. Checking the output this way did not capture the issue as in checking the text for the added space. So it must be happening after it's been written or I need to use a regex for the test. I'll try that next. I'm still seeing it in multiple email clients. Here's an example post test of ADO Stream:
This seems to be a common problem in CDO. I've found a few references online to the problem that spaces are randomly inserted into the HTMLbody.
One answer was to make the HTML body not one long string, because CDO will then insert random spaces, but to include whitespace yourself, so that CDO doesn't have to.
You could try adding VbCrLf or just plain spaces in the text you're sending.
A second suggestion made more sense to me; this can be an encoding problem. That also explains why adding your own whitespace could be a workaround.
Anyway; CDO allows for setting the encoding of the CDO.Message object before sending.
Try objMail.BodyPart.ContentTransferEncoding = "quoted-printable" to see if that solves it.
The issue is windows use of both line break and carrage return. I recommend loading the body of the text and replacing all instances of vbcrlf with just vblf and you will find you wont have the double spacing anymore.
e.g.
body = replace(body, vbcrlf, vblf)

address error when send email with 32bit outlook using automation

NOTE: edited from original after I discovered the outlook version was 32-bit not 64-bit.
I have a legacy 32-bit VB6 program that uses outlook 2010 32bit (full version, not express) to send email. Works perfect on many machines except one machine with windows 7 (64-bit I assume). Not sure if all windows 7 machines don't work or just this one.
If I use the automation technique or the MAPI technique (as I call them, see code below) outlook sends the email but the mail server kicks it back as undeliverable saying the recipient does not exist.
Now if the automation technique is used outlook displays no UI and the email is sent in the background.
However if the MAPI technique is used outlook opens it's compose email dialog which allows the user to edit the email prior to sending. What is interesting is that the recipient email looks fine but will fail as undeliverable if sent. However if the recipient is deleted and re-typed then the email will succeed. I believe a copy and re-paste works also.
This tells me there must be one or more hidden illegal characters in the recipient email address (nulls perhaps?). The code to do this shown below is very plain and I can't think of any obvious fix. txtTo is a vb6 string with an email address and this is the field that is causing all the problems.
The error message:
Your message did not reach some or all of the intended recipients.
Subject: a test from daryls cpu #2
Sent: 11/17/2017 8:01 PM
The following recipient(s) cannot be reached:
'someemail#gmail.com' on 11/17/2017 8:01 PM
None of your e-mail accounts could send to this recipient.
Automation Technique
Dim mOutlookApp As Object
Set mOutlookApp = GetObject("", "Outlook.application")
Dim olNs As Object
Set olNs = mOutlookApp.GetNamespace("MAPI")
olNs.Logon
Dim OutMail As Object
Set OutMail = mOutlookApp.CreateItem(0)
'Set the To and Subject lines. Send the message.
With OutMail
.To = txtTo
.CC = txtCC
.Subject = txtSubjext
.HTMLBody = txtBody & vbCrLf
Dim myAttachments As Object
Set myAttachments = .Attachments
vAttach = Split(mAttachments, ",")
For i = 0 To UBound(vAttach)
myAttachments.add vAttach(i)
Next i
Dim myFolder As Object
Set myFolder = olNs.GetDefaultFolder(5) 'olFolderSent
Set .SaveSentMessageFolder = myFolder
StatusBar1.Panels(1).Text = "Status: Sending"
.send
End With
MAPI Technique
'Open up a MAPI session:
With frmMain.MAPISession1
.DownLoadMail = False
.Username = ""
.LogonUI = True
.SignOn
End With
With frmMain.MAPIMessages1
.SessionID = frmMain.MAPISession1.SessionID
.Compose
.MsgIndex = -1
.RecipIndex = 0
.RecipAddress = txtTo
.RecipDisplayName = txtTo
.RecipType = mapToList
If txtCC <> "" Then
.RecipIndex = 1
.RecipDisplayName = txtCC
.RecipAddress = txtCC
.RecipType = mapCcList
End If
'spaces are important! need one space for each attachment
'NOTE .MsgNoteText = " " MUST be there see.. KB173853 in microsoft
.MsgSubject = txtSubjext
.MsgNoteText = Space$(UBound(vAttach) + 1) & vbCrLf
.MsgNoteText = txtBody & vbCrLf
For i = 0 To UBound(vAttach)
.AttachmentIndex = i
.AttachmentPosition = i
.AttachmentType = mapData
.AttachmentName = GetFileFromPath(vAttach(i))
.AttachmentPathName = vAttach(i)
Next i
StatusBar1.Panels(1).Text = "Status: Sending"
.send True
End With
More Info:
I'm making some progress. The error has to do with email type in outlook not being SMTP. If on the send-to email in the outlook compose dialog you right-click on the email address then select outlook properties and change the email type to SMTP it will work. The type displayed is the email address itself, valid values seem to be 'mailto' and 'smtp'. So if I can set the email type from vb6 it should fix the error.
The 'Answer'? https://kb.intermedia.net/article/2344
I can't believe there is no fix for this...
RESOLVED!
I realize this topic is most likely of no interest to anyone programming in the 20th century but here is the fix:
.RecipAddress = "SMTP:" & txtTo
It just came to me. :)

VBscript for sending mail using SMTP for .mht file attachment

I am using Gmail SMTP server to send out a .mht file. once send out, i am getting a lot of attachemnts and the .mht file were loading in the email body (from yahoo mail). Instead in outlook, i am getting it as a mail attachements.
VB script used for this :
Set objMessage = CreateObject("CDO.Message")
objMessage.HTMLBody = "<h1>Matrikon AM</h1>"
objMessage.Subject = "Test 1 -Blank"
objMessage.From = "xxxxx#gmail.com"
objMessage.To = "xxxxx#yahoo.com"
objMessage.TextBody = "From b.vbs"
objMessage.AddAttachment "c:\xxxxxxx\A1.mht"
objMessage.Send
I had some quick search online that "ContentMediaType" have to be define somewhere since the file extension is .mht.
Need help on this as i could not find a way to define the content-type.
My issue were exactly like mentioned in "https://stackoverflow.com/questions/15976836/modifying-the-content-type-of-an-attachment-in-a-cdo-message-object"
thanks.
This is from Help.
urn:schemas:mailheader: Namespace
The urn:schemas:mailheader: namespace defines fields that contain Internet standard message header values. Each field value (with a few exceptions) is stored as US-ASCII characters and is identical to the ASCII string found in the message stream. Non US-ASCII characters are encoded according to the RFC 1522 specification. No conversion is performed when the property value is set or updated. An application that sets the raw message header property must RFC 1522-encode non US-ASCII characters or the header value will be corrupted.
String constants are provided in the C++ header file cdosysstr.h and type library (cdoMailHeader module) for each field name. You can use these constants when referring to the fields to avoid typos and extra typing.
Example
The following example demonstrates use of the fields in the urn:schemas:mailheader: namespace. First, RFC 822 headers are added for a message. Next, body parts are added and the MIME headers set manually.
This code is for illustrative purposes only.
Copy Code
Dim iMsg as New CDO.Message
Dim Flds as ADODB.Fields
With iMsg
.To = """Someone"" <example#example.com>"
.From = """Me"" <example#example.com>"
.Subject = "Here is a sample message"
' Now set some custom mail headers using the raw fields collection
Set Flds = .Fields
With Flds
.Item("urn:schemas:mailheader:X-Mailer") = "Microsoft CDO for Windows 2000"
' I add a custom header here
.Item("urn:schemas:mailheader:Myheader")= "some value"
.Update
.Resync
End With ' Flds
End With ' iMsg
' Create a multipart/alternative (HTML) message below
Dim iBp as CDO.IBodyPart
Dim iBp2 as CDO.IBodyPart
Set iBp = iMsg ' get IBodyPart on Message object
Set Flds = iBp.Fields
Flds("urn:schemas:mailheader:content-type") = "multipart/alternative"
Flds.Update
Set iBp2 = iBp.AddBodyPart
Set Flds = iBp2.Fields
Flds("urn:schemas:mailheader:content-type") = "text/plain"
Flds("urn:schemas:mailheader:content-transfer-encoding") = "quoted-printable"
Flds.Update
Dim Stm as ADODB.Stream
Set Stm = iBp2.GetDecodedContentStream
Stm.WriteText "This is a test", stWriteLine
Stm.Flush
Set iBp2 = iBp.AddBodyPart
Set Flds = iBp2.Fields
Flds("urn:schemas:mailheader:content-type") = "text/html"
Flds("urn:schemas:mailheader:content-transfer-encoding") = "quoted-printable"
Flds.Update
Set Stm = iBp2.GetDecodedContentStream
Stm.WriteText "This is a <i>test</i>", stWriteLine
Stm.Flush
iMsg.Send

Get the actual recipient of an exchange email

I have the following bit of code:
For each Item in ofChosenFolder.Items
msgbox Item.Subject
for each recip in Item.Recipients
msgbox "sent to " & recip.address
msgbox "sent to " & recip.addressEntry
next
next
I have some emails addressed to me awalker#example.com and other addressed to projects#example.com.
All are received by my exchange mailbox.
Using the above code I always get my Exchange /O=EXAMPLE/OU=EXCHANGE.../CN=RECIPIENTS/CN=A Walker, etc and my Exchange name "A Walker". This is because Exchange resolves the emails against the Global Address Book.
Is there any way to stop it resolving the email addresses and identify the actual smtp address the email was sent to?
That looks like a perfectly valid EX type address. To get the SMTP address
Check the AddressEntry.Type property. If it is "SMTP", just use the AddressEntry.Address property.
If it is "EX", use AddressEntry.GetExchangeUser.PrimarySmtpAddress
The answer is to get PR_TRANSPORT_MESSAGE_HEADERS.
To do this in VBS:
For Each Item in myNameSpace.GetDefaultFolder(olFolderInbox).items
PropName = "http://schemas.microsoft.com/mapi/proptag/0x007D001E"
Set oPA = Item.PropertyAccessor
Header = oPA.GetProperty(PropName)
'parse the "To" line out of your header to get the email address
Next

MAPI Control Multiple attachments

I am trying to send an email with multiple attachments using the VB6 MAPIMessages control.
1) I am able to use this control to send a single attachment but it displays an error saying "Attachment not found", if I try to send more than one file.
2) I also need to suppress the warning message when I try to send an email
Any ideas?
Here's the code:
MAPISession1.SignOn
MAPIMessages1.SessionID = MAPISession1.SessionID
MAPIMessages1.Compose
MAPIMessages1.RecipDisplayName = "abbid_siddiqui#hotmail.com"
MAPIMessages1.MsgSubject = "MAPI subject with attachments"
MAPIMessages1.MsgNoteText = "This is atest"
MAPIMessages1.AttachmentIndex = 0
MAPIMessages1.AttachmentName = "test.csv"
MAPIMessages1.AttachmentPathName = "C:\test.csv"
MAPIMessages1.AttachmentIndex = 1
MAPIMessages1.AttachmentName = "holidays_2013.xls"
MAPIMessages1.AttachmentPathName = "E:\holidays_2013.xls"
MAPIMessages1.ResolveName
'Send the e-mail message to the Recipient
MAPIMessages1.Send
Try setting the AttachmentPosition property to the same value as the AttachmentIndex

Resources