« Return to Thread: Attach File to E-mail (SMTP)

Re: Attach File to E-mail (SMTP)

by yantzefr :: Rate this Message:

Reply to Author | View in Thread

Hi,

I found a solution which consists in creating one Transformer upstream to the call to ObjectToMimeMessage.
The role of this Transform it will be to add the attachment document and to put the contents body of the email.

Mule config file :

....
<connector name="EmailConnector" className="org.mule.providers.email.SmtpConnector">
   <properties>
       <property name="host" value="${SMTP_HOST}" />
       <property name="port" value="${SMTP_PORT}" />
       <property name="username" value="${SMTP_USER}" />
       <property name="password" value="${SMTP_PASSWORD}" />
       <property name="subject" value="${SMTP_SUBJECT}"/>
       <property name="fromAddress" value="${SMTP_FROM_ADDRESS}"/>
   </properties>
</connector>

<transformers>
        <transformer name="FileToAttachedFileTransformer" className="com.gb.ram.common.mule.transformer.FileToAttachedFile" />
        <transformer name="ObjectToMimeMessageTransformer" className="org.mule.providers.email.transformers.ObjectToMimeMessage" returnClass="javax.mail.Message" />
....
</transformers>
....

<mule-descriptor name="SendEmailWithAttachmentFile" implementation="com.gb.ram.mule.component.DummyComponent">
....
....

<outbound-router>
    <router className="org.mule.routing.outbound.OutboundPassThroughRouter">
        <endpoint address="smtp://${SMTP_HOST}" connector="EmailConnector" transformers="FileToAttachedFileTransformer ObjectToMimeMessageTransformer">
        <properties>
                        <property name="toAddresses" value="${SMTP_TO_ADDRESS}" />
                        <property name="contentType" value="text/plain" />
                </properties>
      </endpoint>
   </router>
</outbound-router>
</mule-descriptor>
...
...


Code of the "DummyComponent" : it do nothing

public class DummyComponent {
        /**
         * @param contenu
         * @return
         */
        public Object contenu(Object content) {
                System.out.println("*-*-*-*-*-*-*-* DummyComponent : " + content);
            return contenu;
        }

Code of the FileToAttachedFile Transformer :

public class FileToAttachedFile extends AbstractEventAwareTransformer {

        private final String BODY_EMAIL = "Hello,\r\n\r\n\r\nI'm very happy!!";
       
        public Object transform(Object source, String encoding, UMOEventContext context) throws TransformerException {
               
                UMOMessage message = context.getMessage();
               
                try {
                        //attachment file "payload"
                        String pathFile = message.getUniqueId();
                        FileDataSource attachFile = new FileDataSource(pathFile);

                        //attachment external file
                        //FileDataSource attachFile = new FileDataSource("<path_string_file>");


                        message.addAttachment("firstAttachmentFile", new DataHandler(attachFile));
                       
                        return BODY_EMAIL;

                } catch (Exception e) {
                        Message messageErreur = MessageFactory.createStaticMessage("Error FileToAttachedFile");
                        throw new TransformerException(messageErreur, e);
                }
        }

I'm testing this code and it's running fine


 « Return to Thread: Attach File to E-mail (SMTP)