JavaMail Service Provider for the Exchange server
This page shows how to implement and use a JavaMail service provider connecting to the MS Exchange server. The implementation uses the Java Bridge to Exchange product.Download and setup the service provider
The binary and source code of the service provider can be downloaded here. The download package contains a folder named "JavaMail" where you'll find the required JAR files. Ensure that the two JAR files "jbex-v1.4.8-eval.jar" and "jbex-javamail.jar" are included in your application's classpath.The registry entries for this service provider are:
protocol=jbexStore; type=store; class=com.moyosoft.exchange.javamail.JbexStore;
protocol=jbexTransport; type=transport; class=com.moyosoft.exchange.javamail.JbexTransport;
Please place those entries in your "javamail.providers" configuration file. For more information, have a look at the JavaMail documentation.protocol=jbexTransport; type=transport; class=com.moyosoft.exchange.javamail.JbexTransport;
Using the service provider
To access the store and folders containing messages, the method Session.getStore("jbexStore") is used as illustrated
in the following code snippet:
// Create a session
Session session = Session.getDefaultInstance(new Properties());
// Create and connect to the store
Store store = session.getStore("jbexStore");
store.connect(hostname, username, password);
// Open the Inbox folder
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
// List messages
int count = inbox.getMessageCount();
System.out.println("Messages count in inbox: " + count);
for(int i=1; i <= Math.min(count, 10); i++)
{
Message message = inbox.getMessage(i);
System.out.println("Message " + i + ": " + message.getSubject());
}
// Disconnect
inbox.close(true);
store.close();
To send messages with JavaMail, you need to create a Transport object. To create the Exchange Transport object, use the method Session.getTransport("jbexTransport") as shown here:
Session session = Session.getDefaultInstance(new Properties());
// Create and connect to the store
Store store = session.getStore("jbexStore");
store.connect(hostname, username, password);
// Open the Inbox folder
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
// List messages
int count = inbox.getMessageCount();
System.out.println("Messages count in inbox: " + count);
for(int i=1; i <= Math.min(count, 10); i++)
{
Message message = inbox.getMessage(i);
System.out.println("Message " + i + ": " + message.getSubject());
}
// Disconnect
inbox.close(true);
store.close();
// Create a session
Session session = Session.getDefaultInstance(new Properties());
// Create and connect to the transport
Transport transport = session.getTransport("jbexTransport");
transport.connect(hostname, username, password);
// Recipients
Address[] recipients = new Address[] {new InternetAddress("test@example.com")};
// Create a message
Message msg = new MimeMessage(session);
msg.setRecipients(Message.RecipientType.TO, recipients);
msg.setSubject("Test message");
msg.setContent("Hello from JavaMail", "text/plain");
msg.saveChanges();
// Send the message
transport.sendMessage(msg, recipients);
// Disconnect
transport.close();
Session session = Session.getDefaultInstance(new Properties());
// Create and connect to the transport
Transport transport = session.getTransport("jbexTransport");
transport.connect(hostname, username, password);
// Recipients
Address[] recipients = new Address[] {new InternetAddress("test@example.com")};
// Create a message
Message msg = new MimeMessage(session);
msg.setRecipients(Message.RecipientType.TO, recipients);
msg.setSubject("Test message");
msg.setContent("Hello from JavaMail", "text/plain");
msg.saveChanges();
// Send the message
transport.sendMessage(msg, recipients);
// Disconnect
transport.close();
Transport implementation
To create a JavaMail transport provider, the abstract class Transport has to be inherited and implemented.Below is shown a simple Transport implementation snippet used to send an email through the Exchange server:
public class JbexTransport extends Transport
{
private Exchange exchange;
public JbexTransport(Session session, URLName urlname)
{
super(session, urlname);
}
protected boolean protocolConnect(String host, int port, String user, String password) throws MessagingException
{
try
{
exchange = new Exchange(host, user, password);
}
catch(ExchangeServiceException e)
{
handleError(e);
}
return true;
}
public void sendMessage(Message msg, Address[] addresses) throws MessagingException
{
try
{
ExchangeMail mail = createMail(msg, addresses);
if(mail == null)
{
throw new MessagingException("Unable to create the message");
}
mail.send();
}
catch(ExchangeServiceException e)
{
handleError(e);
}
}
private ExchangeMail createMail(Message msg, Address[] addresses) throws ExchangeServiceException, MessagingException
{
...
}
}
{
private Exchange exchange;
public JbexTransport(Session session, URLName urlname)
{
super(session, urlname);
}
protected boolean protocolConnect(String host, int port, String user, String password) throws MessagingException
{
try
{
exchange = new Exchange(host, user, password);
}
catch(ExchangeServiceException e)
{
handleError(e);
}
return true;
}
public void sendMessage(Message msg, Address[] addresses) throws MessagingException
{
try
{
ExchangeMail mail = createMail(msg, addresses);
if(mail == null)
{
throw new MessagingException("Unable to create the message");
}
mail.send();
}
catch(ExchangeServiceException e)
{
handleError(e);
}
}
private ExchangeMail createMail(Message msg, Address[] addresses) throws ExchangeServiceException, MessagingException
{
...
}
}
Store and Folder access implementation
The Store and Folder abstract classes provide a base for implementing a JavaMail Store able to read message from the Exchange folders.Below is a simple Store implementation demonstrating this:
public class JbexStore extends Store
{
private Exchange exchange;
public JbexStore(Session session, URLName urlname)
{
super(session, urlname);
}
protected boolean protocolConnect(String host, int port, String user, String password) throws MessagingException
{
try
{
exchange = new Exchange(host, user, password);
}
catch(ExchangeServiceException e)
{
handleError(e);
}
return true;
}
public Folder getDefaultFolder() throws MessagingException
{
try
{
return new JbexFolder(this, exchange.getTopFolder());
}
catch(ExchangeServiceException e)
{
handleError(e);
}
return new JbexFolder(this, null);
}
public Folder getFolder(String name) throws MessagingException
{
try
{
if("INBOX".equals(name))
{
return new JbexFolder(this, exchange.getInboxFolder());
}
else
{
return new JbexFolder(this, exchange.getTopFolder(name));
}
}
catch(ExchangeServiceException e)
{
handleError(e);
}
return new JbexFolder(this, null);
}
public Folder getFolder(URLName urlName) throws MessagingException
{
try
{
return getFolder(urlName.getURL().getPath());
}
catch(MalformedURLException ex)
{
throw new MessagingException(ex.getMessage(), ex);
}
}
private void handleError(ExchangeServiceException e) throws MessagingException
{
throw new MessagingException(e.toString(), e);
}
}
{
private Exchange exchange;
public JbexStore(Session session, URLName urlname)
{
super(session, urlname);
}
protected boolean protocolConnect(String host, int port, String user, String password) throws MessagingException
{
try
{
exchange = new Exchange(host, user, password);
}
catch(ExchangeServiceException e)
{
handleError(e);
}
return true;
}
public Folder getDefaultFolder() throws MessagingException
{
try
{
return new JbexFolder(this, exchange.getTopFolder());
}
catch(ExchangeServiceException e)
{
handleError(e);
}
return new JbexFolder(this, null);
}
public Folder getFolder(String name) throws MessagingException
{
try
{
if("INBOX".equals(name))
{
return new JbexFolder(this, exchange.getInboxFolder());
}
else
{
return new JbexFolder(this, exchange.getTopFolder(name));
}
}
catch(ExchangeServiceException e)
{
handleError(e);
}
return new JbexFolder(this, null);
}
public Folder getFolder(URLName urlName) throws MessagingException
{
try
{
return getFolder(urlName.getURL().getPath());
}
catch(MalformedURLException ex)
{
throw new MessagingException(ex.getMessage(), ex);
}
}
private void handleError(ExchangeServiceException e) throws MessagingException
{
throw new MessagingException(e.toString(), e);
}
}
Please download and look at the provider source code for a complete implementation.