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

RE: Attach File to E-mail (SMTP)

by VinithGowda :: Rate this Message:

Reply to Author | View in Thread

Thanks Wayne Wundram,

Can u provide me the entire config.xml

Vinith Gowda.
Wayne Wundram wrote:
I created a Transformer that takes the content of the message and
attaches it to an email. The code of my transformer is:

 

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

 

package miraculum.mule;

 

import java.io.IOException;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Multipart;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

import org.apache.log4j.Logger;

import org.mule.transformers.AbstractTransformer;

import org.mule.umo.transformer.TransformerException;

import org.mule.util.DateUtils;

 

/**

 *

 * @author Wayne Wundram

 *

 *  Mule Transformer to take the contents of a Mail Message

 *  and send it as an attachment.

 *

 */

public class TfmMailContentsToAttachment extends AbstractTransformer {

    private String attachmentName;

    public static final String DEFAULT_DATE_FORMAT =
"yyyy-MM-dd_HH-mm-ss.SSS";

 

    static Logger log =
Logger.getLogger(TfmMailContentsToAttachment.class.getName());

 

    public TfmMailContentsToAttachment() {

        super();

        this.registerSourceType(Message.class);

        this.setReturnClass(Message.class);

    }

 

    public Object doTransform(Object src, String encoding) throws
TransformerException {

 

        MimeMessage inputMessage = (MimeMessage) src;

 

        // If the attachmentName property is not set then default it.  

        if (getAttachmentName() == null) {

            setAttachmentName("attachment.txt");

        }

        String generatedName = getAttachmentName();

        if (generatedName.indexOf("{DATE}") > -1) {

            String currentDate =
DateUtils.getTimeStamp(DEFAULT_DATE_FORMAT);

            generatedName = generatedName.replace("{DATE}",
currentDate);

            setAttachmentName(generatedName);

        }

         

        // Move content to an attachment.

        try {

            MimeBodyPart mimeBodyPart = new MimeBodyPart();

 
mimeBodyPart.setContent(inputMessage.getContent(),inputMessage.getConten
tType());

            mimeBodyPart.setFileName(getAttachmentName());

            Multipart multipart = new MimeMultipart();

            multipart.addBodyPart(mimeBodyPart);

            inputMessage.setContent(multipart);

 

        } catch (IOException ioe) {

            log.error("IOException " + ioe.getMessage());

            throw new TransformerException(this, ioe);

        } catch (MessagingException me) {

            log.error("MessagingException " + me.getMessage());

            throw new TransformerException(this, me);

 

        }

        return inputMessage;

    }

 

    public String getAttachmentName() {

        return attachmentName;

    }

 

    public void setAttachmentName(String attachmentName) {

        this.attachmentName = attachmentName;

    }

}

 

 

I used it in my config file as follows:

 

            <transformer name="GGMailContentsToAttachment"

                  className="miraculum.mule.TfmMailContentsToAttachment"

                  returnClass="javax.mail.Message">

                  <properties>

                        <property name="attachmentName"
value="GA01-{DATE}.txt" />

                  </properties>

            </transformer>

 

Regards,

Wayne Wundram

 

________________________________

From: VinithGowda [mailto:vinith.hr@i-logicon.co.in]
Sent: Friday, July 04, 2008 7:16 AM
To: user@mule.codehaus.org
Subject: [mule-user] Attach File to E-mail (SMTP)

 

Hi All, I would like to attach a file to the mail sending through SMTP
connector,which is the best way doing it. Thanks in Advance, Vinith
Gowda.

________________________________

View this message in context: Attach File to E-mail (SMTP)
<http://www.nabble.com/Attach-File-to-E-mail-%28SMTP%29-tp18272975p18272
975.html>
Sent from the Mule - User mailing list archive
<http://www.nabble.com/Mule---User-f2727.html>  at Nabble.com.

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