Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

HTML Email with Attachments via CDO

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • HTML Email with Attachments via CDO

    Code:
    'Simple CDO Example for sending HTML-based email.
    'CDO.INC is not included, just prepare your own from
    'the PB COM Browser, I used:
    'Microsoft CDO for NTS 1.2 Library on Windows 2000 Pro
    'Feel free to use this code in any derivative you would like.
    'Author:  Ken Myers
    'Date: 10/06/2003
    'PBCC 3.0 Version.  Feel free to change STDOUT below to MSGBOX for PBWIN 7.0.
    'FYI, I use it via JNI and the CoInitialize is very handy to know for other
    'JNI bridge makers.   [img]http://www.powerbasic.com/support/forums/smile.gif[/img]
    
    #COMPILE EXE
    #INCLUDE "win32api.inc"
    #INCLUDE "cdo.inc"
    
    FUNCTION PBMAIN() AS LONG
        
        ON ERROR GOTO errtrap:
        
        CALL CoInitialize(BYVAL 0)
        
        DIM myMail AS CDONTSNewMail
        DIM A AS ASCIIZ * 1024
        SET myMail = NEW CDONTSNewMail IN $PROGID_CDONTSNewMail1
        
        IF ISFALSE ISOBJECT(myMail)THEN
            GOTO errtrap:
        END IF
        
        DIM HTML AS STRING
    
        HTML = "<!DOCTYPE HTML PUBLIC ""-//IETF//DTD HTML//EN"">" & $CRLF
        HTML = HTML & "<html>"
        HTML = HTML & "<head>"
        HTML = HTML & "<title>CDO Magic</title>"
        HTML = HTML & "</head>"
        HTML = HTML & "<body>"
        HTML = HTML & "<b>This</b> is a test email in HTML format. <BR></body>"
        HTML = HTML & "</html>"
    
        DIM vVnt AS VARIANT
        DIM vBody AS VARIANT
        DIM vSubject AS VARIANT
        DIM vTo AS VARIANT
        DIM vFrom AS VARIANT
        DIM vBodyFormat AS VARIANT
        DIM vMailFormat AS VARIANT
        DIM vAttach AS VARIANT
        
        'Change the from/to email addresses!
        vFrom = "[email protected]"
        vTo = "[email protected]"
        vSubject = "Sample Message"
        vBodyFormat = 0
        vMailFormat = 0
        vBody = HTML
        
        'Make sure you use the absolute path to the file if you want to attach one!
        'vAttach = "c:\test.txt"
        
        'FYI, Really should test all string values before executing the LET...
        If trim$(VARIANT$(vFrom))<>"" Then
           OBJECT LET myMail.From = vFrom
        End If
    
        OBJECT LET myMail.To = vTo
        OBJECT LET myMail.Subject = vSubject
        OBJECT LET myMail.BodyFormat = vBodyFormat
        OBJECT LET myMail.MailFormat = vMailFormat
        OBJECT LET myMail.Body = vBody
        
        'OBJECT CALL myMail.AttachFile(vAttach) TO vVnt
           
        OBJECT CALL myMail.Send TO vVnt
    
        SET myMail = NOTHING
        
        CALL CoUninitialize()
        
        STDOUT "Success!"
        WAITKEY$
        EXIT FUNCTION
    
    errtrap:
    
        'I like showing the more descriptive error during
        'testing.
        CALL FormatMessage(%FORMAT_MESSAGE_FROM_SYSTEM, _
        BYVAL 0&, OBJRESULT, BYVAL MAKELANGID( _
        %LANG_NEUTRAL, %SUBLANG_DEFAULT), _
        A, SIZEOF(A), BYVAL 0&)
        STDOUT A
        STDOUT "Failure " & ERROR$
        WAITKEY$
        EXIT FUNCTION
    
    END FUNCTION
    ------------------




    [This message has been edited by Ken Myers (edited October 06, 2003).]
    Mac Application Reviews and More

  • #2
    By request, here is the CDO.INC that I used.

    Code:
    ' Legal Copyright:   Copyright (C) 1986-1999 Microsoft Corp. All rights reserved.
    
    [b]Removed by Administrator. Please don't post copyrighted code unless
    you can show that it may legally be distributed in public.[/b]
    ------------------
    Mac Application Reviews and More

    Comment


    • #3
      In case not everyone is following the Programming thread... You
      can download an example EXE and source code (written for VD for PB)
      from the following address:
      http://www.zcureit.com/vd/CDOMailer.zip

      -Ken

      ------------------
      Mac Application Reviews and More

      Comment


      • #4
        Failed on my Windows 98 SE machine and Windows XP Pro machine.
        Says operation completed successfully then after clicking on
        ok says CDOMailer failed to send the email!


        ------------------
        The world is full of apathy, but who cares?

        Comment


        • #5
          Odd, here is the code that generates success/fail messages:

          Code:
              If ret<>1 Then
                  MsgBox "Successfully sent the email!",%MB_TASKMODAL, VD_App.Title 
                  Call Form1_cmdNew_Click(nCbHndl,%ID_FORM1_CMDNEW)
              Else
                  MsgBox "CDOMailer failed to send the email!",%MB_TASKMODAL, VD_App.Title 
              End If
          Anyway, what it would mean is that CDO failed. This is most likely related
          to your environment. However, I can change the code above to be safer by reversing
          the logic and checking for a return of 0. However, that won't help Mike. I'll
          have to research any dependencies (e.g. M$ Exchange, etc.). I'm sure that would be
          helpful.

          The update has now been made.

          [This message has been edited by Ken Myers (edited October 09, 2003).]
          Mac Application Reviews and More

          Comment


          • #6
            Here is a link to some of the requirements for CDO:
            http://msdn.microsoft.com/library/de...quirements.asp

            I am sure there is more information on Microsoft's site. I'm
            sure that everyone may not have everything that is needed for
            CDO to work for them.

            Works great for me through VD, PB, and JNI! Just thought I
            would offer it up.

            -Ken

            ------------------
            Mac Application Reviews and More

            Comment


            • #7
              Originally posted by Ken Myers:
              Odd,
              Not odd at all... CDONTS is not installed on either of those OSes. Although CDONTS can be copied to and used
              on an XP system (and it would be there if you have upgraded from W2K to WXP), CDOSYS is a better fit for W2K
              and up. Even with CDONTS installed you still have that IIS SMTP dependency (yes MCIS or Exchange can then be
              used as well) on the system running it.

              The CDO for Windows 2000 (CDOSYS v2.0) library is Microsoft's replacement for CDO for NT Server (CDONTS v1.21).
              Although both can be on system and used at the same time.

              CDOSYS is a subset of CDO for Exchange 2000 (CDOEX v3.0) and is code compatible with it (except a few things
              like can't use SMTP drop dir with CDOEX). CDOSYS supports using local or remote SMTP or NNTP servers, better
              MIME support/control, event synch support, etc.

              See the following article for other differences and tips on converting your code:
              http://support.microsoft.com/default...en-us%3B810702


              Note: In another thread in the programming forum I mention that CDO cannot be redistributed, but at the top of this
              post I talk about about copying CDONTS from another system. I got this suggestion directly from TechNet in the link
              below. The TechNet article (Migrate CDONTS) tals about copying CDONTS to a Windows 2003 server. I would clarify with
              Microsoft if in doubt of its legality:

              http://www.microsoft.com/technet/tre...g_dep_tnoc.asp

              Gerald W. Gaston


              [This message has been edited by Gerald W. Gaston (edited October 13, 2003).]

              Comment


              • #8
                Originally posted by Ken Myers:
                Here is a link to some of the requirements for CDO:
                http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cd o/html/_olemsg_software_requirements.asp

                That is the requirements for CDO v1.2.1 (MAPI based) not CDONTS v1.2.1. There are lots of different versions of
                CDO in use now:

                CDO 1.2.1 (CDO.DLL)
                CDO 1.2.1 HTML Rendering (CDOHTML.DLL)
                CDO 1.2.1 for Windows NT Server (CDONTS.DLL)
                CDO 2.0 for Windows 2000 (CDOSYS.DLL)
                CDO 3.0 for Exchange 2000 (CDOEX.DLL)
                CDO Management Objects for Exchange 2000 (CDOEXM.DLL)
                CDO Workflow Objects for Exchange 2000 (CDOWF.DLL)
                CDO Personal Knowledge Management (PKMCDO.DLL)

                The CDO "roadmap" is here: http://msdn.microsoft.com/library/de...do_roadmap.asp

                It doesn't mention OLE Messaging library (CDO 1.0), and a few other details so see these links as well:
                http://www.cdolive.com/cdo2.htm
                http://www.cdolive.com/cdo7.htm

                Gerald W. Gaston


                [This message has been edited by Gerald W. Gaston (edited October 12, 2003).]

                Comment

                Working...
                X