How do you send email from your z/OS system?
Java on z/OS makes this very easy. Java on z/OS can use Java libraries developed for other platforms which means there are powerful, free libraries available and plenty of documentation available via Google.
Instead of setting up a SMTP server on z/OS, you can define a user (or more than one) for z/OS in your corporate mail system, and create batch jobs that log in and send mail just like any other email user.
This example uses the JavaMail package (part of Java EE and available for download for Java SE) to send email from z/OS. It uses the JZOS Batch Launcher so you can define the input using JCL DD statements.
You can send an email using JCL like this:
//JAVAG EXEC PROC=JAVAG,
// JAVACLS='''ZMail''',
// CLASPATH='''java/lib/javax.mail.jar'''
//TO DD *
someone@example.com
//SUBJECT DD *
Test Message from batch job
//MESSAGE DD *
Message line 1
Message line 2
...
Message Line n
//
The JCL uses the JAVAG JCL PROC described in this post: Systems Programmer Friendly Java
The following code is the complete Java program. The sample uses Gmail as an example to show how to use TLS and SMTP authentication. Customize the Properties
used to create the Session
to suit your own mail server.
To, Subject and the Message Body are read from DD statements defined in the JCL. You could use the same pattern to allow other information to be input from DD statements, e.g. CC, BCC, From, or even the properties used to log in to the mail server.
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import com.ibm.jzos.ZFile;
import com.ibm.jzos.ZFileException;
public class ZMail
{
public static void main(String[] args)
throws IOException, MessagingException
{
List<String> messagelines = readLinesFromDD("MESSAGE");
String subject = readLinesFromDD("SUBJECT").get(0);
List<String> recipients = readLinesFromDD("TO");
StringBuilder messagetext = new StringBuilder();
for (String line : messagelines)
{
messagetext.append(line);
messagetext.append(System.lineSeparator());
}
final String username = "smtp-username";
final String password = "smtp-password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.ssl.checkserveridentity", "true");
Session session = Session.getInstance(props,
new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("me@example.com"));
for (String recipient : recipients)
{
message.addRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipient));
}
message.setSubject(subject);
message.setText(messagetext.toString());
Transport.send(message);
}
private static List<String> readLinesFromDD(String dd)
throws ZFileException, IOException
{
List<String> lines = new ArrayList<String>();
ZFile input = null;
try
{
input = new ZFile("//DD:" + dd, "rt");
BufferedReader reader =
new BufferedReader(
new InputStreamReader(input.getInputStream()));
String line;
while ((line = reader.readLine()) != null)
{
lines.add(line);
}
}
finally
{
if (input != null)
{
input.close();
}
}
return lines;
}
}