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

PB/DLL - SendMyMail

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

  • PB/DLL - SendMyMail

    Code:
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' Demo of SendMyMail function that handles long body text in mail,
    ' since ShellExecute can fail if text is too long (like > 1.4 KB..)
    ' Only tested with Netscape 4.6
    ' Demonstrates how to find a child window in any running app' and
    ' get/set own text to it. Look and see - can be base for doing other
    ' fun hacks in Windows too..   [img]http://www.powerbasic.com/support/forums/smile.gif[/img] Public Domain by Borje Hagsten, Sept 2001
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    #COMPILE EXE
    #INCLUDE "WIN32API.INC"
     
    GLOBAL ghMailProg AS LONG, ghBodyTextWindow AS LONG, gsSubject AS STRING
     
    DECLARE CALLBACK FUNCTION DlgProc() AS LONG
    DECLARE FUNCTION EnumWindowsProc(BYVAL hWnd AS LONG, BYVAL lParam AS LONG) AS LONG
    DECLARE FUNCTION EnumChildsProc(BYVAL hWnd AS LONG, BYVAL lParam AS LONG) AS LONG
    DECLARE FUNCTION SendMyMail(BYVAL hDlg AS LONG, _
                                BYVAL mailTo AS STRING, _
                                BYVAL subject AS STRING, _
                                BYVAL bodyText AS STRING) AS LONG
     
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' Main entrance
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    FUNCTION PBMAIN () AS LONG
     LOCAL hDlg AS LONG
     
      DIALOG NEW 0, "Long mail test..",,, 130, 30, %WS_CAPTION OR %WS_SYSMENU, 0 TO hDlg
      CONTROL ADD BUTTON, hDlg, %IDOK, "&SEND MAIL",   4,  4, 60, 14
      CONTROL ADD BUTTON, hDlg, %IDCANCEL, "&CANCEL", 64,  4, 60, 14
     
      DIALOG SHOW MODAL hDlg CALL DlgProc
    END FUNCTION
     
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' Main dialog's callback procedure
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    CALLBACK FUNCTION DlgProc() AS LONG
     
      IF CBMSG = %WM_COMMAND THEN
         IF CBCTL = %IDOK THEN
            LOCAL sTxt AS STRING
            sTxt = REPEAT$(1000, "Send more money.. " + $CRLF) 'test with a 20,000 byte string
     
            CALL SendMyMail(CBHNDL, "[email protected]", "Stupid subject..", sTxt)
     
         ELSEIF CBCTL = %IDCANCEL THEN
            DIALOG END CBHNDL
         END IF
      END IF
    END FUNCTION
     
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' SendMyMail starts default mail program, passes on address and then
    ' enumerates all windows and looks for proper text field to paste the
    ' body text into. Reason for this - long text fails with ShellExecute.
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    FUNCTION SendMyMail(BYVAL hDlg AS LONG, _
                        BYVAL mailTo AS STRING, _
                        BYVAL subject AS STRING, _
                        BYVAL bodyText AS STRING) AS LONG
     
      LOCAL cnt AS LONG, mText AS ASCIIZ * 1000
     
      mText = "mailto:"   & mailTo & _
              "?Subject=" & subject & _
              "&Body="    & "%09%09%09%09%09"  'send 5 tabs (whatever) as id string for body text window
     
      IF ShellExecute(hDlg, "open", mText, "", "", %SW_SHOWNORMAL) > 32 THEN ' 'if success
         gsSubject = subject                             'tell global string what subject to look for
     
         'Note:  following loop is not the best/safest way to find the mail program/body text window,
         'but can work fine for own use. Probably not wise to rely on it for public use, though.
         'Note2: since we set the text via api call, no need to convert anything in text..
         
         DO                                              'look for the mail program..
            SLEEP 100                                    'take a 0.1 second break between each attempt
            EnumWindows CODEPTR(EnumWindowsProc), 0      'enumerate all open windows
            IF ghMailProg THEN                           'we got one with correct caption,
               EnumChildWindows ghMailProg, CODEPTR(EnumChildsProc), 0 'enumerate its childs
               EXIT DO                                            'and exit this loop
            END IF
            INCR cnt                                     'still nothing found - increase counter
         LOOP UNTIL cnt = 100                            'try 100 times (ten seconds), then bail out
     
         IF ghBodyTextWindow THEN                        'if we found correct child window in mail program
            SendMessage ghBodyTextWindow, %WM_SETTEXT, 0, BYVAL STRPTR(bodyText) 'set the text
         END IF
      END IF
     
    END FUNCTION
     
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' enumerate running app's - look for caption containing subject string
    '(note: only tested with Netscape, you may have to exeriment here..)
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    FUNCTION EnumWindowsProc(BYVAL hWnd AS LONG, BYVAL lParam AS LONG) AS LONG
      LOCAL zTxt AS ASCIIZ * 260
     
      GetWindowText hWnd, zTxt, 260
     
      IF LEN(zTxt) AND INSTR(zTxt, gsSubject) THEN  'look for the subject in the window's caption
         ghMailProg = hWnd            'assign current hWnd to global variable
         FUNCTION = 0 : EXIT FUNCTION 'return zero to break enumeration and exit
      END IF
     
      FUNCTION = 1
    END FUNCTION
     
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' enumerate child windows - look for identification string (5 tabs)
    ' Note: I have only tested this with Netscape 4.6 - don't know if it
    ' works with other versions/other mail programs.
    ' Note2: one can also use GetClassName and look for a specific control
    ' class, like EDIT, LISTBOX and COMBOBOX, etc. Not done here, because
    ' we can't be sure searched window is a standard control..
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    FUNCTION EnumChildsProc(BYVAL hWnd AS LONG, BYVAL lParam AS LONG) AS LONG
      LOCAL zTxt AS ASCIIZ * 20
     
      SendMessage hWnd, %WM_GETTEXT, 20, VARPTR(zTxt) 'get child window's text
     
      IF LEFT$(zTxt, 5) = $TAB + $TAB + $TAB + $TAB + $TAB THEN 'look for id string (5 tabs)
         ghBodyTextWindow = hWnd      'assign current hWnd to global variable
         FUNCTION = 0 : EXIT FUNCTION 'return zero to break enumeration and exit
      END IF
     
      FUNCTION = 1
    END FUNCTION

    ------------------


    [This message has been edited by Borje Hagsten (edited September 20, 2001).]

  • #2
    Succesfully tested it with Outlook Express 5.5.

    Jan2007: does not paste the body text in OE6

    Regards,
    Henk

    ------------------
    Henk Broekhuizen,
    [email protected]

    [This message has been edited by Henk Broekhuizen (edited January 13, 2007).]
    Henk Broekhuizen, PA3BLP
    powerbasicforum -at- doorhet.net
    Sexbierum, The Netherlands
    ========================

    Comment

    Working...
    X