Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Chilkat sending mail to Office 365 with attachments I'm stuck.

Status
Not open for further replies.

Babs23

Programmer
Jun 22, 2023
2
GB
Hi All,

I'm hoping someone has experience with Chilkat I'm currently just running the 30 day trial to see if it can do what I want it to.
My company is moving from a dedicated mail server on site to Office 365, a number of our current Foxpro forms automatically generate an email for our end customer using Westwind Utils and it works great but I cannot figure out how to use WW for 365 and so am trialling Chilkat.

Code:
LOCAL loJsonToken
LOCAL lnSuccess
LOCAL loMailman
LOCAL loEmail
LOCAL loAttach1

SET DEFAULT TO "T:\WIPTRACK\Chilkat"

* First get our previously obtained OAuth2 access token.
loJsonToken = CreateObject('Chilkat_9_5_0.JsonObject')
lnSuccess = loJsonToken.LoadFile("t:\wiptrack\chilkat\token\office365.json")

loMailman = CreateObject('Chilkat_9_5_0.MailMan')

loMailman.SmtpHost = "smtp.office365.com"
loMailman.SmtpPort = 587
loMailman.StartTLS = 1

* Use your Office365 email address for the SmtpUsername.
loMailman.SmtpUsername = "it@DOMAIN.onmicrosoft.com"
loMailman.OAuth2AccessToken = loJsonToken.StringOf("access_token")

* Create a new email object
loEmail = CreateObject('Chilkat_9_5_0.Email')

loAttach1 = CreateObject('Chilkat_9_5_0.BinData')
loAttach1.LoadFile("C:\1\c2v.txt")

loEmail.Subject = "This is sent via chilkat in vfp"
loEmail.Body = "This is a test"
loEmail.From = "IT <it@DOMAIN.onmicrosoft.com>"
lnSuccess = loEmail.AddTo("Testing email","babs23@DOMAIN.co.uk")

lnSuccess = loMailman.SendEmail(loEmail)
IF (lnSuccess <> 1) THEN
    thisform.edit1.Value= loMailman.LastErrorText &&debug purposes
    RELEASE loJsonToken
    RELEASE loMailman
    RELEASE loEmail
    CANCEL
ENDIF

lnSuccess = loMailman.CloseSmtpConnection()
IF (lnSuccess <> 1) THEN
    thisform.Label1.Caption= "Connection to SMTP server not closed cleanly."
ELSE
	thisform.Label1.Caption= "Mail Sent!"   
ENDIF

RELEASE loJsonToken
RELEASE loMailman
RELEASE loEmail

This is the current code I am running to try and send an email with an attachment I am receiving the email just without the file attached I'm hoping someone can point me in the right direction.

Cheers,
Babs.
 
I have managed to figure it out and will leave my solution here for anyone else that is struggling.
I hope it helps someone in the future!

Code:
* Create a new email object
loEmail = CreateObject('Chilkat_9_5_0.Email')

[s]loAttach1 = CreateObject('Chilkat_9_5_0.BinData')
loAttach1.LoadFile("C:\1\c2v.txt")[/s]

loEmail.AddFileAttachment("C:\1\c2v.txt") &&Working

loEmail.Subject = "This is sent via chilkat in vfp"
loEmail.Body = "This is a test"
loEmail.From = "IT <it@DOMAIN.onmicrosoft.com>"
lnSuccess = loEmail.AddTo("Testing email","babs23@DOMAIN.co.uk")
 
Well,

it was obvious already, just loading a file into a Chilkat_9_5_0.BinData object doesn't attach it to the mail, no matter if you name it oAttach or not. Just like creating oTextbox = CreateObject("tettbox") and wondering why no textbox appears on a form. It's just there standalone, not attached to anything.

And the mail object has an AddFileAttachment method you finally found yourself.

Chilkat also has so many code samples...

Always refer to all the Chilkat code samples at In your case find "Email object" on the left side, that's leading to and then just search on the page, CTRL+F, and searching either "attach" or "file" leads to the code sample you need.

Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top