SendEmailPlugin.java [plain text]
package org.blojsom.plugin.email;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.blojsom.blog.Blog;
import org.blojsom.blog.BlogEntry;
import org.blojsom.blog.BlogUser;
import org.blojsom.blog.BlojsomConfiguration;
import org.blojsom.plugin.BlojsomPlugin;
import org.blojsom.plugin.BlojsomPluginException;
import org.blojsom.util.BlojsomUtils;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class SendEmailPlugin implements BlojsomPlugin, EmailConstants {
private Log _logger = LogFactory.getLog(SendEmailPlugin.class);
private Session _mailsession = null;
public void init(ServletConfig servletConfig, BlojsomConfiguration blojsomConfiguration) throws BlojsomPluginException {
String hostname = servletConfig.getInitParameter(SMTPSERVER_IP);
if (hostname != null) {
if (hostname.startsWith("java:comp/env")) {
try {
Context context = new InitialContext();
_mailsession = (Session) context.lookup(hostname);
} catch (NamingException e) {
_logger.error(e);
throw new BlojsomPluginException(e);
}
} else {
String username = servletConfig.getInitParameter(SMTPSERVER_USERNAME_IP);
String password = servletConfig.getInitParameter(SMTPSERVER_PASSWORD_IP);
Properties props = new Properties();
props.put(SESSION_NAME, hostname);
if (BlojsomUtils.checkNullOrBlank(username) || BlojsomUtils.checkNullOrBlank(password)) {
_mailsession = Session.getInstance(props, null);
} else {
props.put("mail.smtp.auth", "true" );
props.put("mail.smtp.username", username);
props.put("mail.smtp.password", password);
_mailsession = Session.getInstance(props, new SimpleAuthenticator(username, password));
}
}
}
}
public BlogEntry[] process(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
BlogUser user,
Map context,
BlogEntry[] entries) throws BlojsomPluginException {
Blog blog = user.getBlog();
String defaultrecipientname = null;
String defaultrecipientemail = null;
if (blog.getBlogOwnerEmail() != null) {
defaultrecipientemail = blog.getBlogOwnerEmail();
}
if (blog.getBlogOwner() != null) {
defaultrecipientname = blog.getBlogOwner();
}
List messageList = (List) context.get(EmailUtils.BLOJSOM_OUTBOUNDMAIL);
if (messageList != null) {
for (int x = 0; x < + messageList.size(); x++) {
EmailMessage message = (EmailMessage) messageList.get(x);
if (message != null) {
sendMailMessage(message, defaultrecipientemail, defaultrecipientname);
}
}
}
return entries;
}
private void sendMailMessage(EmailMessage emailmessage, String defaultRecipientEmail, String defaultRecipientName) {
try {
InternetAddress defaultAddress = new InternetAddress(defaultRecipientEmail, defaultRecipientName);
EmailUtils.sendMailMessage(_mailsession, emailmessage, defaultAddress);
} catch (UnsupportedEncodingException e) {
_logger.error(e);
}
}
public void cleanup() throws BlojsomPluginException {
}
public void destroy() throws BlojsomPluginException {
}
}