http://www.chrispearson.org/pages/programming/vbscript/Email/newmail.asp
09h38
Wednesday, 8. October 2008

NEWMAIL OBJECT

Summary

When Microsoft Windows 2000 or NT4 with NT Option Pack is installed, the default setup includes Simple Mail Transfer Protocol (SMTP). Among other purposes, this can be used to provide email functions for web sites which use Active Server Pages (ASP).

CDONTS - Collaborative Data Objects for NT Server - is a library which, as Microsoft puts it, "exposes messaging objects for use by Microsoft® Visual Basic® [and other programming languages]". Although CDONTS provides a number of objects with their properties and their methods, the CDONTS NewMail object is all that is required to email-enable ASP web pages. And using NewMail can be extremely easy.

This page covers the NewMail object's properties and the methods that can be used to support HTML forms to create email.

A couple of points of background may help in placing NewMail into your thinking about a practical application.

The CDONTS library allows server applications to send and receive email without interacting with Microsoft Exchange Server or even needing access to Exchange. It communicates directly with the SMTP server available in IIS. Note, also, that the SMTP Server in IIS has its own message store in which the Inbox and Outbox are mapped to the file system. No other folders exist or are allowed to exist. This is different to CDONTS when used on an Exchange Server where each messaging user has their own Inbox.

Applications created using CDONTS are interchangable between IIS and Exchange environments except where the NewMail object is used. So, what we are discussing below is applicable to web servers running IIS.

CDONTS is designed to be used in a server environment: There is no user interface.

ASP email: Main article ASP Email: Main article
  Back to the top of this page

Properties

This table shows all the properties of the NewMail object -Below the table follows more detail on the six key properties, To, From, Subject, Body, BodyFormat and MailFormat which, together with Send are what you need to use to send a message.

In the descriptions below the code examples should be preceded by code which sets up the NewMail object, such as

<%@LANGUAGE="VBSCRIPT" %>
. . .
<%
Set objNewMail = ServerCreateObject("CDONTS.NewMail")
. . .


Property
Available in version
Type

Access
Bcc 1.2 String Write-only
Body 1.2 IStream object or String Write-only
BodyFormat 1.2 Long Write-only
Cc 1.2 String Write-only
ContentBase 1.2 String Write-only
ContentLocation 1.2 String Write-only
From 1.2 String Write-only
Importance 1.2 Long Write-only
MailFormat 1.2 Long Write-only
Subject 1.2 String Write-only
To 1.2 String Write-only
Value 1.2 String Write-only
Version 1.2 String Read-only

To

The value assigned to the To property is one or more recipient addresses. Each should be a full messaging address such as

angus.mcnutter@spamail.com

If there is more than a single recipient each messaging address must be seperated by a semicolon, as:

angus.mcnutter@spamail.com;herbet.mcalpine@alpmail.com

It's a straightforward string, so coda can assign the above recipients using:

Dim strRecipients
. . .
strRecipients = _
     "angus.mcnutter@spamail.com;herbet.mcalpine@alpmail.com"
. . .
objNewMail.To = strRecipinets

From

The From property adds the sender's messaging address to the email. It's another string and can contain only one email address and cannot contain a semicolon.

Dim strSender
. . .
strSender = "me@myserver.org"
. . .
objNewMail.From = strSender

Subject

The Subject property is the text that appears in the subject line of the message. Again, this is a simple string and can be set to null (zero length string; blank, that is, but bear in mind recipients may not appreciate having to open an email just to see what it's about) It can be set to a very long string but most users will only be ale to see the first part of a long string.

Dim strSubject
. . .
strSubject = "An email sent from a server to a person!"
. . .
objNewMail.Subject = strSubject

Body

The body property allows you to add text to the main part of the email: It's body. C-languages and Java can use the IStream but, in VBScript, this is a string property.

The message's body can contain a string of either plain text or HTML. The BodyFormat property must be set to one for plain text or reset to zero to allow HTML content. See BodyFormat, below. The text can be formatted with newline characters.

Dim strBody
. . .
strBody = "Hello, my friends" & vbCR & _
     "Lots of news to send to you."
. . .
strBody = strBody & vbCR & _
     "But I'll pass that on another time."
. . .
objNewMail.Body = strBody

MailFormat

MailFormat allows you to select MIME encoding or plain text by using a number, as

Dim lngMailFormat
. . .
lngMailFormat = 1     ' Set to uninterupted plain text format
. . .
objNewMail.MailFormat = lngMailFormat

Using a provided constant, the variable could be set up for MIME encoding by using the code

objMailFormat = CdoMailFormatMime

This property determines how the Attach and AttachURL methods work and this property is automatically set to zero (The value of CdoMailFormatMime) without any code to do this. Only set this to 1 if you are working in plain text.

BodyFormat

If BodyFormat is set to 1 it indicates that the body of the message is exclusively plain text. A value of zero indicates text which conatins - or may contain - HTML markup.

The options here are, plain text only:

Dim strBody
Dim lngBodyFormat
. . .
lngBodyFormat = 1     ' Plain text format
. . .
strBody = "This is a text message"
. . .
objNewMail.Body = strBody

And for HTML encoded content:

Dim strBody
Dim lngBodyFormat
. . .
lngBodyFormat = 0     ' HTML format
. . .
strBody = "<!DOCTYPE HTML PUBLIC ""-//IETF//DTD HTML//EN"">"
strBody = strBody & vbCrLf & "<html>"
strBody = strBody & "<head>"
strBody = strBody & "<meta http-equiv=""Content-Type"""
strBody = strBody & ""content=""text/html; charset=iso-8859-1"">""
strBody = strBody & "<title>Example of NewMail in HTML </title>"
strBody = strBody & "</head>"
strBody = strBody & "<body>"
strBody = strBody & "This is a HTML message<BR></body>"
strBody = strBody & "</html>"
. . .
objNewMail.Body = strBody

 

Methods


Method
Available in version
Parameters
AttachFile 1.2 Source as Object or String,
(optional) FileName as String,
(optional) EncodingMethod as Long
AttachURL 1.2 Source as Object or String,
ContentLocation as String,
(optional) ContentBase as String,
(optional) EncodingMethod as Long
Send 1.2 (optional) From as String,
(optional) To as String,
(optional) Subject as String,
(optional) Body as Object or String,
(optional) Importance as Long
SetLocaleIDs 1.2 CodePageID as Long

Send

The Send method brings together the properties described above to send a message through the SMTP server.

As shown in the table above, most of the proprties are optional but a number are (quite logically!) necessary to properly send an email. Once all the proprties have been assigned values the message can be sent using simply

objNewMail.Send

 

And finally . . .

After all the processing is completed you should, properly, wrap up with

set objNewMail = Nothing
. . .
%>

And further . . .

There is masses on the Microsoft web site to fill in the gaps and further inform: at http://www.microsoft.com search for "CDONTS NewMail" or start out at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdo/html/_denali_newmail_object_cdonts_library_.asp

ASP Email - main article

xxx,xxx

copyright ©2000 - 2008 Chris Pearson