in jelly-tags/email/src/main/java/org/apache/commons/jelly/tags/email/EmailTag.java [143:269]
public void doTag(XMLOutput xmlOutput) throws JellyTagException {
Properties props = new Properties();
props.putAll(context.getVariables());
// if a server was set then configure the system property
if (server != null) {
Object serverInput = this.server.evaluate(context);
props.put("mail.smtp.host", serverInput.toString());
}
else {
if (props.get("mail.smtp.host") == null) {
throw new JellyTagException("no smtp server configured");
}
}
// check if there was no to address
if (to == null) {
throw new JellyTagException("no to address specified");
}
// check if there was no from
if (from == null) {
throw new JellyTagException("no from address specified");
}
String messageBody = null;
if (this.message != null) {
messageBody = this.message.evaluate(context).toString();
}
else {
// get message from body
messageBody = getBodyText(encodeXML);
}
Object fromInput = this.from.evaluate(context);
Object toInput = this.to.evaluate(context);
// configure the mail session
Session session = Session.getDefaultInstance(props, null);
// construct the mime message
MimeMessage msg = new MimeMessage(session);
try {
// set the from address
msg.setFrom(new InternetAddress(fromInput.toString()));
// parse out the to addresses
StringTokenizer st = new StringTokenizer(toInput.toString(), ";");
InternetAddress[] addresses = new InternetAddress[st.countTokens()];
int addressCount = 0;
while (st.hasMoreTokens()) {
addresses[addressCount++] = new InternetAddress(st.nextToken().trim());
}
// set the recipients
msg.setRecipients(Message.RecipientType.TO, addresses);
// parse out the cc addresses
if (cc != null) {
Object ccInput = this.cc.evaluate(context);
st = new StringTokenizer(ccInput.toString(), ";");
InternetAddress[] ccAddresses = new InternetAddress[st.countTokens()];
int ccAddressCount = 0;
while (st.hasMoreTokens()) {
ccAddresses[ccAddressCount++] = new InternetAddress(st.nextToken().trim());
}
// set the cc recipients
msg.setRecipients(Message.RecipientType.CC, ccAddresses);
}
}
catch (AddressException e) {
throw new JellyTagException(e);
}
catch (MessagingException e) {
throw new JellyTagException(e);
}
try {
// set the subject
if (subject != null) {
Object subjectInput = this.subject.evaluate(context);
msg.setSubject(subjectInput.toString());
}
// set a timestamp
msg.setSentDate(new Date());
// if there was no attachment just set the text/body of the message
if (attachment == null) {
msg.setText(messageBody);
}
else {
// attach the multipart mime message
// setup the body
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(messageBody);
// setup the attachment
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(attachment);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
// create the multipart and add its parts
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
msg.setContent(mp);
}
logger.info("sending email to " + to + " using " + server);
// send the email
Transport.send(msg);
}
catch (MessagingException e) {
throw new JellyTagException(e);
}
}