Sending Authenticated E-mail using APLX and Java

APLX includes the built-in system class 'sendmail' which is useful for sending quick e-mails under program control. However, sendmail doesn't currently work with SMTP mail servers which require authentication.

If you are using APLX under Windows, one solution is to use the .NET System.Mail namespace, described here. However, this option is not available if you are running APLX under Macintosh OS X, or under Linux.

Another option for APLX users is described below. This makes use of APLX's ability to call Java code. This has the advantage of being cross-platform, but will only work under APLX (Dyalog does not currently include the ability to call Java code).

Libraries

The following example makes use of two Java APIs. The libraries need to be downloaded and installed on your computer:

This is Sun's own API for e-mail handling. It's not part of the base classes included with all Java installations, but you can download the binary files (JAR files) from here:

This is a wrapper around the JavaMail APIs that has been developed as part of the Apache Commons project. It makes e-mail handling much simpler by hiding some of the details. You can download it from here:

In order to use the two libraries you need to make sure that they are added to the APLX Java classpath. This is done using the ⎕setup system function. This needs to be done before any other Java code is invoked, because the classpath cannot be changed once the Java Virtual Machine has been loaded by APLX.

The exact classpath syntax will depend on which OS you are using. Here's an example for Windows:

      path←'java' ⎕setup 'classpath'
      path←path, ';C:\Users\Simon\Downloads\javamail-1.4.3\mail.jar'
      path←path, ';C:\Users\Simon\Downloads\commons-email-1.2\commons-email-1.2.jar'
      'java' ⎕setup 'classpath' path

...and here's an example for Macintosh OS X:

      path←'java' ⎕setup 'classpath'
      path←path, ':/Users/Simon/Downloads/javamail-1.4.3/mail.jar'
      path←path, ':/Users/Simon/Downloads/commons-email-1.2/commons-email-1.2.jar'
      'java' ⎕setup 'classpath' path

Note that the path separator character is a ';' for Windows, and a ':' for Macintosh and Linux.

Sending an e-mail

Here is an example of using the Commons Email API to send an e-mail in APLX. This first example doesn't use authentication or SSL encryption.

      email← 'java' ⎕new 'org.apache.commons.mail.SimpleEmail'
      email.setHostName 'smtp.supernet.com'
      email.setFrom 'bob3@yahoo.com' 'Bob Dylan'
      email.addTo 'fredsmith@supernet.com'
      email.setSubject 'Pricing and Availability'
      email.setMsg 'Did you get the new price list?',⎕R,'Thanks. Bob'
      email.send

If you are using an SMTP server which requires authentication, just add the following line before sending the e-mail:

      email.setAuthentication 'myusername' 'mysecretpassword'

For SMTP servers which use a different port, you can change the port with e.g.

      email.setSmtpPort 587

SSL encryption can be enabled as follows:

      email.setSSL 1

Adding an Attachment

The above code can be changed easily so that you can include one or more attachments in an e-mail. It's just a case of using the MultiPartEmail class instead of SimpleEmail, and then creating/adding an EmailAttachment object:

      ⍝ Code as above, but using MultiPartEmail class
      email← 'java' ⎕new 'org.apache.commons.mail.MultiPartEmail'
      email.setHostName 'smtp.supernet.com'
      email.setFrom 'bob3@yahoo.com' 'Bob Dylan'
      email.addTo 'fredsmith@supernet.com'
      email.setSubject 'Pricing and Availability'
      email.setMsg 'Did you get the new price list?',⎕R,'Thanks. Bob'

      ⍝ Create and add an attachment:
      attachment←'java' ⎕new 'org.apache.commons.mail.EmailAttachment'
      attachment.setPath 'C:\Users\Simon\Pictures\BobPhoto.jpg'
      attachment.setDisposition attachment.ATTACHMENT
      attachment.setDescription 'Photo of Bob'
      attachment.setName 'BobPhoto.jpg'
      email.attach attachment

      ⍝ Send the e-mail
      email.send

Finding out More

For more information, see the documentation which accompanies the Commons Email software:

http://commons.apache.org/email/userguide.html

Author: SimonMarsden


CategoryAplx CategoryJava CategoryEmail

authenticatedemailjava (last edited 2010-02-24 10:47:02 by SimonMarsden)