Work with RTF and Word


We often send emails with RTF and Word document as attachments by EASendMail. We can use AddAttachment method to attach RTF and Word document to message very easily. But here we will discuss how to convert RTF or Word document to html file and send the html email to end users without attachments.

First of all, we need to use Word Object(Please make sure Microsoft Word is installed on your machine) to convert the RTF or Doc to html file, secondly we can use ImportMailEx method to import html and embedded pictures to the message. The following vbscript demonstrates how to convert *.rtf to html and send message out.

On Error Resume Next

Dim oWordApp, oWordDocuments, oWordDoc, oConverters, oConverter
Dim source, target
source = "c:\document.rtf"
target = "c:\document.htm"

Err.Clear
Set oWordApp = CreateObject("Word.Application")
If Err.Number <> 0 Then
	WScript.Echo Err.Description
	WScript.Quit()
End If

oWordApp.Visible = False
Set oWordDocuments = oWordApp.Documents

Const wdFormatHTML = 8

Err.Clear
Set oWordDoc = oWordDocuments.Open( source )
If Err.Number <> 0 Then
	WScript.Echo Err.Description
	oWordApp.Quit()
	WScript.Quit()
End If

Err.Clear
oWordDoc.SaveAs target, wdFormatHTML     

oWordDoc.Close() 
oWordApp.Quit()

If Err.Number <> 0 Then
	WScript.Echo Err.Description
	WScript.Quit()
End If

Dim oSmtp
Set oSmtp = CreateObject("EASendMailObj.Mail")

'The license code for EASendMail ActiveX Object,
'for evaluation usage, please use "TryIt" as the license code.
oSmtp.LicenseCode = "TryIt"

oSmtp.ServerAddr = "myserver"

'If your server require user authentication
'oSmtp.UserName = "test@adminsystem.net"
'oSmtp.Password = "test"

'If your server requires SSL connection
'oSmtp.SSL_init
'oSmtp.SSL_starttls = 0
'oSmtp.ServerPort = 465

''If your server requires TLS connection
'oSmtp.SSL_init
'oSmtp.SSL_starttls = 1
'oSmtp.ServerPort = 25

oSmtp.FromAddr = "test@mydomain.com"
oSmtp.AddRecipient "tester", "test@mydomain.com", 0

oSmtp.Subject = "Test"
oSmtp.BodyFormat = 1
If oSmtp.ImportMailEx( target ) <> 0 Then
	WScript.Echo "import mail failed" 
	WScript.Quit()
End If

If oSmtp.SendMail() <> 0 Then
	WScript.Echo oSmtp.GetLastErrDescription()
Else
	WScript.Echo "Message delivered"
End If

Note*: if you use Word Object in ASP/ASP.NET application, there are some issues with it, more detail, please refer to http://support.microsoft.com/kb/257757.

See Also

Using EASendMail ActiveX Object
Work with EASendMail Service(Mail Queuing)
How to use DomainKeys Signature
Send email without SMTP server(DNS lookup)
Error with sending recipient(Relay denied)
Mail vs. FastSender
Programming with Asynchronous Mode
Digital Signature and Email Encryption
Programming with FastSender
EASendMail ActiveX Object References
EASendMail SMTP Component Samples