« Return to Thread: Attach File to E-mail (SMTP)
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.getContentType());
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@...]
Sent: Friday, July 04, 2008 7:16
AM
To:
user@...
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)
Sent from the Mule - User mailing list
archive at Nabble.com.
« Return to Thread: Attach File to E-mail (SMTP)
| Free embeddable forum powered by Nabble | Forum Help |