Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to send e-mail from Visual FoxPro
Message
From
21/11/2018 08:50:03
 
 
To
21/11/2018 07:42:13
General information
Forum:
Visual FoxPro
Category:
Internet applications
Environment versions
Visual FoxPro:
VFP 9 SP2
Miscellaneous
Thread ID:
01663591
Message ID:
01663599
Views:
61
>>>>I'm developing a VFP9 app and it needs to send email (simple or with a file attached) with a free tool or free library. I remember several years ago (2009) when there were some tools that worked (although I don't remember their names). I'd like to use my gmail account for testing (but not using gmail client).
>>>>
>>>>One (less desired) alternative would be to use Outlook in the background (without showing it on screen).
>>>>
>>>>TIA
>>>
>>>I would avoid using Outlook, because as soon as clients install Office 64bit it would not work.
>>>
>>>I attempted to write a 64bit wrapper application in NET to be used from VFP, but that comes with a lot of hurdles. In the end I used CDO.
>>
>>If you use OLE it shouldn't be an issue, should it?
>>
>>
lo = CREATEOBJECT("Outlook.application")
>>...
>>
>>The interface there is transparent between 32-bit and 64-bit, isn't it?
>
>No, I thought so too, but this only works with 64 bit Word or Excel.
>
>Outlook apparently does not work, unless someone has another experience?

I just had a look and was surprised that the running instance of outlook.exe for our Outlook 365 64-bit machines / 64-bit versions of Windows 10 is a 32-bit process! I never noticed before, and just assumed it was 64-bit.

I just checked "Excel.application" and it's the same way. I never knew. Interesting.

Regardless, here's the general code we use. The actual implementation is a little more than this, handles error conditions differently, but this is the gist.
TRY
    * Create our Outlook instance object
    loOutlook           = CREATEOBJECT("outlook.application")
    llOutlookPresent    = .t.
CATCH
    llOutlookPresent    = .f.
ENDTRY

* With Outlook we try to use it to send first
llError = .f.
IF llOutlookPresent
    TRY
        * Create our message
        loNameSpace = loOutlook.GetNameSpace("MAPI")
        loOutbox    = loNameSpace.GetDefaultFolder(4)
        loNewMsg    = loOutbox.Items.Add()

        * Append attachment (added for a simple example here, the logic actually may have multiple in a loop)
        loNewMsg.Attachments.Add(lcWhateverFile,1,1,"VisibleFilename.ext")
        * Note:  See https: //msdn.microsoft.com/en-us/library/office/ff869553.aspx
        * Note:  See https: //msdn.microsoft.com/en-us/library/office/ff868693.aspx

        * Populate content
        loNewMsg.Subject = ALLTRIM(this.Parent.subject.TextBox.Value)
        loNewMsg.Body    = ALLTRIM(this.Parent.bodyText.EditBox.Value)
        thisForm.add_emails(loNewMsg, this.Parent.toAddy.TextBox.Value, 1)  && 1=olTo

        * Add Cc: if need be
        IF NOT EMPTY(this.Parent.ccAddy.TextBox.Value)
            thisForm.add_emails(loNewMsg, this.Parent.ccAddy.TextBox.Value, 2)  && 2=olCC
        ENDIF

        * Add Bcc: if need be
        IF NOT EMPTY(this.Parent.bccAddy.TextBox.Value)
            thisForm.add_emails(loNewMsg, this.Parent.bccAddy.TextBox.Value, 3) && 3=olBCC
        ENDIF

        * Try to resolve each email address
        FOR EACH loRecip IN loNewMsg.Recipients
            IF !loRecip.Resolve
                * On first failure, display the message in Outlook so they can resolve manually
                loNewMsg.Display

                * Raise an error
                llError = .t.
                lcMsg   = "Error parsing email recipient.  Please correct manually."
                EXIT
            ENDIF
        ENDFOR

        IF !llError
            * Send
            loNewMsg.Send()
            * If we get here, success
        ENDIF
        * Note:  Depending on settings, the user may now have an unsent email
        *        on their desktop waiting for them to manually click send.

        * Indicate an email was sent
        thisForm.lEmailSent = .t.

    CATCH
        llError = .t.
        lcMsg   = "Unable to send email automatically using Outlook."
    ENDTRY

ENDIF

* Were we successful?
IF llError OR NOT llOutlookPresent
    * Fallback method...
ENDIF
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform