By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,659 Members | 1,611 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,659 IT Pros & Developers. It's quick & easy.

How do I get my message body emailed to me using CDO?

P: 7
Here is the code that I have: The email is being sent and received however the message body is not. Please help!
Expand|Select|Wrap|Line Numbers
  1. <%
  2. dim sql, rs, rs1, reportID, mail, body
  3. dim jobSite, jobSiteNum, reportDate, supervisor, weather, temperature, comment, problem
  4. dim emailTo, emailToArr, emailSubject, temp, header
  5. set conn = Server.CreateObject("ADODB.Connection")
  6. conn.Open connString
  7. set rs = Server.CreateObject("ADODB.Recordset")
  8. set rs1 = Server.CreateObject("ADODB.Recordset")
  9. reportID = Request.Form("reportID")
  10. if reportID <> "" Then
  11. sql = "SELECT * FROM TblReport r, TblJobSite js, TblUser u " & _
  12. "WHERE r.JobSiteID = js.JobSiteID AND r.SupervisorID = u.UserID " & _
  13. "AND ReportID = " & reportID
  14. rs.Open sql, conn
  15. emailTo = Request.Form("txtEmailTo")
  16. emailToArr = Split(emailTo,";")
  17. emailSubject = Request.Form("txtEmailSubject")    
  18. set mail = Server.CreateObject("CDO.Message")
  19. mail.From = getSetting("pEmailFrom")
  20. mail.To = emailTo
  21. mail.Subject = emailSubject
  22. body = "<html><head><title>Report</title>"
  23. body = body & "<style type='text/css'>body, td { font-family:Verdana; font-size:11px; } </style>"
  24. body = body & "</head><body>"
...

Here is another example of code that I wrote that was unsuccessful:
Expand|Select|Wrap|Line Numbers
  1. <%
  2.     dim sql, rs, rs1, reportID, mail, body
  3.     dim jobSite, jobSiteNum, reportDate, supervisor, weather, temperature, comment, problem
  4.     dim emailTo, emailToArr, emailSubject, temp, header
  5.  
  6.     set conn = Server.CreateObject("ADODB.Connection")
  7.     conn.Open connString
  8.     set rs = Server.CreateObject("ADODB.Recordset")
  9.     set rs1 = Server.CreateObject("ADODB.Recordset")
  10.     reportID = Request.Form("reportID")
  11.     if reportID <> "" Then
  12.         sql = "SELECT * FROM TblReport r, TblJobSite js, TblUser u " & _
  13.               "WHERE r.JobSiteID = js.JobSiteID AND r.SupervisorID = u.UserID " & _
  14.               "AND ReportID = " & reportID
  15.         rs.Open sql, conn
  16.         if NOT rs.EOF Then
  17.             jobSite = rs.Fields("JobSiteName")
  18.             jobSiteNum = rs.Fields("JobSiteNum")
  19.             reportDate = rs.Fields("ReportDate")
  20.             supervisor = rs.Fields("FirstName") & " " & rs.Fields("LastName")
  21.             if not isnull(rs.Fields("WeatherID")) Then
  22.                 sql = "SELECT * FROM TblWeather WHERE WeatherID = " & rs.Fields("WeatherID")
  23.                 rs1.Open sql, conn
  24.                 if not rs1.EOF Then
  25.                     weather = rs1.Fields("WeatherName")
  26.                 end If
  27.                 rs1.Close
  28.             end If
  29.             temperature = rs.Fields("Temperature")
  30.             comment = rs.Fields("Comment")
  31.             problem = rs.Fields("Problem")            
  32.         end if        
  33.  
  34.         rs.Close
  35.  
  36.         emailTo = Request.Form("txtEmailTo")
  37.         emailToArr = Split(emailTo,";")
  38.  
  39.         'set mail = Server.CreateObject("Persits.MailSender")
  40. set objMessage = createobject("cdo.message") 
  41. set objConfig = createobject("cdo.configuration") 
  42. Set Flds = objConfig.Fields 
  43. Flds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
  44. Flds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="mail.mydomain.com" 
  45. ' ' Passing SMTP authentication 
  46. Flds.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'basic (clear-text) authentication 
  47. Flds.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="postmaster@mydomain.com" 
  48. Flds.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="MyPassword"
  49. Flds.update 
  50. Set objMessage.Configuration = objConfig 
  51. Set Flds = objConfig.Fields 
  52. objMessage.To = emailTo
  53. objMessage.From = getSetting("pEmailFrom") 
  54. objMessage.Subject = Request.Form("txtEmailSubject") 
  55. objMessage.fields.update 
  56. objMessage.HTMLBody = body
  57.  
  58.         body = body & "<html><head><title>Report</title>"
  59.         body = body & "<style type='text/css'>body, td { font-family:Verdana; font-size:11px; } </style>"
  60.         body = body & "</head><body>"
  61. ...
  62.  
  63.         body = body & "</body></html>"
  64.  
  65.         'mail.Body = body
  66.         'mail.AddEmbeddedImage Server.MapPath("/Images/Calendar Email Logo.jpg"), "Logo" ' Embed logo in email body
  67.         'mail.IsHTML = true
  68.  
  69.         'Attach pictures to email
  70.         dim path
  71.         path = Server.MapPath("Pictures")
  72.  
  73.         sql = "SELECT * FROM TblReportPicture WHERE ReportID = " & reportID
  74.         rs.Open sql, conn
  75.         if NOT rs.EOF then
  76.             do until rs.EOF                 
  77.                 mail.AddAttachment path & "/" & rs.Fields("Filename")
  78.                 rs.MoveNext
  79.             loop
  80.         end if
  81.         rs.Close
  82.  
  83.         On Error Resume Next
  84.         objMessage.Send 
  85.         set mail = nothing
