Guinness
Guinness

Reputation: 97

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?

Upvotes: 1

Views: 70

Answers (2)

Karsten
Karsten

Reputation: 2433

According to the documentation at: Microsoft the property Attachments is of type Attachments and not String, so you cannot just set it to a String. It's also rather a collection of things and not just a single thing.

As such you should try sending Add to the Attachments object like so:

    (t2 getProperty: #Attachments) invokeMethod: #Add withArguments: #('C:\temp\file.txt')

(I haven't tested the code)

Upvotes: 2

Bob Nemec
Bob Nemec

Reputation: 511

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.

Upvotes: 2

Related Questions