Enhance mail transport rules for picking the main part, and provide a way to handle alternative parts gracefully

Issue #15 resolved
Asankha Perera
repo owner created an issue

Comments (3)

  1. Asankha Perera reporter

    The rules for the identification of the main part of a multipart message is now upto the user to specify. This could be defined at the proxy service level, or at the mail transport listener level as

    <u:property name="mainPartIdentificationPriority" value=" text/plain, text/html"/>

    The value is a comma separated list of content types by priority, with the first being highest priority. The body part with the highest priority is selected as the main body part. According to the above rule, a "text/plain" part is selected as the main part over text/html or any other like text/xml etc.

    The new implementation also drops alternative parts, or parts without a name. For example, if someone considers a text/xml part as the main part (say a SOAP message), any text/plain and/or text/html alternatives are dropped altogether. By setting the following attribute to true at the proxy service or mail transport listener level, one would be able to retain these as "unnamed-attachment-<n>" etc.

    <u:property name="retainAlternativeParts" value="true"/>

    The updated code fully supports, and retains HTML mail with correct line separation etc.

  2. Asankha Perera reporter

    Here is a sample configuration file that fetches mail from Gmail, saves attachments into the file system, and forwards the email to another address

    <bean id="incoming-mail-listener" class="org.adroitlogic.ultraesb.transport.mail.MailTransportListener">
        <property name="properties">
                <entry key="mail.pop3.socketFactory.fallback"  value="false"/>
                <entry key="mail.pop3.host" value="pop.gmail.com"/>
                <entry key="mail.pop3.socketFactory.class" value="javax.net.ssl.SSLSocketFactory"/>
                <entry key="mail.pop3.socketFactory.port" value="995"/>
                <entry key="mail.pop3.port" value="995"/>
                <!--<entry key="removeHeaders" value="RemoveHeader, AnotherHeaderToRemove"/>-->
                <!--<entry key="preserveHeaders" value="To, Subject, From"/>-->
    <bean id="mail-sender" class="org.adroitlogic.ultraesb.transport.mail.MailTransportSender">
        <property name="properties">
                <entry key="mail.smtp.host" value="smtp.gmail.com"/>                <!--REPLACE THIS-->
                <entry key="mail.smtp.port" value="587"/>                           <!--REPLACE THIS-->
                <entry key="mail.smtp.user" value="mail.maildemo.demo"/>            <!--REPLACE THIS-->
                <entry key="mail.smtp.password" value="password"/>                  <!--REPLACE THIS-->
                <entry key="mail.smtp.from" value="mail.maildemo.demo@gmail.com"/>  <!--REPLACE THIS-->
                <entry key="mail.smtp.starttls.enable" value="true"/>               <!--REPLACE THIS-->
                <entry key="mail.smtp.auth" value="true"/>                          <!--REPLACE THIS-->
    <u:proxy id="mail-proxy">
        <u:transport id="incoming-mail-listener">
            <u:property name="url" value="mail.maildemo.demo@gmail.com"/>               <!-- REPLACE THIS -->
            <u:property name="mail.pop3.password" value="password"/>                    <!-- REPLACE THIS -->
            <u:property name="startDelay" value="1000"/>
            <u:property name="repeatInterval" value="10000"/>
            <!--<u:property name="concurrent" value="true"/>-->
            <!--<u:property name="cronExpression" value="0/20/40 * * ? * MON-FRI"/>-->
            <u:property name="mail.pop3.user" value="mail.maildemo.demo"/>
            <u:property name="mainPartIdentificationPriority" value=" text/plain, text/html"/>
            <!--<u:property name="retainAlternativeParts" value="true"/>-->
            <!--<u:property name="removeHeaders" value="RemoveHeader"/>-->
            <!--<u:property name="preserveHeaders" value="To, Subject, From"/>-->
                    String subject = msg.getFirstTransportHeader("Subject");
                    logger.info("Got mail with Subject : " + subject + " and message : \n" + mediation.readPayloadAsString(msg));
                    java.util.Map<String, MessageFormat> attachments = msg.getAttachments();
                    for (java.util.Map.Entry<String, MessageFormat> entry : attachments.entrySet()) {
                        if (entry.getKey() != null) {
                            logger.info("Saving attachment : {}", entry.getKey());
                            entry.getValue().writeTo(new java.io.FileOutputStream("/tmp/" + entry.getKey()));
                    msg.addMessageProperty("subject", "PROCESSED - " + subject);
                    msg.addMessageProperty("from", "UltraESB<mail.maildemo.demo@gmail.com>");
                    msg.setDestinationURL("mailto:asankha@gmail.com");           // <-- REPLACE THIS
                <u:address type="default"/>
  3. Log in to comment