Apr 7 '11 #1
Share this Question
Share on Google+
13 Replies


Rabbit
Expert Mod 10K+
P: 12,370
Are you sure your web server has access to the e-mail servers?
Apr 7 '11 #2

P: 7
They are both within the same hosting company so I would assume so.
Apr 7 '11 #3

Rabbit
Expert Mod 10K+
P: 12,370
That doesn't mean the proxy account has access to it. You should ask them if it does, and if so, what server settings to use.
Apr 7 '11 #4

P: 7
Thx. I will get back to you as soon as I get a response from their Tech.
Apr 7 '11 #5

P: 7
Here is the message that was given to me:
"Yes. The web server has access to the email server. Remember if you are able to manually type the body, instead of passing the variable through, and the email comes through means that the application is able to connect to the mail server and relay it out. The problem is the form page, passing the body from the form page to the actual CDO page to set out the email. That's where it seems to be breaking down."
Would it help if I posted the code for the prior page?
Apr 7 '11 #6

jhardman
Expert 2.5K+
P: 3,405
You need to populate the body variable (body = "...") Before you assign it to the email body (objmessage.htmlbody = body). Asp doesn't usually use references, instead it passes current content. Does this make sense?

Jared
Apr 8 '11 #7

P: 7
No I'm sorry it does not.
How then would I build an html coded email that I can send?

