CDO. Message Problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dduran
    New Member
    • Jul 2006
    • 4

    CDO. Message Problems

    Is Exchange/Outlook required to be on the same box as web server to function properly.
    If not, are there any dangers in using CDO in directing a web form contents to an email address (mine of course)?
    I'm not an expert programmer, but am somewhat familiar with .asp and .net. I'll let you know if you go over my head. Any help would be greatly appreciated.
  • sashi
    Recognized Expert Top Contributor
    • Jun 2006
    • 1749

    #2
    hi there,

    not really.. all you need is a an smtp server to manage outgoing mail messages.. it doesn't have to be on the same box.. dangers? well.. not that i know of as long as you are behind a properly configured firewall or an antivirus.. take care.

    Comment

    • dduran
      New Member
      • Jul 2006
      • 4

      #3
      Thanks Sashi for the quick reply. I can't seem to get the code right to use a remote smtp server. Any suggestions? I have tried the following code with no success...what do you think?

      Start David's Test Code----
      Code:
       <% 
      option explicit
       
      '---------------------------------------------------------------------------------------------------
      'FORM MAIL SCRIPT
      '----------------
      'usage:
      '<form ACTION="sendmail.asp" ...>
      '
      'hidden fields:
      '	redirect	- the url to redirect to when the mail has been sent (REQUIRED)
      '	mailto		- the email address of the recipient (separate multiple recipients with commas) (REQUIRED)
      '	cc			- the email address of the cc recipient (separate multiple recipients with commas) (OPTIONAL)
      '	bcc			- the email address of the bcc recipient (separate multiple recipients with commas) (OPTIONAL)
      '	mailfrom	- the email address of the sender (REQUIRED)
      '	subject		- the subject line of the email (REQUIRED)
      '	message		- the message to include in the email above the field values. not used when a template is being used. (OPTIONAL)
      '	template	- specifies a text or html file to use as the email template, relative to the location of the sendmail script. (e.g. ../email.txt)
      '				 A template should reference form fields like this: [$Field Name$]
      '	html		- if this has the value "yes", the email will be sent as an html email. only used if a template is supplied.
      '	testmode	- if this is set to "yes", the email contents will be written to the screen instead of being emailed.
      '---------------------------------------------------------------------------------------------------
      dim pde : set pde = createobject("scripting.dictionary")
      '---------------------------------------------------------------------------------------------------
      'PREDEFINED ADDRESSES for the "mailto" hidden field
      'if you don't want to reveal email addresses in hidden fields, use a token word instead and specify
      'below which email address it applies to. e.g. <input type="hidden" name="mailto" value="%stratdepartment%">
      'ALSO, in the same way, you can use %mailfrom% to hide the originating email address
      pde.add "%DirectMailRequest%", "thermocraft@satx.rr.com"
      'pde.add "%salesenquiry%", "anotheremail@someaddress.com"
      '---------------------------------------------------------------------------------------------------
       
      function getTextFromFile(path)
      	dim fso, f, txt
      	set fso = createobject("Scripting.FileSystemObject")
      	if not fso.fileexists(path) then
      		getTextFromFile = ""
      		exit function
      	end if
      	set f = fso.opentextfile(path,1)
      	if f.atendofstream then txt = "" else txt = f.readall
      	f.close
      	set f = nothing
      	set fso = nothing
      	getTextFromFile = txt
      end function
       
      dim redir, mailto, mailfrom, subject, item, body, cc, bcc, message, html, template, usetemplate, testmode
      redir = request.form("redirect")
      mailto = request.form("mailto")
      if pde.exists(mailto) then mailto = pde(mailto)
      cc = request.form("cc")
      bcc = request.form("bcc")
      mailfrom = request.form("mailfrom")
      if mailfrom = "" then mailfrom = pde("%mailfrom%")
      subject = request.form("subject")
      message = request.form("message")
      template = request.form("template")
      'testmode = lcase(request.form("testmode"))="no"
       
      if len(template) > 0 then template = getTextFromFile(server.mappath(template))
      if len(template) > 0 then usetemplate = true else usetemplate = false
      dim msg : set msg = server.createobject("CDO.Message")
      msg.subject = subject
      msg.to = mailto
      msg.from = mailfrom
      if len(cc) > 0 then msg.cc = cc
      if len(bcc) > 0 then msg.bcc = bcc
       
      if not usetemplate then
      	body = body & message & vbcrlf & vbcrlf
      else
      	body = template
      end if
      for each item in request.form
      	select case item
      		case "redirect", "mailto", "cc", "bcc", "subject", "message", "template", "html", "testmode"
      		case else
      			if not usetemplate then
      				if item <> "mailfrom" then body = body & item & ": " & request.form(item) & vbcrlf & vbcrlf
      			else
      				body = replace(body, "[$" & item & "$]", replace(request.form(item),vbcrlf,"<br>"))
      			end if
      	end select
      next
       
      if usetemplate then 'remove any leftover placeholders
      	dim rx : set rx = new regexp
      	rx.pattern = "\[\$.*\$\]"
      	rx.global = true
      	body = rx.replace(body, "")
      end if
       
      if usetemplate and lcase(request.form("html")) = "yes" then
      	msg.htmlbody = body
      else
      	msg.textbody = body
      end if
      if testmode then
      	if lcase(request.form("html")) = "yes" then
      		response.write "<pre>" & vbcrlf
      		response.write "Mail to: " & mailto & vbcrlf
      		response.write "Mail from: " & mailfrom & vbcrlf
      		if len(cc) > 0 then response.write "Cc: " & cc & vbcrlf
      		if len(bcc) > 0 then response.write "Bcc: " & bcc & vbcrlf
      		response.write "Subject: " & subject & vbcrlf & string(80,"-") & "</pre>"
      		response.write body
      	else
      		response.write "<html><head><title>Sendmail.asp Test Mode</title></head><body><pre>" & vbcrlf
      		response.write "Mail to: " & mailto & vbcrlf
      		response.write "Mail from: " & mailfrom & vbcrlf
      		if len(cc) > 0 then response.write "Cc: " & cc & vbcrlf
      		if len(bcc) > 0 then response.write "Bcc: " & bcc & vbcrlf
      		response.write "Subject: " & subject & vbcrlf & vbcrlf
      		response.write string(80,"-") & vbcrlf & vbcrlf & "<span style=""color:blue;"">"
      		response.write body & "</span>" & vbcrlf & vbcrlf
      		response.write string(80,"-") & vbcrlf & "**END OF EMAIL**</pre></body></html>"
      	end if
      else
      	msg.send
      	response.redirect redir
      end if
      set msg = nothing
      %>
      End David's Test Code---
      Last edited by Niheel; Aug 22 '06, 05:22 AM.

      Comment

      • tejaswini
        New Member
        • Jul 2006
        • 7

        #4
        hi there,
        you need to use the configuaration object for ur settings of smtp server and the port.

        something like ths

        Code:
         
        Set objCDOSYSCon = Server.CreateObject ("CDO.Configuration") 
        		objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "127.0.0.1" 
        		objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
        objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
        objCDOSYSCon.Fields.Update
        Last edited by Niheel; Aug 22 '06, 05:22 AM.

        Comment

        • dduran
          New Member
          • Jul 2006
          • 4

          #5
          Does smtpserver have to be an ip address? Can I use "smtp-server.satx.rr. com"?
          Also, does it matter where I Set the objCDOSYSCon?
          The code I'm using is accepting the info from my form, then redirecting to a thank you page. But it is still not sending the information to my email address.

          I place the objCDOSYSCon near ("CDO.message") .

          Code:
           <% 
          option explicit
           
          'Dim objCDOSYSCon
          'Set objCDOSYSCon = Server.CreateObject ("CDO.Configuration") 
          'objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp-server.satx.rr.com" 
          'objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
          'objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
          'objCDOSYSCon.Fields.Update
           
          '---------------------------------------------------------------------------------------------------
          'FORM MAIL SCRIPT
          '----------------
          'usage:
          '<form ACTION="sendmail.asp" ...>
          '
          'hidden fields:
          '	redirect	- the url to redirect to when the mail has been sent (REQUIRED)
          '	mailto		- the email address of the recipient (separate multiple recipients with commas) (REQUIRED)
          '	cc			- the email address of the cc recipient (separate multiple recipients with commas) (OPTIONAL)
          '	bcc			- the email address of the bcc recipient (separate multiple recipients with commas) (OPTIONAL)
          '	mailfrom	- the email address of the sender (REQUIRED)
          '	subject		- the subject line of the email (REQUIRED)
          '	message		- the message to include in the email above the field values. not used when a template is being used. (OPTIONAL)
          '	template	- specifies a text or html file to use as the email template, relative to the location of the sendmail script. (e.g. ../email.txt)
          '				 A template should reference form fields like this: [$Field Name$]
          '	html		- if this has the value "yes", the email will be sent as an html email. only used if a template is supplied.
          '	testmode	- if this is set to "yes", the email contents will be written to the screen instead of being emailed.
          '---------------------------------------------------------------------------------------------------
          dim pde : set pde = createobject("scripting.dictionary")
          '---------------------------------------------------------------------------------------------------
          'PREDEFINED ADDRESSES for the "mailto" hidden field
          'if you don't want to reveal email addresses in hidden fields, use a token word instead and specify
          'below which email address it applies to. e.g. <input type="hidden" name="mailto" value="%stratdepartment%">
          'ALSO, in the same way, you can use %mailfrom% to hide the originating email address
          pde.add "%contactform%", "davidd@thermo-craft.com"
          pde.add "%salesenquiry%", "dave.dura@gmail.com"
          '---------------------------------------------------------------------------------------------------
           
          function getTextFromFile(path)
          	dim fso, f, txt
          	set fso = createobject("Scripting.FileSystemObject")
          	if not fso.fileexists(path) then
          		getTextFromFile = ""
          		exit function
          	end if
          	set f = fso.opentextfile(path,1)
          	if f.atendofstream then txt = "" else txt = f.readall
          	f.close
          	set f = nothing
          	set fso = nothing
          	getTextFromFile = txt
          end function
           
          dim redir, mailto, mailfrom, subject, item, body, cc, bcc, message, html, template, usetemplate, testmode
          redir = request.form("redirect")
          mailto = request.form("mailto")
          if pde.exists(mailto) then mailto = pde(mailto)
          cc = request.form("cc")
          bcc = request.form("bcc")
          mailfrom = request.form("mailfrom")
          if mailfrom = "" then mailfrom = pde("%mailfrom%")
          subject = request.form("subject")
          message = request.form("message")
          template = request.form("template")
          testmode = lcase(request.form("testmode"))="no"
           
          if len(template) > 0 then template = getTextFromFile(server.mappath(template))
          if len(template) > 0 then usetemplate = true else usetemplate = false
          Dim objCDOSYSCon
          	set objCDOSYSCon = Server.CreateObject ("CDO.Configuration") 
          	objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp-server.satx.rr.com" 
          	objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
          	objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
          	objCDOSYSCon.Fields.Update
          dim msg : set msg = server.createobject("CDO.Message")
          msg.subject = subject
          msg.to = mailto
          msg.from = mailfrom
          if len(cc) > 0 then msg.cc = cc
          if len(bcc) > 0 then msg.bcc = bcc
           
          if not usetemplate then
          	body = body & message & vbcrlf & vbcrlf
          else
          	body = template
          end if
          for each item in request.form
          	select case item
          		case "redirect", "mailto", "cc", "bcc", "subject", "message", "template", "html", "testmode"
          		case else
          			if not usetemplate then
          				if item <> "mailfrom" then body = body & item & ": " & request.form(item) & vbcrlf & vbcrlf
          			else
          				body = replace(body, "[$" & item & "$]", replace(request.form(item),vbcrlf,"<br>"))
          			end if
          	end select
          next
           
          if usetemplate then 'remove any leftover placeholders
          	dim rx : set rx = new regexp
          	rx.pattern = "\[\$.*\$\]"
          	rx.global = true
          	body = rx.replace(body, "")
          end if
           
          if usetemplate and lcase(request.form("html")) = "yes" then
          	msg.htmlbody = body
          else
          	msg.textbody = body
          end if
          if testmode then
          	if lcase(request.form("html")) = "yes" then
          		response.write "<pre>" & vbcrlf
          		response.write "Mail to: " & mailto & vbcrlf
          		response.write "Mail from: " & mailfrom & vbcrlf
          		if len(cc) > 0 then response.write "Cc: " & cc & vbcrlf
          		if len(bcc) > 0 then response.write "Bcc: " & bcc & vbcrlf
          		response.write "Subject: " & subject & vbcrlf & string(80,"-") & "</pre>"
          		response.write body
          	else
          		response.write "<html><head><title>Sendmail.asp Test Mode</title></head><body><pre>" & vbcrlf
          		response.write "Mail to: " & mailto & vbcrlf
          		response.write "Mail from: " & mailfrom & vbcrlf
          		if len(cc) > 0 then response.write "Cc: " & cc & vbcrlf
          		if len(bcc) > 0 then response.write "Bcc: " & bcc & vbcrlf
          		response.write "Subject: " & subject & vbcrlf & vbcrlf
          		response.write string(80,"-") & vbcrlf & vbcrlf & "<span style=""color:blue;"">"
          		response.write body & "</span>" & vbcrlf & vbcrlf
          		response.write string(80,"-") & vbcrlf & "**END OF EMAIL**</pre></body></html>"
          	end if
          else
          	msg.send
          	response.redirect redir
          end if
          set msg = nothing
          %>
          Last edited by Niheel; Aug 22 '06, 05:22 AM.

          Comment

          • sashi
            Recognized Expert Top Contributor
            • Jun 2006
            • 1749

            #6
            Hi Duran,

            the practical way is to leave the ip of your SMTP server to it's localhost address, which is 127.0.0.1.. this is if you have your SMTP server.. you can also specify an external ip address or hostname if you have no SMTP in server your network.. take care my fren..

            Comment

            • dduran
              New Member
              • Jul 2006
              • 4

              #7
              Sashi and tejaswini

              I got everything to work!!! Thanks for your help. This is the first time I use a forum to ask for help, I'm glad I did. Mission Accomplished!!!

              David Duran

              Comment

              • sashi
                Recognized Expert Top Contributor
                • Jun 2006
                • 1749

                #8
                Hi Dave,

                at last ah? :) good luck and take care my fren.. :)

                Comment

                • Niheel
                  Recognized Expert Moderator Top Contributor
                  • Jul 2005
                  • 2432

                  #9
                  Reminder to members posting to this thread: Please use code tags when posting code.
                  niheel @ bytes

                  Comment

                  Working...