Kruse-Net.dk

Det man blogger er man selv...

Webapp mailer

En af de ting jeg har begået som jeg får flest forespørgsler på hjælp om er en lille stump kode der sender emails fra en Visual Dataflex Webapp via CDO (der findes på langt de fleste nuværende windows servere og workstations). Da forespørgslerne kommer fra udlandet er resten af artiklen — undtagelsesvis — på engelsk.

Without further ado — how to send email from a VDF Webapp:

Use mailer.pkg

// Now send a mail like this
Set psTo       of (oCDOMessage(Self)) to '"Some recipient" <mail.address@example.com>'
Set psFrom     of (oCDOMessage(Self)) to '"Me" <me@example.com>'
Set psSubject  of (oCDOMessage(Self)) to "Subject line"
Set psHTMLBody of (oCDOMessage(Self)) to "Body of email - can contain <b>HTML markup</b>"
    
Send CreateComObject to (oCDOMessage(Self))
If (IsComObjectCreated(oCDOMessage(Self))) Begin
    Send ComSend to (oCDOMessage(Self))
End
Else Begin
    Send LogEvent 91101 "Unable to create CDO.Message object"
End

Download

Should you desire to use this code from a regular VDF application, you would only need to change the LogEvent lines, since these deal with the webapp event logging mechanism which is not available in a non-web application.

Please note that mailer.pkg contains code to read general configuration (mail server, user name and password) from a system file. Change as needed!

