[Yanel-commits] rev 53150 - in
public/yanel/trunk/src/core/java/org/wyona/yanel/core: . util
michi at wyona.com
michi at wyona.com
Sun Sep 12 12:18:03 CEST 2010
Author: michi
Date: 2010-09-12 12:18:03 +0200 (Sun, 12 Sep 2010)
New Revision: 53150
Modified:
public/yanel/trunk/src/core/java/org/wyona/yanel/core/Yanel.java
public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/MailUtil.java
Log:
smtp authentication implemented
Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/Yanel.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/Yanel.java 2010-09-11 21:09:22 UTC (rev 53149)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/Yanel.java 2010-09-12 10:18:03 UTC (rev 53150)
@@ -58,6 +58,8 @@
private String reservedPrefix = null;
private boolean schedulerEnabled;
+ private String smtpUsername, smtpPassword;
+
private static Logger log = Logger.getLogger(Yanel.class);
/**
@@ -110,11 +112,23 @@
smtpHost = config.getChild("smtp").getAttribute("host");
+ // INFO: SMTP Authentication (optional), which is normally necessary in order to relay messages to other hosts/domains
+ smtpUsername = config.getChild("smtp").getAttribute("username", null);
+ smtpPassword = config.getChild("smtp").getAttribute("password", null);
+
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", smtpPortSt);
// http://java.sun.com/products/javamail/javadocs/javax/mail/Session.html
- javax.mail.Session session = javax.mail.Session.getDefaultInstance(props, null);
+ javax.mail.Session session = null;
+ if (smtpUsername != null && smtpPassword != null) {
+ log.info("SMTP authentication enabled using username: " + smtpUsername);
+ props.put("mail.smtp.auth", "true");
+ session = javax.mail.Session.getDefaultInstance(props, new YanelMailAuthenticator(smtpUsername, smtpPassword));
+ } else {
+ log.info("No SMTP authentication configured.");
+ session = javax.mail.Session.getDefaultInstance(props, null);
+ }
log.info("Mailserver default session (available to all code executing in the same JVM): " + session.getProperty("mail.smtp.host") + ":" + session.getProperty("mail.smtp.port"));
} else {
log.warn("Mail server not configured within configuration: " + configFile);
@@ -136,7 +150,10 @@
}
}
}
-
+
+ /**
+ *
+ */
public static Yanel getInstance() throws Exception {
if (yanel == null) {
yanel = new Yanel();
@@ -263,9 +280,42 @@
}
/**
- * Check if scheduler is enabled
+ * Check whether scheduler is enabled
*/
public boolean isSchedulerEnabled() {
return schedulerEnabled;
}
+
+ /**
+ * Check whether SMTP authentication is set
+ */
+ public boolean isSMTPAuthSet() {
+ if (smtpUsername != null && smtpPassword != null) {
+ return true;
+ }
+ return false;
+ }
}
+
+/**
+ * Simple authenticator used for SMTP
+ */
+class YanelMailAuthenticator extends javax.mail.Authenticator {
+
+ private String username, password;
+
+ /**
+ *
+ */
+ public YanelMailAuthenticator(String username, String password) {
+ this.username = username;
+ this.password = password;
+ }
+
+ /**
+ * @see javax.mail.Authenticator#getPasswordAuthentication()
+ */
+ protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
+ return new javax.mail.PasswordAuthentication(this.username, this.password);
+ }
+}
Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/MailUtil.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/MailUtil.java 2010-09-11 21:09:22 UTC (rev 53149)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/MailUtil.java 2010-09-12 10:18:03 UTC (rev 53150)
@@ -11,7 +11,7 @@
import org.apache.log4j.Logger;
/**
- *
+ * SMTP mail utility class
*/
public class MailUtil {
private static Logger log = Logger.getLogger(MailUtil.class);
@@ -70,12 +70,23 @@
props.put("mail.smtp.port", "" + smtpPort);
session = Session.getInstance(props, null);
log.warn("Use specific mail session: " + session.getProperty("mail.smtp.host") + ":" + session.getProperty("mail.smtp.port"));
- } else {
+ } else { // INFO: Use the global configuration (default instance) of Yanel
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", "mail.foo.bar"); // Dummy value
props.put("mail.smtp.port", "37"); // Dummy value
- session = Session.getDefaultInstance(props, null); // INFO: The dummy values will be ignored, because Yanel (org.wyona.yanel.core.Yanel) sets during initialization the default session!
- log.warn("Use default mail session: " + session.getProperty("mail.smtp.host") + ":" + session.getProperty("mail.smtp.port"));
+ boolean smtpAuthEnabled = false;
+ try {
+ smtpAuthEnabled = org.wyona.yanel.core.Yanel.getInstance().isSMTPAuthSet();
+ } catch (Exception e) {
+ log.error(e, e);
+ }
+ if (smtpAuthEnabled) {
+ log.info("SMTP authentication enabled.");
+ session = Session.getDefaultInstance(props, new DummyAuthenticator()); // INFO: The dummy values will be ignored, because Yanel (org.wyona.yanel.core.Yanel) sets during initialization the default session!
+ } else {
+ session = Session.getDefaultInstance(props, null); // INFO: The dummy values will be ignored, because Yanel (org.wyona.yanel.core.Yanel) sets during initialization the default session!
+ }
+ log.info("Use default mail session: " + session.getProperty("mail.smtp.host") + ":" + session.getProperty("mail.smtp.port"));
}
// Construct the message
@@ -94,3 +105,26 @@
Transport.send(msg);
}
}
+
+/**
+ * Dummy authenticator used for SMTP
+ */
+class DummyAuthenticator extends javax.mail.Authenticator {
+
+ private String username, password;
+
+ /**
+ *
+ */
+ public DummyAuthenticator() {
+ this.username = "foo";
+ this.password = "bar";
+ }
+
+ /**
+ * @see javax.mail.Authenticator#getPasswordAuthentication()
+ */
+ protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
+ return new javax.mail.PasswordAuthentication(this.username, this.password);
+ }
+}
More information about the Yanel-commits
mailing list