use of net.jforum.util.mail.Spammer in project jforum2 by rafaelsteil.
the class AjaxAction method sendTestMail.
/**
* Sends a test message
* @param sender The sender's email address
* @param host the smtp host
* @param auth if need authorization or not
* @param username the smtp server username, if auth is needed
* @param password the smtp server password, if auth is needed
* @param to the recipient
* @return The status message
*/
public void sendTestMail() {
String sender = this.request.getParameter("sender");
String host = this.request.getParameter("host");
String port = this.request.getParameter("port");
String auth = this.request.getParameter("auth");
String ssl = this.request.getParameter("ssl");
String username = this.request.getParameter("username");
String password = this.request.getParameter("password");
String to = this.request.getParameter("to");
// Save the current values
String originalHost = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_HOST);
String originalAuth = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_AUTH);
String originalUsername = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_USERNAME);
String originalPassword = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_PASSWORD);
String originalSender = SystemGlobals.getValue(ConfigKeys.MAIL_SENDER);
String originalSSL = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_SSL);
String originalPort = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_PORT);
// Now put the new ones
SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_HOST, host);
SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_AUTH, auth);
SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_USERNAME, username);
SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_PASSWORD, password);
SystemGlobals.setValue(ConfigKeys.MAIL_SENDER, sender);
SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_SSL, ssl);
SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_PORT, port);
String status = "OK";
// Send the test mail
class TestSpammer extends Spammer {
public TestSpammer(String to) {
List l = new ArrayList();
User user = new User();
user.setEmail(to);
l.add(user);
this.setUsers(l);
this.setTemplateParams(new SimpleHash());
this.prepareMessage("JForum Test Mail", null);
}
protected String processTemplate() throws Exception {
return ("Test mail from JForum Admin Panel. Sent at " + new Date());
}
protected void createTemplate(String messageFile) throws Exception {
}
}
Spammer s = new TestSpammer(to);
try {
s.dispatchMessages();
} catch (Exception e) {
status = StringEscapeUtils.escapeJavaScript(e.toString());
logger.error(e.toString(), e);
} finally {
// Restore the original values
SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_HOST, originalHost);
SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_AUTH, originalAuth);
SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_USERNAME, originalUsername);
SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_PASSWORD, originalPassword);
SystemGlobals.setValue(ConfigKeys.MAIL_SENDER, originalSender);
SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_SSL, originalSSL);
SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_PORT, originalPort);
}
this.setTemplateName(TemplateKeys.AJAX_TEST_MAIL);
this.context.put("status", status);
}