You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by mo...@apache.org on 2003/01/26 02:13:47 UTC
cvs commit: jakarta-commons-sandbox/jelly/jelly-tags/email/src/java/org/apache/commons/jelly/tags/email EmailTag.java
morgand 2003/01/25 17:13:47
Modified: jelly/jelly-tags/email/src/java/org/apache/commons/jelly/tags/email
EmailTag.java
Log:
converted email taglib from Exception to JellyTagException
Revision Changes Path
1.2 +79 -64 jakarta-commons-sandbox/jelly/jelly-tags/email/src/java/org/apache/commons/jelly/tags/email/EmailTag.java
Index: EmailTag.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/jelly/jelly-tags/email/src/java/org/apache/commons/jelly/tags/email/EmailTag.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- EmailTag.java 8 Jan 2003 04:47:48 -0000 1.1
+++ EmailTag.java 26 Jan 2003 01:13:47 -0000 1.2
@@ -63,14 +63,16 @@
import org.apache.commons.jelly.TagSupport;
import org.apache.commons.jelly.XMLOutput;
-import org.apache.commons.jelly.JellyException;
+import org.apache.commons.jelly.JellyTagException;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import javax.mail.Session;
import javax.mail.Message;
+import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Transport;
+import javax.mail.internet.AddressException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
@@ -184,7 +186,7 @@
/**
* Execute the tag
*/
- public void doTag(XMLOutput xmlOutput) throws Exception {
+ public void doTag(XMLOutput xmlOutput) throws JellyTagException {
Properties props = new Properties();
props.putAll(context.getVariables());
@@ -194,18 +196,18 @@
}
else {
if (props.get("mail.smtp.host") == null) {
- throw new JellyException("no smtp server configured");
+ throw new JellyTagException("no smtp server configured");
}
}
// check if there was no to address
if (to == null) {
- throw new JellyException("no to address specified");
+ throw new JellyTagException("no to address specified");
}
// check if there was no from
if (from == null) {
- throw new JellyException("no from address specified");
+ throw new JellyTagException("no from address specified");
}
// get the messageBody from the message attribute or from the tag body
@@ -224,74 +226,87 @@
// construct the mime message
MimeMessage msg = new MimeMessage(session);
-
- // set the from address
- msg.setFrom(new InternetAddress(from));
-
- // parse out the to addresses
- StringTokenizer st = new StringTokenizer(to, ";");
- 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) {
- st = new StringTokenizer(cc, ";");
- InternetAddress[] ccAddresses = new InternetAddress[st.countTokens()];
- int ccAddressCount = 0;
+
+ try {
+ // set the from address
+ msg.setFrom(new InternetAddress(from));
+
+ // parse out the to addresses
+ StringTokenizer st = new StringTokenizer(to, ";");
+ InternetAddress[] addresses = new InternetAddress[st.countTokens()];
+ int addressCount = 0;
while (st.hasMoreTokens()) {
- ccAddresses[ccAddressCount++] = new InternetAddress(st.nextToken().trim());
+ addresses[addressCount++] = new InternetAddress(st.nextToken().trim());
}
- // set the cc recipients
- msg.setRecipients(Message.RecipientType.CC, ccAddresses);
- }
+ // set the recipients
+ msg.setRecipients(Message.RecipientType.TO, addresses);
+
+ // parse out the cc addresses
+ if (cc != null) {
+ st = new StringTokenizer(cc, ";");
+ InternetAddress[] ccAddresses = new InternetAddress[st.countTokens()];
+ int ccAddressCount = 0;
+ while (st.hasMoreTokens()) {
+ ccAddresses[ccAddressCount++] = new InternetAddress(st.nextToken().trim());
+ }
- // set the subject
- if (subject != null) {
- msg.setSubject(subject);
+ // set the cc recipients
+ msg.setRecipients(Message.RecipientType.CC, ccAddresses);
+ }
+ }
+ catch (AddressException e) {
+ throw new JellyTagException(e);
+ }
+ catch (MessagingException e) {
+ throw new JellyTagException(e);
}
- // set a timestamp
- msg.setSentDate(new Date());
+ try {
+ // set the subject
+ if (subject != null) {
+ msg.setSubject(subject);
+ }
- // if there was no attachment just set the text/body of the message
- if (attachment == null) {
+ // set a timestamp
+ msg.setSentDate(new Date());
- msg.setText(messageBody);
+ // if there was no attachment just set the text/body of the message
+ if (attachment == null) {
- }
- else {
+ msg.setText(messageBody);
- // attach the multipart mime message
- // setup the body
- MimeBodyPart mbp1 = new MimeBodyPart();
- mbp1.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);
- // setup the attachment
- MimeBodyPart mbp2 = new MimeBodyPart();
- FileDataSource fds = new FileDataSource(attachment);
- mbp2.setDataHandler(new DataHandler(fds));
- mbp2.setFileName(fds.getName());
+ msg.setContent(mp);
- // 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);
}
-
- logger.info("sending email to " + to + " using " + server);
-
- // send the email
- Transport.send(msg);
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>