Search code examples
emailoutlooksmalltalkvisualworks

Add file as attachment and send email with outlook and visualworks smalltalk


I can't figure out how to add an file as attachment when sending a mail. I have tried with: t2 setProperty: #Attachments value: 'C:\temp\file.txt'.

And the property seems correct as it wont throw an error. But no file is attached. I suspect it is because i try to send a string...

This is what I have and it works.

(t2 :=  (COMDispatchDriver createObject: 'Outlook.Application') invokeMethod: #CreateItem withArguments: #(0)) setProperty: #To value: '[email protected]'.
t2 setProperty: #CC value: ''.
t2 setProperty: #Subject value: 'Test'.
t2 setProperty: #Body value: 'Here comes the file'.

t2 invokeMethod: #Send withArguments: #().
session release.

Any one who can explain to me how to add an attachment?


Solution

  • You have to attach the file contents to the Graph API call. This is what I use...

    buildAttachmentFilename: aFilename 
    
        | content |
    
        content := aFilename asFilename contentsOfEntireFile asByteArray asBase64String.
        ^Dictionary 
            with: '@odata.type' -> '#microsoft.graph.fileAttachment'
            with: 'name' -> aFilename asFilename filename
            with: 'contentType' -> (self getAttachmentContentType: aFilename)
            with: 'contentBytes' -> content.
    

    ...each file is then added as an entry in the 'attachments' value, along with 'subject', 'body', 'toRecipients', etc. Since we log the email send, I use the draft + message send sequence, so the attachments are part of the aDictionary for the request contents.

    draftOutlookMessageJson: aDictionary
        
        | httpClient request |
    
        httpClient := Net.HttpClient new.
        request :=  Net.HttpRequest method: 'POST' url:  'https://graph.microsoft.com/v1.0/me/messages'.
        request contents: aDictionary asJson. 
        request contentType: 'application/json'.
        request authorization: 'Bearer ' , self graphAccessToken.
        (request getFieldAt: 'Prefer') value: 'IdType="ImmutableId"'.
    
        ^httpClient executeRequest: request
    

    fwiw: our application is deployed on GemStone which uses ZnClient. This VW code handy for development.