Creating a Request
A QueueConnection, a QueueSession and a QueueSender to the queue "mailout" are a precondition to send a mailrequest.
To send a mail, a request in kind of a TextMessage is created:
TextMessage request = session.createTextMessage();
|
Now, the necessary properties are set within the message. These are:
Name | Type | Description |
|
from | String | Contains the 'from' address. Overwrites the default 'from' address mentioned within the Extension Swiftlet. |
|
to | String | contains the 'to' address. Overwrites the default 'to' address mentioned within the Extension Swiftlet. |
|
cc | String | Contains the 'cc' address. Overwrites the default 'cc' address mentioned within the Extension Swiftlet. |
|
replyto | String | Contains the 'replyto' address. Overwrites the default 'replyto' address mentioned within the Extension Swiftlet. |
|
subject | String | Contains the subject. |
|
The mail body is set by setText(). Now, the TextMessage can be sent.
Note: 'from' and 'to' are optional if an appropriate default address is defined within the Swiftlet configuration. 'cc' and 'replyto' are optional. 'subject' and the mail body are mandatory.
Example:
QueueSender sender = session.createSender(mailQueue); TextMessage msg = session.createTextMessage(); msg.setStringProperty("from","registrar@acme.com"); msg.setStringProperty("to","admin@acme.com"); msg.setStringProperty("replyto","registrar@acme.com"); msg.setStringProperty("cc","archive@acme.com"); msg.setStringProperty("subject","Registration Status"); msg.setText("Number of registrations today: "+cnt); sender.send(msg);
|
Receiving a Receipt
Optional, a receipt is sent back as TextMessage by the SMTP Mailer Extension Swiftlet if JMSReplyTo is set within the request. The receipt contains the sending status in the boolean property 'status'. In the case of 'true' the mail was sent successfully. Here, the long property "deliverytime" contains the sending time. In the case of 'false' the message body contains an exception text.
Example:
QueueSender sender = session.createSender(mailQueue); TextMessage msg = session.createTextMessage(); TemporaryQueue responseQueue = session.createTemporaryQueue(); msg.setJMSReplyTo(responseQueue); msg.setStringProperty("from","registrar@acme.com"); msg.setStringProperty("to","admin@acme.com"); msg.setStringProperty("replyto","registrar@acme.com"); msg.setStringProperty("cc","archive@acme.com"); msg.setStringProperty("subject","Registration Status"); msg.setText("Number of registrations today: "+cnt); sender.send(msg); QueueReceiver receiver = session.createReceiver(responseQueue); TextMessage response = (TextMessage)receiver.receive(); if (response.getBooleanProperty("status")) System.out.println("Successfully sent, deliveryTime: "+ formatter.format(new Date(response.getLongProperty("deliverytime")))); else System.out.println("Sent failed, reason: "+response.getText());
|
Example
An example program "TestMailer.java" exists within the 'sample' directory of this distribution which reads the necessary properties from System.in, sends a mail, receives a reply and outputs the result.
Note that the program does a JNDI lookup on "mailout". Thus, it requires an JNDI alias for the real queue. The alias is defined automatically if you use the "install.cli" script. If you install the Swiftlet manually, you need to define the alias by yourself.