Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending HTML mail using CDO

Status
Not open for further replies.

MrGoodbyte

Programmer
Oct 12, 2000
12
ZA
Hi There
I am using the following code to send an e-mail in VB. It works 100% except that I need to send embedded HTML. If I send embedded HTML then it actually shows the tags. I saw some sample code to send HTML using CDONTS but I cannot get CDONTS to work at all on my pc.

Thanks in advance for any help.

Code:
Sub MailDetail(Subject As String, Message As String)
'----------------------------------------------------------------------
' Name:            MailDetail
'
' Description: This procedure sends mail
'
' Inputs:          The Subject and Body of the email message are passed
'              to this procedure as parameters.
'----------------------------------------------------------------------
   On Error GoTo ErrorHandler
       Dim objSession As Object
       Dim objMessage As Object
       Dim objRecipient As Object
       Dim objRecipients As Collection

       'Create the Session Object
       Set objSession = CreateObject("mapi.session")

       'Logon using the session object
       'Specify a valid profile name if you want to
       'Avoid the logon dialog box
       objSession.Logon profileName:="MS Exchange Settings"

       'Add a new message object to the OutBox
       Set objMessage = objSession.Outbox.Messages.Add

       'Set the properties of the message object
       objMessage.Subject = Subject
       objMessage.Text = Message 

       'Add a recipient object to the objMessage.Recipients collection
       Set objRecipient = objMessage.Recipients.Add
       
       'Set the properties of the recipient object
       objRecipient.Name = &quot;russellj@aforbes.co.za&quot;  '<---Replace this with a valid
                                       'display name or e-mail alias
       objRecipient.Type = mapiTo
       objRecipient.Resolve
       
       'Send the message
       objMessage.Send showDialog:=False
       'MsgBox &quot;Message sent successfully!&quot;

       'Logoff using the session object
       objSession.Logoff

End Sub
 
I wrote an application last week to email reports, and I used CDO for NT. However, I found out the one big caveat is that it can only run on NT Server. The big advange is that it doesn't require an email server to work with, like MAPI. It can run stand alone and with very little code.

Here is the code I wrote:

Dim cdoMail As New CDONTS.NewMail

cdoMail.Subject = &quot;Auto report created &quot; & Now()
cdoMail.Body = &quot;This report was sent automatically.&quot;
cdoMail.To = <one or more email addresses>
cdoMail.From = <email address>
cdoMail.AttachFile strAttachmentFile
cdoMail.Send

Set cdoMail = Nothing

The strAttachmentFile is a full path to a file to attach. In my case, it may be a Word document, an Excel spreadsheet or an HTML file. As long as you are running on a server, this is the way to go. You can even shorten it more because the four line above the Send command (From, To, Subject, and Body) can be added as optional parameters to the Send command. So that routine can be four line long!

Hope this helps.

Steve
 
Thanks for the reply. We have figured out that the reason why I couldn't get CDONTS working is because the SMTP server is not set up correctly. I have no idea how to set this up but it keeps placing the mails into the badmail directory. I am running this whole thing on an NT server and our mail backbone is MS Exchange. The SMTP ports on the exchange server have been disabled (according to the administrator) and we are not allowed to use them. I pulled this mail out of the BadMail directory:

Code:
X-Sender: Procurement Test User
X-Receiver: nicholsc@aforbes.co.za
X-Receiver: service@procurement.net
From: <Procurement Test User>
To: <nicholsc@aforbes.co.za>
Cc: <service@procurement.net>
Subject: This is a test
Date: Thu, 26 Oct 2000 18:13:21 +0200
MIME-Version: 1.0
Content-Type: text/plain;
	charset=&quot;iso-8859-1&quot;
Content-Transfer-Encoding: quoted-printable
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3612.1700

As Above - <IMG WIDTH=3D468 HEIGHT=3D60 BORDER=3D0 =
SRC=3D&quot;[URL unfurl="true"]http://alfie/navigation/bannerman/banner.asp?a=3Dbanner&n=3D1&c=3D=[/URL]
1&quot;></A>

nicholsc is the e-mail address that I am trying to send to and the address in the cc box is the domain I have set up in SMTP (procurement.net). We have even tried it with an e-mail address in the from box. The code we used to send the e-mail is not much different from what you listed. Obvious differences are that we named our objects different names and we set the object to nothing after sending.
If I can get CDONTS working then I have some sample code to send embedded HTML messages with it.
For bigfoot who wants info about Collaboration Data Objects(CDO), it lets you send mail from an application without making a connection to a mail program like Outlook. It should be installed with Outlook or if you want CDONTS you can install it with IIS. Try CDOLIVE.com for more info about it.
 
Hi There, Thanks for the posts so far. We have sorted the problem with CDO NTS now. The problem was that two days ago the server that we had to use for a SMART Host, was disabled. As soon as the operations guys gave us the IP address for the new server, the mail started working perfectly.
Thanks again, Cheers.
 
MrGoodbyte am stuck on a similar problem. Have no problem sending email but as soon as I try to send it in HTML, it will come up with all the data that I was attempting to place in the source in the actual body.
If you would'nt mind could you send me your code for this one or post how to do it?
 
If you are sending HTML data in the body of an email message you probably need to set the content-type to text/html somehow.

Chaz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top