Sending and Receiving Email using APLX
APLX includes built-in system classes sendmail and getmail which allow you to send and receive e-mail messages. They are included in all GUI versions of APLX (Windows, Macintosh OS X and Linux). Here is a quick introduction to some of the main features.
Sending Email
To send e-mail you first need to create a sendmail object and call its Open method to establish a connection to the SMTP mail server:
SM←'⎕' ⎕NEW 'SendMail'
SM.host←'smtp.supernet.com'
SM.user←'myusername'
SM.password←'sesame'
SM.Open
0The Open method returns the error code 0 to indicate success. (
Whilst you have this connection open, you are tying up resources on the server. You should aim to close the connection as soon as you can; if you fail to do so, the SMTP server will eventually time out and close the connection automatically.)
After a successful Open operation, you can set the e-mail properties like To and From, and then send the e-mail:
SM.to←'fredsmith@supernet.com'
SM.from←'bob3@yahoo.com'
SM.subject←'Pricing and Availability'
SM.attachments←'c:\docs\Price List.doc'
SM.body←'Here is the price list you requested',⎕R,'Thanks. Bob'
errcode←SM.SendMessageOnce you have called the SendMessage method to send the e-mail, you can then write new values to the various properties, and send another message. To reset all the properties to their default (empty) values before assigning new values, call the Clear method; this is usually recommended to avoid accidentally retaining some parts of the previous message (such as an attachment). In some cases, however, you may want to send out variants of the same message, in which you can simply change those properties which are different, without calling Clear.
Finally you should close the connection when all e-mails have been sent:
SM.Close
Receiving Emails
The getmail object allows your APLX applications to retrieve and manipulate e-mail messages on a mail server, using the POP3 protocol supported by most Internet Service Providers (ISPs) for reading e-mails. (IMAP is not currently supported).
To receive e-mails you must first create a getmail object and connect to the mail server:
Mail1←'⎕' ⎕NEW 'GetMail'
Mail1.host←'pop3.supernet.com'
Mail1.user←'wildfire'
Mail1.password←'secret'
Mail1.Open
0
Whilst you have this connection open, the mailbox will be locked. You should aim to close the connection as soon as you can; if you fail to do so, the POP3 server will eventually time out, close the connection, and abort any pending requests to delete messages.
You can retrieve a list of all the e-mail messages held by the server using the 'messages' property. This returns a nested array with one row per message. Each row includes information like the message date and subject, and who it's from and to:
Mail1.count
177
messages←Mail1.messages
6 1 ⍴ messages[1;]
bob3@yahoo.com
fred@supernet.com
Wed, 24 Dec 2008 11:19:10 +0000
Worldwide sale now on
17698
0To access an individual message you can use the GetSummary or GetMessage method, which retrieves the message with the specified number from the server and then optionally deletes it. After getting a message, you can access its individual properties:
Mail1.GetMessage 1
Mail1.subject
Worldwide sale now on
⍴Mail1.body
15785
70↑Mail1.body
Don't miss this never-to-be-repeated opportunity to buy our products,If you don't want to leave the message on the server, use the Delete method, specifying the message number to delete:
Mail1.Delete 1
Finally, you should close the connection:
Mail1.Close
APL Wiki