Link to home
Start Free TrialLog in
Avatar of MrJarvis2004
MrJarvis2004

asked on

How to send an HTML-ASP email with CDO

I am trying to send an HTML email through an asp page but keep getting an error. The rro occurs in the my variable where I try to create the message body which is HTML. The code is below. Please help


<!--#Include virtual = "/www/downloads/jjh/Utils/SendEmail.asp" -->


<%
  '-------------------------------------------------------------------------------
  'Declare local variables that will be used on this screen
  '-------------------------------------------------------------------------------
  DIM rsMain
  DIM emsg, emsgEnd
  DIM mail, strEmailMsg
 
 

  '-------------------------------------------------------------------------------
  ' Create and Open a recordset to the SQL database tables  
  '-------------------------------------------------------------------------------
Set rsMain = Server.CreateObject("ADODB.Recordset")

rsMain.ActiveConnection = strConnectFtnWeb
rsMain.Source = "SELECT *  FROM vw_training_leader_email_data (nolock) WHERE EEID = '" &  strEEID  & "' and classID = '" & strclassId & "' ; "
rsMain.CursorType = 0
rsMain.CursorLocation = 2
rsMain.LockType = 1
rsMain.Open()

'ERROR STARTS HERE

emsg = ""
emsg = emsg & "<DIV><FONT size=2><FONT size=3><FONT face=Arial><U>"& rsMain("EmployeeName") & " </U></FONT>"  
emsg = emsg & "has enrolled in the <U>"& rsMain("CourseName") & "</U> class that will be held on &nbsp;<EM>"
emsg = emsg & "<U>"& rsMain("StartDate") & "</U></EM> at <U>" & rsMain("StartTime") & "</U>.&nbsp; <o:p></o:p></FONT>"
emsg = emsg & "<P><FONT size=3><U>" & rsMain("Coursename") & "</U>is:</FONT></P>"
emsg = emsg & "<P><FONT size=3>" & rsMain("CourseDescription") & "</FONT></P>"
emsg = emsg & "<FONT size=3>You do not need to take any further action, if this meets your approval.</FONT>"
emsg = emsg & "<FONT size=3>&nbsp;</FONT>"
emsg = emsg & "<FONT size=3>However, if you would like this enrollment canceled or another date/time chosen, please click here to view the Frequently Asked Questions on the Training Web site. </FONT>"
emsg = emsg & "<FONT size=3> <br><br>Thanks, </FONT>"
emsg = emsg & "<FONT size=3>Corporate Training &amp; Development</FONT></P></FONT></DIV><br>"

emsgEnd = ""

 
 
dim strTo, strCc, strBcc, strFrom, strSubject, strMessage, strAttachment
 

 
  strTo         = "jjharris@jajd.com"
  strCc         = ""
  strBcc        = ""
  strFrom       = "CorporateTraining@jajd.com"
  strSubject    = "My Stuff - Employee Training Signup"
  strMessage    = emsg


  strAttachment = "" 

  rsMain.Close
  Set rsMain = Nothing

  %>

Avatar of Steve Bink
Steve Bink
Flag of United States of America image

What error?
Avatar of danataylor
danataylor

Might try starting the emsg with
<HEAD></HEAD><HTML><BODY>

and ending it with
</BODY></HTML>

Also, are you setting the mail format to HTML in the SendEmail.asp include file?
dim mail
set mail = server.CreateObject("CDONTS.Newmail")
mail.MailFormat = 0
mail.BodyFormat = 0
SOLUTION
Avatar of danataylor
danataylor

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MrJarvis2004

ASKER

Dana here is my sendEmail.asp page please give me some insite.

<!--METADATA TYPE="typelib" UUID="CD000000-8B95-11D1-82DB-00C04FB1625D" NAME="CDO for Windows Library" -->


<!--#include virtual = "/www/downloads/jjh/secure/SmtpServer.asp"-->



<%
'****************************************************************************************************************
' Define Global Variables
' Note:  These variable will be available to any web pages that call the functions inside of this include file
'****************************************************************************************************************

