; VM-1 mail code snippets ; As it stands, this works as a test/demo program on the Micro-Robotics ; office network, where the VM-1 has an email account named "vm1" on the server. ; To use on your network, all the user names, passwords and IP addresses will have ; to be changed ; ; The code illustrates: ; * sending email to any destination ; * reading and deleting any number of messages from a POP3 mailbox ; * extracting header values such as "Subject" and "Sender" in a received message TO init MAKE eth Protocol("eth", 1, $d, "172.16.1.150") ; this defines a local nameserver. If you don't do this you'll have ; to use IP addresses instead of names for everything eth.Address('n') := "172.16.1.146" ; you don't need the next line is unless you're using a remote server ; for mail (not on your LAN). It defines a gateway to send all external traffic. ; On our network, for example, it's the address of an ADSL router. eth.Address('d') := "172.16.1.199" ; "thor.localnet" is our local server, which can handle both outgoing (SMTP) mail ; and incoming POP3 mailboxes. ; In many cases the POP3 and SMTP servers will be different. MAKE pop3 Protocol("pop3", "thor.localnet") MAKE smtp Protocol("smtp", "thor.localnet", "vm1.localnet") END TO main send_test_message PRINT "waiting for message to be processed", CR WAIT 2000 PRINT "trying to receive message", CR receive_messages END ; in the code that follows, we give the VM1's email recipient and sender ; an email address of vm1@thor.localnet ; VM-1 sends a fixed test message to itself ; (this relies on the VM-1 having a user account on server thor.localnet) TO send_test_message AUTODESTRUCT LOCAL msg := NEW Buffer("") PRINT TO msg, "Hello!", CR, "This is a test message", CR send_email_to("VM-1 test ", "test message", msg) END ; send a message to anyone ; dest, subject and msg are buffers or strings TO send_email_to(dest, subject, msg) AUTODESTRUCT LOCAL rc PRINT "sending to ", dest, CR rc := smtp.Send("VM-1 test ", dest, subject, "", msg) IF rc = 0 PRINT "msg sent OK", CR ELSE PRINT "message sending failed, error code ", rc:1, CR END ; receives, displays and deletes all messages in mailbox ; ; The vm-1 has a user account for POP3 mail with username "vm1" and password "vm1" ; Normally you might use something a bit more cryptic! TO receive_messages AUTODESTRUCT LOCAL n n := pop3.Open("vm1", "vm1") ; username and password IF n > 0 [ REPEAT n ; positive n = number of messages in mailbox [ PRINT "From: ", pop3.(INDEX, "from"), CR, "Subject: ", pop3.(INDEX, "subject"), CR, "====", CR ; display the message contents ; you could use PRINT TO to store message in a buffer or file PRINT pop3:INDEX, CR, "====", CR, CR pop3.Remove(INDEX) ; marke for deletion now we've read it ] pop3.Close ; server will now remove deleted messages ] ELSE IF n= 0 [ PRINT "no messages",CR pop3.Close ] ELSE ; negative n means error PRINT "pop3 error code ", n,CR END