J/Link Tutorial

Sending email from Mathematica

Email in Java is handled by a standard extension package known as the JavaMail API. This functionality is not yet part of the standard Java 2 platform, although it is a standard extension. Sun provides a free implementation.

This example shows how to send email from Mathematica. The specific program developed here was used to send out upgrade notifications for J/Link version 1.1 to users who had requested to be informed of such updates. The code uses a feature introduced in J/Link 1.1 (the MakeJavaObject function), so don't try to use it with an older version.

This example relies on the JavaMail API, which is not a standard part of the Java distribution, so you need to download and install it. There is a link in the in the first sentence above where you can get the JavaMail component. JavaMail, in turn, depends on the JavaBeans Activation Framework, which you can get from http://java.sun.com/products/javabeans/jaf/index.jsp (there is a link to that page on the JavaMail page). These are both small components that you can download and install quickly. They contain installation instructions in their README files, but the basic idea is that you have to make the two jar files, mail.jar and activation.jar, available to J/Link. This can be done either by adding those files to your CLASSPATH environment variable, or by explicitly telling J/Link where to look. Here is an example of the latter:

<<JLink`
InstallJava[]
AddToClassPath["c:\\downloads\\javamail-1.3.2\\mail.jar", "c:\\downloads\\jaf-1.0.2\\activation.jar"]

Then try the example.

Note that AddToClassPath makes additions to the classpath that only hold for the current InstallJava session. If you quit and restart Java or Mathematica, you need to call AddToClassPath again (call it after InstallJava, as above).

Load J/Link and start the connection.

<< JLink`

InstallJava[];

Here is the code for a simple program that sends an email message. This program is almost line-for-line identical with a corresponding Java program. Consult the documentation for JavaMail for more information about the classes and methods that are used here.

\( (*\ To\ use\ this\ program, \ 
      you\ will\ first\ need\ to\ redefine\ these\ two\ variables\ to\ be\ appropriate\ for\ your\ circumstances . \ 
          The\ first\ is\ the\ name\ of\ your\ SMTP\ host, \ 
      and\ the\ second\ is\ your\ username\ on\ that\ \(\(host\)\(.\)\)
*) 
SMTPHost\  = "smtp.enteract.com"; \)
UserName  =  "tgayley"; 

\(SendMail[to_String, \ from_String, \ subject_String, \ 
        body_String]\  := 
JavaBlock[
Module[{props, \ 
            session, \ msg}, 
props = 
            JavaNew["java.util.Properties"]; 
props@
            put[MakeJavaObject["mail.smtp.host"], 
              MakeJavaObject[SMTPHost]]; 
props@
            put[MakeJavaObject["mail.user"], 
              MakeJavaObject[
                UserName]]; 
LoadClass["javax.mail.Session"]; 
session = 
            Session`getDefaultInstance[props, Null]; 
msg = 
            JavaNew["javax.mail.internet.MimeMessage", 
              session]; 
LoadClass["javax.mail.Message$RecipientType"]; 
msg@
            setRecipients[
              Message$RecipientType`TO, {JavaNew["javax.mail.internet.InternetAddress", 
                  to]}]; 
msg@
            setFrom[JavaNew["javax.mail.internet.InternetAddress", 
                from]]; 
msg@
            setSubject[subject]; 
msg@
            setContent[
              MakeJavaObject[
                body], "text/plain"]; 
LoadClass["javax.mail.Transport"]; 
Transport`send[
            msg]
]
]\)

The list of email addresses was extracted (by a Mathematica program, of course) from a text file containing user responses to the small survey on the J/Link download page. This list is called addresses, and looks like {"john@acme.com", "joe@uiuc.edu", ... }. The variable msg is a string containing the contents of the announcement.

Here we run the program for every name on the list:

Scan[
      SendMail[#,  "J/Link <jlink@wolfram.com>",  "Announcing J/Link \
Version 1.1",  msg] &,  addresses]