This is the full code that I have for the page:
Expand|Select|Wrap|Line Numbers
  1. <%
  2.     dim sql, rs, rs1, reportID, mail, body
  3.     dim jobSite, jobSiteNum, reportDate, supervisor, weather, temperature, comment, problem
  4.     dim emailTo, emailToArr, emailSubject, temp, header
  5.  
  6.     set conn = Server.CreateObject("ADODB.Connection")
  7.     conn.Open connString
  8.     set rs = Server.CreateObject("ADODB.Recordset")
  9.     set rs1 = Server.CreateObject("ADODB.Recordset")
  10.     reportID = Request.Form("reportID")
  11.     if reportID <> "" Then
  12. sql = "SELECT * FROM TblReport r, TblJobSite js, TblUser u " & _
  13. "WHERE r.JobSiteID = js.JobSiteID AND r.SupervisorID = u.UserID " & _
  14. "AND ReportID = " & reportID
  15. rs.Open sql, conn
  16. if NOT rs.EOF Then
  17. jobSite = rs.Fields("JobSiteName")
  18. jobSiteNum = rs.Fields("JobSiteNum")
  19. reportDate = rs.Fields("ReportDate")
  20. supervisor = rs.Fields("FirstName") & " " & rs.Fields("LastName")
  21. if not isnull(rs.Fields("WeatherID")) Then
  22. sql = "SELECT * FROM TblWeather WHERE WeatherID = " & rs.Fields("WeatherID")
  23. rs1.Open sql, conn
  24. if not rs1.EOF Then
  25. weather = rs1.Fields("WeatherName")
  26. end If
  27. rs1.Close
  28. end If
  29. temperature = rs.Fields("Temperature")
  30. comment = rs.Fields("Comment")
  31. problem = rs.Fields("Problem") 
  32. end if 
  33.  
  34. rs.Close
  35.  
  36. emailTo = Request.Form("txtEmailTo")
  37. emailToArr = Split(emailTo,";")
  38.  
  39. set objMessage = createobject("cdo.message") 
  40. set objConfig = createobject("cdo.configuration") 
  41. Set Flds = objConfig.Fields 
  42. Flds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
  43. Flds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="mail.mydomain.com" 
  44.  
  45. ' ' Passing SMTP authentication 
  46. Flds.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'basic (clear-text) authentication 
  47. Flds.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="postmaster@mydomain.com" 
  48. Flds.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="XXXXX"
  49. Flds.update 
  50.  
  51. Set objMessage.Configuration = objConfig 
  52. Set Flds = objConfig.Fields 
  53. objMessage.To = emailTo
  54. objMessage.From = getSetting("pEmailFrom") 
  55. objMessage.Subject = Request.Form("txtEmailSubject") 
  56. objMessage.fields.update 
  57. objMessage.HTMLBody = body
  58.  
  59.         body = body & "<html><head><title>Report</title>"
  60.         body = body & "<style type='text/css'>body, td { font-family:Verdana; font-size:11px; } </style>"
  61.         body = body & "</head><body>"        
  62.         body = body & "<table border='0' cellspacing='0' cellpadding='3'>"
  63.  
  64.         'Report header
  65.         header = getSetting("pCompanyAddress") & ", " & getSetting("pCompanyCity") & ", " & getSetting("pCompanyProvince") & "  " & getSetting("pCompanyPostalCode") & _
  66.                 "<br>Phone: " & getSetting("pCompanyPhone") & "&nbsp;&nbsp;Fax: " & getSetting("pCompanyFax") & "<br><br>" & _
  67.                 "<h4 align='center'>Project Daily Report</h4>"        
  68.         body = body & "<tr><td colspan='2' align='center'><img src='http://www.mydomain.com/Images/Calendar Email Logo.jpg' border='0' /><br>" & header & "</td></tr>"
  69.  
  70.         body = body & "<tr><td width='120'><b>Report #:</b></td><td><b>" & reportID & "</b></td></tr>"
  71.         body = body & "<tr><td><b>Job Site:</b></td><td><b>" & jobSite & "</b></td></tr>"
  72.         body = body & "<tr><td><b>Job Site #:</b></td><td><b>" & jobSiteNum & "</b></td></tr>"
  73.         body = body & "<tr><td><b>Date:</b></td><td><b>" & FormatDateTime(reportDate,1) & "</b></td></tr>"
  74.         body = body & "<tr><td><b>Site Supervisor:</b></td><td><b>" & supervisor & "</b></td></tr>"
  75.         body = body & "<tr><td><b>Weather:</b></td><td>" & weather & "</td></tr>"
  76.         body = body & "<tr><td><b>Temperature:</b></td><td>" & temperature & "</td></tr></table>"
  77.  
  78.         'Trades on site
  79.         Dim i
  80.         sql = "SELECT * FROM TblTrade t, TblReportTrade rt " & _
  81.               "WHERE t.TradeID = rt.TradeID AND rt.ReportID = " & reportID & _
  82.               " ORDER BY TradeName"
  83.         rs.Open sql, conn
  84.         if NOT rs.EOF Then
  85.             i = 0
  86.             body = body & "<table border='0' cellspacing='0' cellpadding='3'>"
  87.             body = body & "<tr><td><b>Trades on site:</b></td><td>Number of Men</td><td>&nbsp;</td><td><b>Trades on site:</b></td><td>Number of Men</td></tr>"
  88.             do until rs.EOF 
  89.                 If(i Mod 2 = 0) Then
  90.                     body = body & "<tr><td>" & rs.Fields("TradeName") & "</td><td>"    & rs.Fields("MenNumber") & "</td><td>&nbsp;</td>"
  91.                 Else
  92.                     body = body & "<td>" & rs.Fields("TradeName") & "</td><td>"    & rs.Fields("MenNumber") & "</td></tr>"
  93.                 End If
  94.                 i = i + 1
  95.                 rs.MoveNext
  96.             loop
  97.         end If    
  98.         If (i Mod 2 = 1) Then
  99.             body = body & "<td>&nbsp;</td><td>&nbsp;</td>></tr>"
  100.         End If
  101.             body = body & "</table>"
  102.         rs.Close    
  103.  
  104.         'Comments and Problems
  105.         body = body & "<table border='0' cellspacing='0' cellpadding='3'>"
  106.         body = body & "<tr><td valign='top'><b>Comments:</b></td><td>" & comment & "</td></tr>"
  107.         body = body & "<tr><td valign='top'><b>Problems:</b></td><td>" & problem & "</td></tr>"
  108.         body = body & "</table>"
  109.  
  110.         'Accidents
  111.         sql = "SELECT * FROM TblReportAccident WHERE ReportID = " & reportID
  112.         rs.Open sql, conn
  113.         If Not rs.EOF Then
  114.             body = body & "<table border='0' cellspacing='0' cellpadding='3'>"
  115.             body = body & "<tr><td width='120'><b>Accident:</b></td><td colspan='3'>" & rs.Fields("AccidentOn") & "</td></tr>"
  116.             body = body & "<tr><td>&nbsp;</td><td colspan='3'>" & rs.Fields("Description") & "</td></tr>"
  117.  
  118.             'Accident WCB
  119.             sql = "SELECT * FROM TblReportAccident ra, TblWCBDoc w WHERE ra.ReportAccidentID = w.ReportAccidentID AND ra.ReportID = " & reportID
  120.             rs1.Open sql, conn 
  121.                 body = body & "<tr><td>&nbsp;</td><td><b>WCB File Name</b></td><td><b>Date Uploaded</b></td><td>&nbsp;</td></tr>"
  122.             If Not rs1.EOF Then
  123.                 Do Until rs1.EOF
  124.                     body = body & "<tr><td>&nbsp;</td><td>" & rs1("FileName") & "</td><td>" & rs1("DateUploaded") & "</td><td><a href='http://www.mydomain.com/Report/WCB/" & rs1("FileName") & "' target='_blank'>View</a></td></tr>"
  125.                     rs1.MoveNext
  126.                 Loop
  127.             Else
  128.                     body = body & "<tr><td>&nbsp;</td><td>-</td><td>-</td><td>&nbsp;</td></tr>"
  129.             End If
  130.             rs1.Close
  131.  
  132.             sql = "SELECT * FROM TblReportAccident ra, TblWCBPicture w WHERE ra.ReportAccidentID = w.ReportAccidentID AND ra.ReportID = " & reportID
  133.             rs1.Open sql, conn 
  134.                 body = body & "<tr><td>&nbsp;</td><td><b>Picture File Name</b></td><td><b>Description</b></td><td>&nbsp;</td></tr>"
  135.             If Not rs1.EOF Then
  136.                 Do Until rs1.EOF
  137.                     body = body & "<tr><td>&nbsp;</td><td>" & rs1("FileName") & "</td><td>" & rs1("Description") & "</td><td><a href='http://www.mydomain.com/Report/WCB/" & rs1("FileName") & "' target='_blank'>View</a></td></tr>"
  138.                     rs1.MoveNext
  139.                 Loop
  140.             Else
  141.                 body = body & "<tr><td>&nbsp;</td><td>-</td><td>-</td><td>&nbsp;</td></tr>"
  142.             End If
  143.             rs1.Close
  144.             body = body & "</table>"
  145.  
  146.         End If
  147.         rs.Close
  148.  
  149.  
  150.         'Equipment
  151.         Dim j
  152.         sql = "SELECT * FROM TblEquipment e, TblReportEquipment re " & _
  153.               "WHERE e.EquipmentID = re.EquipmentID AND re.ReportID = " & reportID & _
  154.               " ORDER BY EquipmentName"
  155.         rs.Open sql, conn
  156.         if NOT rs.EOF Then
  157.             j = 0
  158.             body = body & "<table border='0' cellspacing='0' cellpadding='3'>"
  159.             body = body & "<tr><td><b>Equipment:</b></td><td>Number</td><td>&nbsp;</td><td><b>Equipment:</b></td><td>Number</td></tr>"
  160.             do until rs.EOF
  161.                 If(j Mod 2 = 0) Then
  162.                     body = body & "<tr><td>" & rs.Fields("EquipmentName") & "</td><td>"    & rs.Fields("Number") & "</td><td>&nbsp;</td>"
  163.                 Else
  164.                     body = body & "<td>" & rs.Fields("EquipmentName") & "</td><td>"    & rs.Fields("Number") & "</td></tr>"
  165.                 End If
  166.                 j = j + 1
  167.                 rs.MoveNext
  168.             loop
  169.         end if    
  170.         If (j Mod 2 = 1) Then
  171.             body = body & "<td>&nbsp;</td><td>&nbsp;</td>></tr>"
  172.         End If
  173.             body = body & "</table>"
  174.         rs.Close    
  175.  
  176.         'Site Personnel
  177.         sql = "SELECT * FROM TblUser u, TblReportUser ru " & _
  178.               "WHERE u.UserID = ru.UserID AND ru.ReportID = " & reportID & _
  179.               " ORDER BY FirstName, LastName"
  180.         rs.Open sql, conn
  181.         if NOT rs.EOF then
  182.             body = body & "<table border='0' cellspacing='0' cellpadding='3'>"
  183.             body = body & "<tr><td><b>Site Personnel:</b></td><td>Number of Hours</td></tr>"
  184.             do until rs.EOF 
  185.                 body = body & "<tr><td>" & rs.Fields("FirstName") & " " & rs.Fields("LastName") & "</td><td>"    & rs.Fields("Hours") & "</td></tr>"
  186.                 rs.MoveNext
  187.             Loop
  188.         end if    
  189.         rs.Close    
  190.  
  191.         'Vehicles on site
  192.         body = body & "</table>"
  193.  
  194.         sql = "SELECT * FROM TblReportVehicle WHERE ReportID = " & reportID & _
  195.               " ORDER BY LicenseNum"
  196.         rs.Open sql, conn
  197.         if NOT rs.EOF Then
  198.             body = body & "<table border='0' cellspacing='0' cellpadding='3'>"
  199.             body = body & "<tr><td colspan='4'><b>Company Vehicles on site:</b></td></tr>"
  200.             body = body & "<tr><td width='120'>&nbsp;</td><td>License No</td><td>Milage Start (km)</td><td>Milage Finish (km)</td></tr>"
  201.             do until rs.EOF 
  202.                 body = body & "<tr><td width='120'>&nbsp;</td><td>" & rs.Fields("LicenseNum") & "</td><td>"    & rs.Fields("MilageStart") & "</td><td>" & rs.Fields("MilageFinish") &"</td></tr>"
  203.                 rs.MoveNext
  204.             Loop
  205.             body = body & "</table>"
  206.         end if    
  207.         rs.Close    
  208.  
  209.         body = body & "</body></html>"
  210.  
  211. 'mail.Body = body
  212. 'mail.AddEmbeddedImage Server.MapPath("/Images/Calendar Email Logo.jpg"), "Logo" ' Embed logo in email body
  213. 'mail.IsHTML = true
  214.  
  215. 'Attach pictures to email
  216. dim path
  217. path = Server.MapPath("Pictures")
  218.  
  219. sql = "SELECT * FROM TblReportPicture WHERE ReportID = " & reportID
  220. rs.Open sql, conn
  221. if NOT rs.EOF then
  222. do until rs.EOF 
  223. mail.AddAttachment path & "/" & rs.Fields("Filename")
  224. rs.MoveNext
  225. loop
  226. end if
  227. rs.Close
  228.  
  229. On Error Resume Next
  230. objMessage.Send 
  231. set mail = nothing 
  232.  
  233.         if Err <> 0 then
  234.             Response.Write "We are sorry but an error has occurred when sending email.<br><br>"
  235.             Response.Write "Error description: " & Err.Description
  236.         else
  237.             'Add record to Report Email table
  238.             rs.Open "TblReportEmail", conn, adOpenKeyset, adLockOptimistic
  239.             rs.AddNew
  240.             rs.Fields("EmailTo") = emailTo
  241.             rs.Fields("Subject") = emailSubject
  242.             rs.Fields("EmailBy") = Session.Contents("UserFullName")            
  243.             rs.Fields("ReportID") = reportID
  244.             rs.Update
  245.             rs.Close
  246.  
  247.             Response.Write "Report # " & reportID & " has been sent<br><br>"
  248.             Response.Write "<a href='ReportMain.asp'>Back to Report Main</a>"
  249.     %>
  250.     <script type="text/javascript">
  251.     <!--
  252.         window.setTimeout("window.location='ReportMain.asp'",2500);
  253.     -->
  254.     </script>            
  255.     <%            
  256.         end if
  257.     end if
  258.     conn.Close
  259.     set rs = nothing
  260.     set rs1 = nothing
  261.     set conn = nothing
  262. %>
  263.         </td>
  264.     </tr>
  265. </table>
