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_d ata (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 <EM>"
emsg = emsg & "<U>"& rsMain("StartDate") & "</U></EM> at <U>" & rsMain("StartTime") & "</U>. <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> </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 & Development</FONT></P></FO NT></DIV>< br>"
emsgEnd = ""
dim strTo, strCc, strBcc, strFrom, strSubject, strMessage, strAttachment
strTo = "jjharris@jajd.com"
strCc = ""
strBcc = ""
strFrom = "CorporateTraining@jajd.co m"
strSubject = "My Stuff - Employee Training Signup"
strMessage = emsg
strAttachment = ""
rsMain.Close
Set rsMain = Nothing
%>
<!--#Include virtual = "/www/downloads/jjh/Utils/
<%
'-------------------------
'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
rsMain.ActiveConnection = strConnectFtnWeb
rsMain.Source = "SELECT * FROM vw_training_leader_email_d
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 <EM>"
emsg = emsg & "<U>"& rsMain("StartDate") & "</U></EM> at <U>" & rsMain("StartTime") & "</U>. <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"
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> </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 & Development</FONT></P></FO
emsgEnd = ""
dim strTo, strCc, strBcc, strFrom, strSubject, strMessage, strAttachment
strTo = "jjharris@jajd.com"
strCc = ""
strBcc = ""
strFrom = "CorporateTraining@jajd.co
strSubject = "My Stuff - Employee Training Signup"
strMessage = emsg
strAttachment = ""
rsMain.Close
Set rsMain = Nothing
%>
What error?
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("CDONT S.Newmail" )
mail.MailFormat = 0
mail.BodyFormat = 0
<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("CDONT
mail.MailFormat = 0
mail.BodyFormat = 0
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Dana here is my sendEmail.asp page please give me some insite.
<!--METADATA TYPE="typelib" UUID="CD000000-8B95-11D1-8 2DB-00C04F B1625D" NAME="CDO for Windows Library" -->
<!--#include virtual = "/www/downloads/jjh/secure /SmtpServe r.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.Configur ation")
Set Flds = iConf.Fields
With Flds
.Item(cdoSendUsingMethod) = 2 ' cdoSendUsingPort
.Item(cdoSMTPServer) = strSmtpServer ' Note: value defined in jjh/secure/SmtpServer.asp
.Item(cdoSMTPConnectionTim eout) = 20 ' quick timeout
.Item(cdoURLGetLatestVersi on) = 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
%>
<!--METADATA TYPE="typelib" UUID="CD000000-8B95-11D1-8
<!--#include virtual = "/www/downloads/jjh/secure
<%
'*************************
' 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.Configur
Set Flds = iConf.Fields
With Flds
.Item(cdoSendUsingMethod) = 2 ' cdoSendUsingPort
.Item(cdoSMTPServer) = strSmtpServer ' Note: value defined in jjh/secure/SmtpServer.asp
.Item(cdoSMTPConnectionTim
.Item(cdoURLGetLatestVersi
.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.CreateObj ect("CDO.C onfigurati on")
cdoConfig.Fields.Item(theS chema + "sendusing")= 1
cdoConfig.Fields.Item(theS chema + "smtpserverport") = 25
cdoConfig.Fields.Item(theS chema + "smtpconnectiontimeout") = 10
cdoConfig.Fields.Update
Dim strName, strEmail, strQuery
strName = Trim(Request.Form("ClientN ame"))
strEmail = Trim(Request.Form("ClientE mail"))
strQuery = Trim(Request.Form("ClientQ ry"))
strFrom = Trim(Request.Form("From"))
If (strName <> "" And strEmail <> "" And strQuery <> "") Then
Set Mailer = Server.CreateObject("CDO.M essage")
Mailer.To = strEmail
Mailer.From = strFrom
Mailer.Subject = "Your Inquiry " & strName
Mailer.HTMLBody = strQuery
Mailer.Send
Response.Write "thanks Man!"
Set Mailer = Nothing
%>
<%
theSchema="http://schemas.microsoft.com/cdo/configuration/"
Set cdoConfig=server.CreateObj
cdoConfig.Fields.Item(theS
cdoConfig.Fields.Item(theS
cdoConfig.Fields.Item(theS
cdoConfig.Fields.Update
Dim strName, strEmail, strQuery
strName = Trim(Request.Form("ClientN
strEmail = Trim(Request.Form("ClientE
strQuery = Trim(Request.Form("ClientQ
strFrom = Trim(Request.Form("From"))
If (strName <> "" And strEmail <> "" And strQuery <> "") Then
Set Mailer = Server.CreateObject("CDO.M
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_d ata (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_d ata (nolock) WHERE EEID = " & strEEID & " and classID = " & strclassId & " ; "
EEID ==> number data type
classID ==> number data type...
HTH...
HAppy programming...
rsMain.Source = "SELECT * FROM vw_training_leader_email_d
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_d
EEID ==> number data type
classID ==> number data type...
HTH...
HAppy programming...
ASKER
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,
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.