ATS : Mail and SMTP operations

Introduction


This page shows how to manipulate and send Mime messages through SMTP.



Creating and editing a MIME package

Creating an empty MIME package

You can create a Mime package from scratch.

import com.axway.ats.action.objects.MimePackage;

MimePackage mail = new MimePackage();

Loading a MIME package from a database

You can load the Mime package from a database.

When loading a message from a database, ATS expects a specific format of the table with the messages.

Please contact our team if you intend to use this feature.

// The host from which the packages are loaded is read from a message box instance
MimePackage mail = PackageLoader.loadMimePackageFromDb( 123456 );

// or you can specify the Message Box to use
MimePackage mail = PackageLoader.loadMimePackageFromDb( 123456, "some Message Box name" );



Editing a MIME package. Use one of the many "setter" methods

Once you have the Mime package object, you can use a variety of methods that modify its content.
The list of methods is very large, so please use the auto-complete feature of Eclipse to see the current options.

mail.setSubject( "A test message" );
mail.setRecipient( "recipient@test.com" );
mail.setHeader( "MY-CUSTOM-HEADER-NAME", "MY-CUSTOM-HEADER-VALUE" );
mail.addAttachment( "C:/attachment.txt" );



Sending a MIME package

When the mail is ready, you can send it over one of the supported mail senders

import com.axway.ats.action.mail.MailSender;

// Create the mail sender object
MailSender mailSender = new MailSender();

// Send the message
mailSender.send( mail );

Sending a MIME package over TLS

If you want to send the mail securely(over TLS) you need to use the TLS mail sender.

import com.axway.ats.action.mail.MailTlsSender

// Create the mail TLS sender object
MailSender mailSender = new MailTlsSender();

// Send the message
mailSender.send( mail );

The MailTlsSender comes with a built-in JavaMail trust manager which does not perform validation of the server's certificate i.e. it trusts everybody.



Signing a MIME package

If you want to sign the mail prior to sending, here is some example code:

// Create the signer
PackageEncryptor signer = new SMimePackageEncryptor( "<keystore/certificate (chain) file>", "<keystore file password>", "<key pair alias or CN (Common Name) if there is no private key>");

// Set a specific signature algorithm
// signer.setSignatureAlgorithm( SMimePackageEncryptor.SignatureAlgorithm.SHA256_with_RSA );

// Set the signer
mailSender.setSigner( signer );

// Send the message
// Now the message will be signed prior to sending
mailSender.send( mail );

The message is signed prior to it being sent. The message will be encrypted as well, it is first signed.



Encrypting a MIME package

If you want to encrypt the mail prior to sending, here is some example code:

// Create the encriptor
PackageEncryptor encryptor =  new SMimePackageEncryptor( "<keystore file/certificate (chain)>", "<keystore file password>", "<key pair alias(es) or CNs (Common Name) if there is no private key>");

// Set a specific encryption cipher
// encryptor.setEncryptionCipher( SMimePackageEncryptor.Cipher.AES128_CBC );

// Set the encryptor
mailSender.setEncryptor( encryptor );

// Send the message
// Now the message will be encrypted prior to sending
mailSender.send( mail );

The message is encrypted prior to it being sent.



SMTP Operations



If you want you can open SMTP connections and send direct SMTP commands. Here is a simple example:

// get connected to a SMTP host
SmtpManager smtpMgr = new SmtpManager();
int connectionId = smtpMgr.openConnection( <SMTP host>, <SMTP port> );
SmtpConnection smtpConnection = smtpObject.getConnection( connectionId );

// use the established SMTP connection
smtpConnection.helo( "test.com" );
smtpConnection.mailFrom( "user@test.com", null );
smtpConnection.rcptTo( address, null );
smtpConnection.data( "Subject: Test 123 \nTo: " + address + " \nTest in body" );
smtpConnection.finishData();

// release the allocated resources
smtpMgr.closeConnection( connectionId  );

You can also open and close more than one connection at once. For the full list of supported operations please refer to the java documentation.



Back to parent page

Go to Table of Contents