Apr 8 '11 #8

jhardman
Expert 2.5K+
P: 3,405
The problem is you keep initializing a new blank variable called "body" and assigning that blank variable to the message body. You then add text to the body variable. You have never assigned any text to the message body. Put all of the text in the body variable BEFORE you assign the message.body = body. Would a diagram help?

Jared
Apr 8 '11 #9

P: 7
Please a diagram would definately help! Thx sooo much!!
Apr 8 '11 #10

P: 7
Hi Jared
I figured out what you ment in your last posting. It now works great! Thx a bunch for your help! U saved me a bunch of time!!
Tibor
Apr 8 '11 #11

jhardman
Expert 2.5K+
P: 3,405
OK, imagine that when you create a new message (set objMessage = createobject("cdo.message") ) you are asking the server to create a blank email form like this:

You go about filling this in with commands like this:
Expand|Select|Wrap|Line Numbers
  1. objMessage.To = emailTo
  2. objMessage.From = getSetting("pEmailFrom") 
  3. objMessage.Subject = Request.Form("txtEmailSubject") 
Does this make sense so far? Everything up to this point you do right.The problem comes when you try to fill in the body of the email. You have a variable called "body". Because it holds text, let's imagine the variable as a blank text box:

When you go to fill in the body of the email, you say
Expand|Select|Wrap|Line Numbers
  1. objMessage.HTMLBody = body
This command is fine, and it would work, except that body is your blank text box. There is no text in the text box, so you have just specified that the email body should be blank.

After this you have several lines that look like this:
Expand|Select|Wrap|Line Numbers
  1. body = body & "<HTML>..."
This adds text to the variable body BUT IT DOESN'T ADD TEXT TO THE BODY OF THE EMAIL. In other words, your email message looks like this:

Where is all the text that you wanted to put in the body of the email? it is in the variable "body":

Are you with me so far?
Apr 8 '11 #12

jhardman
Expert 2.5K+
P: 3,405
The whole problem boils down to you filling out the body of the email by pointing it to an empty variable
Expand|Select|Wrap|Line Numbers
  1. objMessage.HTMLBody = body
there is no text in this variable, so your email body is blank. You spend a lot of time after that putting text into the variable, but not into the body of the email. If you just switched those around (fill in the body of the email AFTER you put text into the variable "body") that would solve your problem.

Jared
Apr 8 '11 #13

P: 1
jhardman,

Thanks for your awesome post! I was able to solve my problem as well with your detailed explanation :)
Oct 18 '13 #14

Post your reply

Sign in to post your reply or Sign up for a free account.