Posted by on in iDevelop | 2 comments

In a wake to continue my article Popkin Functions, here’s an extension to the mail() function. The mail() function in that article used CDONTS, the now deprecated eMail sending COM provided by Microsoft. I won’t go deep into it but Microsoft hinted its deprecation in Windows 2000 when it shipped another eMail sending COM named CDOSYS with it. CDONTS was included in it to have backward compatibility but in WindowsXP & Windows Server 2003, its been removed. So, those using CDONTS until now, if their host switches over to Windows Server 2003, might find them in a soup as their CDONTS scripts won’t work.

There’ve been many tutorials and articles on how to use & send eMails using CDOSYS & you can relax, I’m not going to start one. Its just that I’ve extended my mail() function, which uses CDONTS, to use CDOSYS. Actually, calling it an extension would be quite wrong, as its a brand new function with no reference or relation whatsoever with the mail() function, but anyways… 😀

So, not taking your much time, here’s the candy. 😀

  1. Private Function mailCdo(pTo, pFr, pCc, pBc, pSu, pMes, pBf)
  2.     pTo = TRIM(LCASE(pTo))
  3.     pFr = TRIM(pFr)
  4.     pCc = TRIM(LCASE(pCc))
  5.     pBc = TRIM(LCASE(pBc))
  6.     pBf = TRIM(LCASE(pBf))
  8.     SET objMail = Server.CreateObject("CDO.Message")
  9. '   SET objConfig = CreateObject("CDO.Configuration")
  10. '   '//Configuration:
  11. '   objConfig.Fields(cdoSendUsingMethod) = cdoSendUsingPort
  12. '   objConfig.Fields(cdoSMTPServer) = "localhost"
  13. '   objConfig.Fields(cdoSMTPServerPort) = 25
  14. '   objConfig.Fields(cdoSMTPAuthenticate) = cdoBasic
  15. '   '//Update configuration
  16. '   objConfig.Fields.Update
  17. '   SET objMail.Configuration = objConfig
  19.     WITH objMail
  20.         .From = pFr
  21.         .To = pTo
  22.         IF NOT(pCc="") THEN
  23.             .CC = pCc
  24.         END IF
  25.         IF NOT(pBc="") THEN
  26.             .BCC = pBc
  27.         END IF
  28.         .Subject = pSu
  29.         IF pBf="html" THEN
  30.             .HTMLBody = pMes
  31.         ELSE
  32.             .TextBody = pMes
  33.         END IF
  34.         .Send
  35.     END WITH
  36.     SET objMail = NOTHING
  37. '   SET objConfig = NOTHING
  38. End Function

Whoa!! :dizzy:

Well, I’ll also explain the usage, not to worry. 😀
Readers of Popkin Functions & users of my mail() function must’ve recognised some of the parameters, no? Anyways, here they are all explained.

pTo is for specifying the destination eMail address, pFr is for specifying the sender’s eMail address, pCc & pBc are for specifying Carbon Copy recievers, pSu is for setting the eMail’s Subject, pMes for the message or eMail body & finally pBf for specifying whether the eMail should be sent as plain text or html. The values you can give it are “html” or “text”, with quotes ofcourse. See, its not difficult at all. Now here’s an example:

  1. strTo = ""
  2. strFrom = ""
  3. strSubject = "Hey Dude!!"
  4. strMessage = "What's up big bro?"
  5. CALL mailCdo(strTo, strFrom, "", "", strSubject, strMessage, "text")

This will send our eMail in a text format as we have specified the BodyFormat as text in the function call. Notice the blank quotes in the function? They are in place where the pCc & pBc parameters are to be set. Since we don’t need them, I’ve set them as empty.

Now the important things to note are:

  • All parameters except pCc, pBc & pBf, are required for sending an eMail.
  • You can skip the pBf parameter if you want, as by default its set as TEXT, meaning that essentially you need to set it only when you want to send an HTML formatted eMail. But its a good practice that you set it as TEXT if you want to send a TEXT formatted eMail or as HTML if you want to send an HTML formatted eMail. And it should ofcourse be wrapped in quotes(“).

Now, some of you may find that this function gives out error in its current state, when called & every parameter is passed correctly. 😯 Well, the code isn’t faulty, so relax. 🙂 Its just that this situation will arise if you are running this code on WindowsXP. So for this function to work correctly, you need to uncomment all the lines in the function that doesn’t have double slashes(//) in them.

If your code still doesn’t work, providing that everything else is correct, you need to add following lines to the top of the page where you are calling this function.

  1. <!--METADATA TYPE="typelib" UUID="CD000000-8B95-11D1-82DB-00C04FB1625D" NAME="CDO for Windows 2000 Library" -->
  2. <!--METADATA TYPE="typelib" UUID="00000205-0000-0010-8000-00AA006D2EA4" NAME="ADODB Type Library" -->

These lines should be the first two lines of your ASP page in which you are calling this function.

If you are running this code on Windows Server 2003, then you wouldn’t need the above 2 adjustments to the code. You can run the original code outright. 😀