19 Responses på “Webapp mailer”

  1. gravatar 1 James Stone
    25. januar 2007 kl. 16:44

    Hi Jakob,

    This is great. Thank you very much. Do you have a copy of the mailer.pkg mentioned?

    Best Regards,

    James Stone Box 426 Templeton CA 93465 805-434-1111 http://www.salesnavigator.com jstone@salesnavigator.net

  2. gravatar 2 Jakob Kruse
    25. januar 2007 kl. 18:15

    James,

    mailer.pkg, as well as the two other files it references, are available for download just below the source code above.

  3. gravatar 3 Braulio Bragado
    26. januar 2007 kl. 20:35

    No funciono para webApp

  4. gravatar 4 Jakob Kruse
    27. januar 2007 kl. 10:01

    Does that mean “it doesn’t work in WebApp”?

    Yes, it does work in WebApp. I have it running in several webapps. All it requires is that Microsofts CDOSYS and ADO 2.6 are available on the server.

    Look for COM objects “CDO.Message” and “ADODB.Parameter” version 2.6 or higher. These are normally available on all versions of Windows 2000, XP and 2003.

    Do note the configuration parameters in the top of mailer.pkg. You have to specify the address of your SMTP server there as well as any required login information.

  5. gravatar 5 John
    10. februar 2007 kl. 23:00

    Hello, i wanted to use your code to send an email from a WO using gmail to send the mail. But i have to send a starttls command first otherwise it won’t work. But i have no clue how to send it. The error i get is: Reaction of server: 530 5.7.0 Must issue a STARTTLS command first x24sm22096612nfb\nVDF error#: 4399 op line: 13820.

    i can send an email with this account with outlook.

    I’m using VDF12

    Thanks in advance John

  6. gravatar 6 Jakob Kruse
    11. februar 2007 kl. 13:57

    Hi John

    According to documentation that should be possible by setting the configuration field “http://schemas.microsoft.com/cdo/configuration/smtpusessl” to “true” (in oCDOConfiguration in mailer.pkg). Haven’t tried it though.

  7. gravatar 7 John
    11. februar 2007 kl. 20:49

    Thank you very much, tried it and it works fine.

    added afer the other settings of the mailer.pkg:

    Get ComItem of (oADOFields(Self)) http://schemas.microsoft.com/cdo/configuration/smtpusessl” to vItem Set pvComObject of (oADOField(Self)) to vItem Set ComValue of (oADOField(Self)) to (True)

    John

  8. gravatar 8 Jakob Kruse
    12. februar 2007 kl. 08:04

    Great. I added it to mailer.pkg, with a default value of false, for future reference.

  9. gravatar 9 Anders Hydén
    11. januar 2008 kl. 12:21

    Hi! I now have tested sending from my VDF-app, and it works fine. Some questions remain:

    1. I do want to make an attachment, but don’t know how.
    2. I use OutlookExpress but the sent mail doesn’t show up in the ‘Sent-box’
    3. The text in the body, comes from an cTextEdit-form. When sending it, the linefeeds dont work, so everything is written on the same line – not so nice.

    Greatful for an answer Anders

  10. gravatar 10 Jakob Kruse
    11. januar 2008 kl. 13:19

    Anders,

    1. Somewhere between CreateComObject and ComSend you can call ComAddAttachment on oCDOMessage. Its first (and only mandatory) parameter can be a normal path to a file you wish to attach (or an URL). Check cdosys.pkg for details or look here for an example in ASP.
    2. CDO talks directly with your mail server using the username and password you specified in mailer.pkg. It does not use Outlook Express. The mail you send will show up in the Sent Mail folder of the specified user on the mail server. If you use IMAP from Outlook Express, it will also show up there. If you use POP3 you might need to do something special to download mail from the Sent Mail folder.
    3. mailer.pkg is setup to send HTML mail. The string you use as body text must be in HTML format. Linefeeds and other whitespace is irrelevant in HTML, so it won’t show up in your mail. If you wish to add raw text (and thus show linefeeds) you should set ComTextBody from mailer.pkg instead of ComHTMLBody.

    Enjoy!

  11. gravatar 11 Bill
    29. januar 2008 kl. 09:48

    Hi Jakob,

    The cdosys.pkg file is blocked from downloading on my pc/ network. I get the other two downloads OK.

    Is there an alternative method for for downloading the file?

    Thanks

    Bill

  12. gravatar 12 Jakob Kruse
    1. februar 2008 kl. 10:19

    Bill,

    I have added a ZIP download to the page. Try that.

  13. gravatar 13 Nils G. Svedmyr
    7. september 2009 kl. 18:36

    Hi Jakob,

    I’m keep getting the following error when sending from a Vista x86 Ultimate machine with VDF 15: “COM object method invocation error. Transporten lyckades inte ansluta till servern. Error: 4399″

    Any idea what causes it? Is there some component perhaps missing on Vista machines?

    (I’ve been searching on MSDN and have Googled, but haven’t found anything that explains it)

    Thanks.

    • Nils G. Svedmyr
  14. gravatar 14 Nils G. Svedmyr
    8. september 2009 kl. 04:58

    Hi again.

    Which COM object did you select in the Studio to build the cdosys.pkg and msado26.pkg?

    Maybe there is something wrong with my machine – and it would fix it if I try to re-generate those packages?

    • Nils G.
  15. gravatar 15 Jakob Kruse
    8. september 2009 kl. 11:46

    Nils,

    The packages were built from the dll’s listed at the top of the files. I have verified that both dll’s exist on my Vista x86 Ultimate.

    The Swedish part of your error message suggest a different problem though, because that part comes from the CDOSYS COM object (so it must be working). The problem seems to be that the server address is incorrect, or maybe a firewall is blocking your attempt. CDOSYS connects using SMTP (port 25) so that must be open.

  16. gravatar 16 Nils G. Svedmyr
    8. september 2009 kl. 13:02

    Jakob,

    You are right that the dll’s exist on Vista x86 – I just found them on my laptop (silly Microsoft Search facility that didn’t list them because they are in a protected area). And it is indeed as you say the CDOSYS settings object that throws the exception error.

    Just as John reports above, I’m trying to send to an gmail address – and I have set the “STARTTLS” to true in the config object. All other info for the CDOSYS object has been copied from Windows Live Mail Account Settings for the gmail account in question. That account and all other e-mail accounts works just fine with the Windows Live Mail client.

    There’s no other firewalls than Windows Defender – and I cannot see any blocking there for using SMTP on port 25.

    Is there somewhere else I should be looking?

    More info: Checking my gmail account settings I see that the SMTP port is set to 465 and “This server requires a secure connection (SSL)” is checked.

    These settings were automatically set by the Live Mail client when the account was created. So if these settings are necessary how come that John who is also using gmail got it working??

    I’m all confused…

    Any ideas are most welcome.

    • Nils G.
  17. gravatar 17 Jakob Kruse
    8. september 2009 kl. 13:11

    There is another setting named “http://schemas.microsoft.com/cdo/configuration/smtpserverport” that you could try setting to 465 (same way that the other options are set in mailer.pkg). You could also try increasing the connection timeout from 10 to 60. Some servers deliberately delay the connection handshake because that makes some spam clients give up.

    Maybe Google has changed the port since 2007 when John got it working.

    Let me know if you have any success.

  18. gravatar 18 Nils G. Svedmyr
    8. september 2009 kl. 14:04

    Success!

    The port setting solved all problems – and I can now successfully send e-mails to gmail accounts. As said above the port number was changed to 465.

    Thank you very much for your help Jakob :-) and of course for this wonderful solution as well!

    Bye, from one happy puppy ;-)

    • Nils G.
  19. gravatar 19 Adam D'Auria
    12. juli 2010 kl. 00:08

    Oh thank you for this. Ran into a problem where I had MAPI working and then the customer bought a new computer with Win7 and, well, that was no longer an option.

    Just a note with regards to attachments, the ComAddAttachment call need to be a Get.

    Example: Get ComAddAttachment of (oCDOMessage(Self)) “c:\email.pdf” “” “” to vRtn

    Where vRtn is a variant datatype.

    Hope that saves someone some time.

Skriv en kommentar