The ServiceMix Mail component provides support for receiving and sending mails via the enterprise service bus.
You can use Maven servicemix-mail-service-unit archetype to create a service unit:
mvn archetype:create \
-DarchetypeGroupId=org.apache.servicemix.tooling \
-DarchetypeArtifactId=servicemix-mail-service-unit \
-DarchetypeVersion=2010.01 \
-DgroupId=your.group.id \
-DartifactId=your.artifact.id \
-Dversion=your-version
|
<mail:poller service="test:myMailService"
endpoint="pollerEndpoint"
targetService="test:myMailProcessor"
period="10000"
connection="imap://user@testserver:143/INBOX?password=myPass"
deleteProcessedMessages="false"
processOnlyUnseenMessages="true" />
|
|
The poller endpoint will only generate InOnly exchanges. |
|
The DefaultMailMarshaler (used if no other is set) will convert in the following way:
The connection uri has to be specified in the following way:
Template: <protocol>://<user>@<host>[:<port>][/<folder>]?password=<password> OR Template: <protocol>://<host>[:<port>][/<folder>]?user=<user>;password=<password> Example: imap://user@imapserver:143/INBOX?password=mypass pop3://pop3server/INBOX?user=me@myhome.org;password=mypass |
|
<mail:sender service="test:myMailService"
endpoint="senderEndpoint"
sender="no-reply@servicemix.org"
receiver="testreceiver@targethost.com"
connection="smtp://lhein@testserver?password=myPass" />
|
|
The DefaultMailMarshaler (used if no other is set) will convert in the following way:
If there is a preconfigured sender for the endpoint from xbean.xml, it will be used
else if MSG_TAG_FROM is defined in the message properties, then it will be used
else the method getDefaultSender() of the marshaler is invoked
If there is a TO property in the normalized message, then it will be used.
If this property is missing, then the pre-configured receiver will be used.
|
If you are just setting up a mail-bridge (means just a poller connected to a sender for mail forwarding) you will need |
Via this attribute you can provide a map of properties which will be applied to the connection properties. Doing so enables you to
completely configure the connection properties.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mail="http://servicemix.apache.org/mail/1.0"
xmlns:test="http://www.servicemix.org/example"
xmlns:util="http://www.springframework.org/schema/util">
<mail:poller service="test:myMailService"
endpoint="pollerEndpoint"
targetService="test:myMailProcessor"
period="10000"
connection="imap://lhein@testserver:143/INBOX?password=myPass"
deleteProcessedMessages="false"
processOnlyUnseenMessages="true"
customProperties="#customProps"/>
<util:map id="customProps">
<entry key="mail.pop3.forgettopheaders" value="true" />
</util:map>
</beans>
|
For a list of properties used by the SUN implementation influencing the connection have a look at the Mail API documentation:
http://java.sun.com/products/javamail/javadocs/overview-summary.html
Have especially a look at the protocol specific package summaries.
Via this property you can provide a list of properties of the normalized in message which will be set to null
before marshaling it. This will enable you for example to have a simple email forward bridge with a poller and a
sender which will forward the received message to a new email address. Without skipping at least the TO address property
the mail would be sent again to the account you are polling from (see how the receiver is determined).
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mail="http://servicemix.apache.org/mail/1.0"
xmlns:test="http://www.servicemix.org/example"
xmlns:util="http://www.springframework.org/schema/util">
<mail:sender service="test:myMailService"
endpoint="senderEndpoint"
sender="no-reply@servicemix.org"
receiver="testreceiver@targethost.com"
connection="smtp://lhein@testserver?password=myPass"
ignoreMessageProperties="#ignoreProps" />
<util:list id="ignoreProps">
<value>org.apache.servicemix.mail.to</value>
<value>org.apache.servicemix.mail.cc</value>
<value>org.apache.servicemix.mail.bcc</value>
<value>org.apache.servicemix.mail.from</value>
<value>org.apache.servicemix.mail.replyto</value>
</util:list>
</beans>
|
For all xbean file endpoint configuration take a look at Xml schemas
You can write your own marshalers for conversion between mail and normalized message and vice versa.
To do this you simply need to subclass the org.apache.servicemix.mail.marshaler.AbstractMailMarshaler or even the
DefaultMailMarshaler if you don't want to start from scratch.
For providing your own marshaler you only need to implement two methods:
This method is responsible for translating a received mail message into a jbi compliant normalized message ready to be sent to the bus.
This method is responsible for translating a received normalized message into a mail message ready to be sent to the mail server.
After finishing your marshaler you can simply configure your endpoints to use it:
<mail:poller service="test:myMailService"
endpoint="senderEndpoint"
sender="no-reply@servicemix.org"
connection="imap://lhein@testserver:143?password=myPass" >
<property name="marshaler">
<bean class="com.mycompany.MyMailMarshaler" />
</property>
</mail:poller>
|
All mail headers are mapped (nearly) one-to-one into message properties. If there are multiple mail headers with the same name, then the values will be concatenated and separated by the semicolon (;-character).
There are some more important properties which affect the handling of the message / mail (constants are defined in AbstractMailMarshaler):
|
These are the available content types:
On incoming mails you may evaluate this property on the normalized message to find out which kind of mime type the mail had. On outgoing mails you can control the mime type to use for the mail. If you don't set the content type it will be auto-sensed by the marshaler.
If you want to use secure protocols like IMAPS or POP3S you have several oportunities to do that.
The standard way would be to just set the protocol in the connection uri to imaps instead of imap. (same for smtp and pop3)
Java will then try to verify the certificate of the mail server by following the chain of certificates signing the server's certificate back to one of these well known CA certificates specified in the default java keystore file. (see SSLNOTES.txt of JavaMail API for details)
If the certificate has no chain (is self signed) then this chain following is impossible. For this case you have to specify the key store to use via system properties:
java -Djavax.net.ssl.trustStore=$HOME/.keystore ...
To import keys to that store you may use the keytool command.
You have the possibility to write your own TrustManager and let the component use this custom trust manager. If you want no verification of the servers certificate at all then you can use the shipped DummyTrustManager.
|
Using the DummyTrustManager will provide you NO SECURITY AT ALL! |
1. You need to subclass a trust manager which implements the TrustManager interface directly or indirectly. The constructor has to be parameterless.
2. You have to specify the attribute customTrustManagers in the endpoint configuration inside your xbean.xml file. The content will be the full name of the class including the package name (for example: customTrustManagers="com.mycompany.mypackage.MyTrustManager"). If you want to define more then one just use the semicolon ( ; ) to separate the class names.
<mail:poller service="test:myMailService"
endpoint="pollEndpoint"
targetService="test:myMailProcessingService"
period="10000"
connection="imaps://lhein@imapserver/INBOX?password=mypass"
deleteProcessedMessages="false"
processOnlyUnseenMessages="true"
customTrustManagers="org.apache.servicemix.mail.security.DummyTrustManager" />
|
If you are not using a secure protocol the attribute customTrustManager will be ignored at all.
There is a additional attribute you may specify in the endpoints configuration. This attribute is called debugMode and accepts the values "true" or "false". This will switch the JavaMail debug mode on or off providing useful details to you via console or servicemix.log file.
NOTHING DONE YET...FEEL FREE TO HELP US OUT HERE.....TODO
NOTHING DONE YET...FEEL FREE TO HELP US OUT HERE.....TODO