Dim fSendMsgAsText
Dim fSendMsgAsHtml

fSendMsgAsText = 1
fSendMsgAsHTML = 2

function SendEmailMsg(strTo, strCc, strBcc, strFrom, strSubject, strMessage, fMessageType, strAttachment)


Dim iMsg
Dim iConf
Dim Flds
Dim strResults(1)


On Error Resume Next


Set iMsg  = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Set Flds  = iConf.Fields


With Flds
   .Item(cdoSendUsingMethod)       = 2                     ' cdoSendUsingPort
   .Item(cdoSMTPServer)            = strSmtpServer         ' Note: value defined in jjh/secure/SmtpServer.asp
   .Item(cdoSMTPConnectionTimeout) = 20                    ' quick timeout
   .Item(cdoURLGetLatestVersion)   = True
   .Update
End With


err.Clear()


With iMsg
    Set .Configuration = iConf

     .To        = strTo
     .Cc        = strCc
     .Bcc        = strBcc
     .From        = strFrom
     .Sender   = strFrom
     .Subject  = strSubject
     
     if fMessageType = fSendMsgAsText then
          .TextBody = strMessage
     else
                .HTMLBody = strMessage
     end if

     if strAttachment <> "" then
                .AddAttachment strAttachment
     end if

           .Send
End With


If err.number = 0 Then
     strResults(0) = 0
     strResults(1) = "Email Sent Successfully"
Else
     strResults(0) = err.number
     strResults(1) = err.description
End IF


Set iMsg  = Nothing
Set iConf = Nothing


SendEmailMsg = strResults


end function
%>
Here's a snipppet from a mailer I made


<%
theSchema="http://schemas.microsoft.com/cdo/configuration/"
Set cdoConfig=server.CreateObject("CDO.Configuration")
cdoConfig.Fields.Item(theSchema + "sendusing")= 1
cdoConfig.Fields.Item(theSchema + "smtpserverport") = 25
cdoConfig.Fields.Item(theSchema + "smtpconnectiontimeout") = 10
cdoConfig.Fields.Update

Dim strName, strEmail, strQuery
strName = Trim(Request.Form("ClientName"))
strEmail = Trim(Request.Form("ClientEmail"))
strQuery = Trim(Request.Form("ClientQry"))
strFrom = Trim(Request.Form("From"))

If (strName <> "" And strEmail <> "" And strQuery <> "") Then

Set Mailer = Server.CreateObject("CDO.Message")
Mailer.To = strEmail
Mailer.From = strFrom
Mailer.Subject = "Your Inquiry " & strName
Mailer.HTMLBody = strQuery
Mailer.Send

Response.Write "thanks Man!"

Set Mailer = Nothing
%>
I am not sure but this is just a guess snce you do not post the exact error messag ethat you are getting... Is the sql statement below work properly or does it return a data...

rsMain.Source = "SELECT *  FROM vw_training_leader_email_data (nolock) WHERE EEID = '" &  strEEID  & "' and classID = '" & strclassId & "' ; "

Now if the field datatype of both fields is a number data type then you must bound it with a single quote... You can try this...

rsMain.Source = "SELECT *  FROM vw_training_leader_email_data (nolock) WHERE EEID = " &  strEEID  & " and classID = " & strclassId & " ; "

EEID ==>  number data type
classID ==> number data type...


HTH...

HAppy programming...
Okay, I am almost complete can someone tell me how to get the database fields to display in the email.  Currently this is how the email looks when mailed.

From: Total@jajd.com [mailto:Total@jajd.com]
Sent: Monday, April 11, 2005 10:48 AM
To: J
Subject: My Stuff - Employee Training Signup


"& rsMain("EmployeeName") &"has enrolled in the & rsMain("CourseName") & class that will be held on  "& rsMain("StartDate") &" at " & rsMain("StartTime") & ".  
"& rsMain("Coursename") &"is:

"& rsMain("CourseDescription") & "

You do not need to take any further action, if this meets your approval. However, if you would like this enrollment canceled or another date/time chosen, please click here to view the Frequently Asked Questions on the Training Web site.

Thanks,
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial