Commit bd240cff authored by Matt Tucker's avatar Matt Tucker Committed by matt

Initial version of email code. Port by Ryan Graham (JM-273).


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@1355 b35dd754-fafc-0310-a699-88a17e54d16e
parent f3694819
......@@ -87,6 +87,8 @@ tab.server.descr=Click to manage server settings
sidebar.server-logs.descr=Click to view server logs
sidebar.plugin-settings=Plugins
sidebar.plugin-settings.descr=Click to view plugins
sidebar.server-email=Email Settings
sidebar.server-email.descr=Click to configure email settings
sidebar.sidebar-server-settings=Server Settings
sidebar.server-reg-and-login=Registration & Login
sidebar.server-reg-and-login.descr=Click to edit registration & login policies
......@@ -1277,4 +1279,49 @@ plugin.admin.delete=Delete
plugin.admin.no_plugin=No plugins installed.
plugin.admin.click_edit=Click to edit...
plugin.admin.click_delete=Click to delete...
plugin.admin.confirm=Delete plugin?
\ No newline at end of file
plugin.admin.confirm=Delete plugin?
# System Email
system.email.title=Email Settings
system.email.info=Use the form below to set the host and port of your email server (SMTP). At a minimum \
you should set the host and optionally you can set the port, username and password and whether \
or not to connect over SSL. If you have problems sending email please check the SMTP configuration \
on your mail server. Note, if you choose to enable mail debugging the debug output will be written \
to your appserver's standard out log.
system.email.update_success=SMTP settings updated successfully.
system.email.update_failure=An error occured. Please verify that you have filled out all required fields \
correctly and try again.
system.email.name=SMTP Settings
system.email.mail_host=Mail Host
system.email.restart_possible=may require appserver restart
system.email.valid_host_name=Please enter a valid host name.
system.email.server_port=Server Port (Optional)
system.email.mail_debugging=Mail Debugging
system.email.server_username=Server Username (Optional)
system.email.server_password=Server Password (Optional)
system.email.ssl=Use SSL (Optional)
system.email.save=Save Changes
system.email.send_test=Send Test Email...
system.emailtest.title=Email Settings
system.emailtest.info=Use the form below to send a test message. By default, the senders email address will \
be tha of the admin user.
system.emailtest.no_host=Error, sending an email will fail because the mail server host is not set. Please \
go back to the {0}mail settings page{1} and set the mail host.
system.emailtest.success=Message was sent successfully. Verify it was sent by checking the mail account you \
sent the message to.
system.emailtest.failure=Sending the message failed. Please verify that your host and port settings \
are correct.
system.emailtest.failure_authentication=Authenticating to the SMTP server failed - make sure your username \
and password are correct, or that "guest" users can authenticate to send emails.
system.emailtest.mail_server=Mail Server
system.emailtest.host_not_set=Not set
system.emailtest.ssl=SSL
system.emailtest.username=Username
system.emailtest.from=From
system.emailtest.to=To
system.emailtest.subject=Subject
system.emailtest.body=Body
system.emailtest.send=Send
system.emailtest.cancel=Cancel/Go Back
\ No newline at end of file
......@@ -96,8 +96,6 @@ public class EmailService {
password = JiveGlobals.getProperty("mail.smtp.password");
sslEnabled = JiveGlobals.getBooleanProperty("mail.smtp.ssl");
debugEnabled = JiveGlobals.getBooleanProperty("mail.debug");
createSession();
}
/**
......@@ -108,6 +106,9 @@ public class EmailService {
* @return A new JavaMail message.
*/
public MimeMessage createMimeMessage() {
if (session == null) {
createSession();
}
return new MimeMessage(session);
}
......@@ -253,75 +254,170 @@ public class EmailService {
}
/**
* Sets the SMTP host (eg mail.example.com). The host is null by
* default, but must be set before gateway exports can execute.
* Sends a collection of email messages. This method differs from
* {@link #sendMessages(Collection<MimeMessage>)} in that messages are sent
* before this method returns rather than queueing the messages to be sent later.
*
* @param messages
* @throws MessagingException
*/
public void sendMessagesImmediately(Collection<MimeMessage> messages)
throws MessagingException
{
EmailTask task = new EmailTask(messages);
task.sendMessages();
}
/**
* Returns the SMTP host (e.g. mail.example.com). The host is <tt>null</tt> by
* default, but must be set before email can be sent.
*
* @param host The SMTP host.
* @return the SMTP host.
*/
public String getHost() {
return host;
}
/**
* Sets the SMTP host (e.g. mail.example.com). The host is <tt>null</tt> by
* default, but must be set before email can be sent.
*
* @param host the SMTP host.
*/
public void setHost(String host) {
this.host = host;
createSession();
JiveGlobals.setProperty("mail.smtp.host", host);
session = null;
}
/**
* Returns the port number used when connecting to the SMTP server. The default port is 25.
*
* @return the SMTP port.
*/
public int getPort() {
return port;
}
/**
* Sets the port number that will be used when connecting to the SMTP
* server. The default is 25, the standard SMTP port number.
*
* @param port The SMTP port number.
* @param port the SMTP port number.
*/
public void setPort(int port) {
this.port = port;
createSession();
JiveGlobals.setProperty("mail.smtp.port", Integer.toString(port));
session = null;
}
/**
* Returns the username used to connect to the SMTP server. If the username
* is <tt>null</tt>, no username will be used when connecting to the server.
*
* @return the username used to connect to the SMTP server, or <tt>null</tt> if
* there is no username.
*/
public String getUsername() {
return username;
}
/**
* Sets the username that will be used when connecting to the SMTP
* server. The default is null, or no username.
* server. The default is <tt>null</tt>, or no username.
*
* @param username The SMTP username.
* @param username the SMTP username.
*/
public void setUsername(String username) {
this.username = username;
createSession();
if (username == null) {
JiveGlobals.deleteProperty("mail.smtp.username");
}
else {
JiveGlobals.setProperty("mail.smtp.username", username);
}
session = null;
}
/**
* Sets the username that will be used when connecting to the SMTP
* server. The default is null, or no username.
* Returns the password used to connect to the SMTP server. If the password
* is <tt>null</tt>, no password will be used when connecting to the server.
*
* @param password The SMTP password.
* @return the password used to connect to the SMTP server, or <tt>null</tt> if
* there is no password.
*/
public String getPassword() {
return password;
}
/**
* Sets the password that will be used when connecting to the SMTP
* server. The default is <tt>null</tt>, or no password.
*
* @param password the SMTP password.
*/
public void setPassword(String password) {
this.password = password;
createSession();
if (password == null) {
JiveGlobals.deleteProperty("mail.smtp.password");
}
else {
JiveGlobals.setProperty("mail.smtp.password", password);
}
session = null;
}
/**
* Toggles SMTP transport layer debugging on or off. Debug information is
* Returns true if SMTP debugging is enabled. Debug information is
* written to <tt>System.out</tt> by the underlying JavaMail provider.
*
* @return true if SMTP debugging is enabled.
*/
public boolean isDebugEnabled() {
return debugEnabled;
}
/**
* Enables or disables SMTP transport layer debugging. Debug information is
* written to <tt>System.out</tt> by the underlying JavaMail provider.
*
* @param debugEnabled true if SMTP debugging should be enabled.
*/
public void setDebugEnabled(boolean debugEnabled) {
this.debugEnabled = debugEnabled;
createSession();
JiveGlobals.setProperty("mail.debug", Boolean.toString(debugEnabled));
session = null;
}
/**
* Sets whether this gateway is configured for SSL connections
* to the SMTP server or not.
* Returns true if SSL is enabled for SMTP connections.
*
* @return true if SSL is enabled.
*/
public boolean isSSLEnabled() {
return sslEnabled;
}
/**
* Sets whether the SMTP connection is configured to use SSL or not.
* Typically, the port should be 465 when using SSL with SMTP.
*
* @param sslEnabled true if ssl should be enabled, false otherwise.
*/
public void setSSLEnabled(boolean sslEnabled) {
this.sslEnabled = sslEnabled;
createSession();
JiveGlobals.setProperty("mail.smtp.ssl", Boolean.toString(sslEnabled));
session = null;
}
/**
* Creates a Javamail session.
*/
private void createSession() {
private synchronized void createSession() {
if (host == null) {
throw new IllegalArgumentException("Host cannot be null.");
}
Properties mailProps = new Properties();
mailProps.setProperty("mail.smtp.host", host);
mailProps.setProperty("mail.smtp.port", String.valueOf(port));
......@@ -359,9 +455,21 @@ public class EmailService {
}
public void run() {
try {
sendMessages();
}
catch (MessagingException me) {
Log.error(me);
}
}
public void sendMessages() throws MessagingException {
Transport transport = null;
try {
URLName url = new URLName("smtp", host, port, "", username, password);
if (session == null) {
createSession();
}
transport = new com.sun.mail.smtp.SMTPTransport(session, url);
transport.connect(host, port, username, password);
for (MimeMessage message : messages) {
......@@ -379,9 +487,6 @@ public class EmailService {
}
}
}
catch (MessagingException me) {
Log.error(me);
}
finally {
if (transport != null) {
try {
......
......@@ -42,6 +42,11 @@
<item id="plugin-settings" name="${sidebar.plugin-settings}"
url="plugin-admin.jsp"
description="${sidebar.plugin-settings.descr}" />
<!-- Email -->
<item id="system-email" name="${sidebar.server-email}"
url="system-email.jsp"
description="${sidebar.server-email.descr}" />
</sidebar>
<!-- Server Settings -->
......
<%--
- Copyright (C) 2005 Jive Software. All rights reserved.
-
- This software is published under the terms of the GNU Public License (GPL),
- a copy of which is included in this distribution.
--%>
<%@ page import="java.util.*,
org.jivesoftware.util.*,
org.jivesoftware.admin.*"
errorPage="error.jsp"
%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>
<%-- Define Administration Bean --%>
<jsp:useBean id="admin" class="org.jivesoftware.util.WebManager" />
<% admin.init(request, response, session, application, out ); %>
<%
// get parameters
String host = ParamUtils.getParameter(request,"host");
int port = ParamUtils.getIntParameter(request,"port",0);
String username = ParamUtils.getParameter(request,"server_username");
String password = ParamUtils.getParameter(request,"server_password");
boolean ssl = ParamUtils.getBooleanParameter(request,"ssl");
boolean save = request.getParameter("save") != null;
boolean test = request.getParameter("test") != null;
boolean success = ParamUtils.getBooleanParameter(request,"success");
boolean debug = ParamUtils.getBooleanParameter(request, "debug");
// Handle a test request
if (test) {
response.sendRedirect("system-emailtest.jsp");
return;
}
EmailService service = EmailService.getInstance();
// Save the email settings if requested
Map errors = new HashMap();
if (save) {
if (host != null) {
service.setHost(host);
}
else {
errors.put("host","");
}
if (port > 0) {
service.setPort(port);
}
else {
// Default to port 25.
service.setPort(25);
}
service.setUsername(username);
service.setPassword(password);
service.setDebugEnabled(debug);
service.setSSLEnabled(ssl);
if (errors.size() == 0) {
response.sendRedirect("system-email.jsp?success=true");
}
}
host = service.getHost();
port = service.getPort();
username = service.getUsername();
password = service.getPassword();
ssl = service.isSSLEnabled();
debug = service.isDebugEnabled();
%>
<jsp:useBean id="pageinfo" scope="request" class="org.jivesoftware.admin.AdminPageBean" />
<% // Title of this page and breadcrumbs
String title = LocaleUtils.getLocalizedString("system.email.title");
pageinfo.setTitle(title);
pageinfo.getBreadcrumbs().add(new AdminPageBean.Breadcrumb("Main", "main.jsp"));
pageinfo.getBreadcrumbs().add(new AdminPageBean.Breadcrumb(title, "system-email.jsp"));
pageinfo.setPageID("system-email");
%>
<jsp:include page="top.jsp" flush="true" />
<jsp:include page="title.jsp" flush="true" />
<p>
<fmt:message key="system.email.info" />
</p>
<% if ("true".equals(request.getParameter("success"))) { %>
<div class="jive-success">
<table cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr>
<td class="jive-icon"><img src="images/success-16x16.gif" width="16" height="16" border="0"></td>
<td class="jive-icon-label"><fmt:message key="system.email.update_success" /></td>
</tr>
</tbody>
</table>
</div>
<% } %>
<% if (errors.size() > 0) { %>
<div class="jive-error">
<table cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr>
<td class="jive-icon"><img src="images/error-16x16.gif" width="16" height="16" border="0"></td>
<td class="jive-icon-label"><fmt:message key="system.email.update_failure" /></td>
</tr>
</tbody>
</table>
</div>
<% } %>
<p>
<form action="system-email.jsp" name="f" method="post">
<b><fmt:message key="system.email.name" /></b>
<ul>
<table cellpadding="3" cellspacing="0" border="0">
<tr>
<td width="1%" nowrap>
<fmt:message key="system.email.mail_host" />:
</td>
<td width="1%" nowrap>
<input type="text" name="host" value="<%= (host != null)?host:"" %>" size="40" maxlength="150">
</td>
</tr>
<% if (errors.containsKey("host")) { %>
<tr>
<td width="1%" nowrap>
&nbsp;
</td>
<td width="1%" nowrap class="jive-error-text">
<fmt:message key="system.email.valid_host_name" />
</td>
</tr>
<% } %>
<tr>
<td width="1%" nowrap>
<fmt:message key="system.email.server_port" />:
</td>
<td width="1%" nowrap>
<input type="text" name="port" value="<%= (port > 0) ? String.valueOf(port) : "" %>" size="10" maxlength="15">
</td>
</tr>
<tr>
<td width="1%" nowrap>
<fmt:message key="system.email.mail_debugging" />:
</td>
<td width="1%" nowrap>
<input type="radio" name="debug" value="true"<%= (debug ? " checked" : "") %> id="rb01"> <label for="rb01">On</label>
&nbsp;
<input type="radio" name="debug" value="false"<%= (debug ? "" : " checked") %> id="rb02"> <label for="rb02">Off</label>
&nbsp; (<fmt:message key="system.email.restart_possible" />)
</td>
</tr>
<%-- spacer --%>
<tr><td colspan="2">&nbsp;</td></tr>
<tr>
<td width="1%" nowrap>
<fmt:message key="system.email.server_username" />:
</td>
<td width="1%" nowrap>
<input type="text" name="server_username" value="<%= (username != null) ? username : "" %>" size="40" maxlength="150">
</td>
</tr>
<tr>
<td width="1%" nowrap>
<fmt:message key="system.email.server_password" />:
</td>
<td width="1%" nowrap>
<input type="password" name="server_password" value="<%= (password != null) ? password : "" %>" size="40" maxlength="150">
</td>
</tr>
<tr>
<td width="1%" nowrap>
<fmt:message key="system.email.ssl" />:
</td>
<td width="1%" nowrap>
<input type="checkbox" name="ssl"<%= (ssl) ? " checked" : "" %>>
</td>
</tr>
</table>
</ul>
<br>
<input type="submit" name="save" value="<fmt:message key="system.email.save" />">
<input type="submit" name="test" value="<fmt:message key="system.email.send_test" />">
</form>
<jsp:include page="bottom.jsp" flush="true" />
\ No newline at end of file
<%--
- Copyright (C) 2005 Jive Software. All rights reserved.
-
- This software is published under the terms of the GNU Public License (GPL),
- a copy of which is included in this distribution.
--%>
<%@ page import="org.jivesoftware.admin.*,
org.jivesoftware.util.*,
org.jivesoftware.messenger.user.*,
java.util.*,
javax.mail.*,
javax.mail.internet.*"
errorPage="error.jsp"
%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>
<%-- Define Administration Bean --%>
<jsp:useBean id="admin" class="org.jivesoftware.util.WebManager" />
<% admin.init(request, response, session, application, out ); %>
<% // Get paramters
boolean doTest = request.getParameter("test") != null;
boolean cancel = request.getParameter("cancel") != null;
boolean sent = ParamUtils.getBooleanParameter(request,"sent");
boolean success = ParamUtils.getBooleanParameter(request,"success");
String from = ParamUtils.getParameter(request,"from");
String to = ParamUtils.getParameter(request,"to");
String subject = ParamUtils.getParameter(request,"subject");
String body = ParamUtils.getParameter(request,"body");
// Cancel if requested
if (cancel) {
response.sendRedirect("system-email.jsp");
return;
}
// Variable to hold messaging exception, if one occurs
Exception mex = null;
// Validate input
Map errors = new HashMap();
if (doTest) {
if (from == null) { errors.put("from",""); }
if (to == null) { errors.put("to",""); }
if (subject == null) { errors.put("subject",""); }
if (body == null) { errors.put("body",""); }
EmailService service = EmailService.getInstance();
// Validate host - at a minimum, it needs to be set:
String host = service.getHost();
if (host == null) {
errors.put("host","");
}
// if no errors, continue
if (errors.size() == 0) {
// Create a message
MimeMessage message = service.createMimeMessage();
// set to and from
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to, null));
message.setFrom(new InternetAddress(from, null));
message.setSubject(subject);
message.setText(body);
// Send the message, wrap in a try/catch:
try {
service.sendMessagesImmediately(Collections.singletonList(message));
// success, so indicate this:
response.sendRedirect("system-emailtest.jsp?sent=true&success=true");
return;
}
catch (MessagingException me) {
me.printStackTrace();
mex = me;
}
}
}
// Set var defaults
User user = admin.getUserManager().getUser("admin");
if (from == null) {
from = user.getEmail();
}
if (to == null) {
to = user.getEmail();
}
if (subject == null) {
subject = "Test email sent via Jive Messenger";
}
if (body == null) {
body = "This is a test message.";
}
%>
<jsp:useBean id="pageinfo" scope="request" class="org.jivesoftware.admin.AdminPageBean" />
<% // Title of this page and breadcrumbs
String title = LocaleUtils.getLocalizedString("system.emailtest.title");
pageinfo.setTitle(title);
pageinfo.getBreadcrumbs().add(new AdminPageBean.Breadcrumb("Main", "main.jsp"));
pageinfo.getBreadcrumbs().add(new AdminPageBean.Breadcrumb(title, "system-email.jsp"));
pageinfo.setPageID("system-email");
%>
<jsp:include page="top.jsp" flush="true" />
<jsp:include page="title.jsp" flush="true" />
<script language="JavaScript" type="text/javascript">
var clicked = false;
function checkClick(el) {
if (!clicked) {
clicked = true;
return true;
}
return false;
}
</script>
<p>
<fmt:message key="system.emailtest.info" />
</p>
<% if (JiveGlobals.getProperty("mail.smtp.host") == null) { %>
<div class="jive-error">
<table cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr>
<td class="jive-icon"><img src="images/error-16x16.gif" width="16" height="16" border="0"></td>
<td class="jive-icon-label">
<fmt:message key="system.emailtest.no_host">
<fmt:param value="<%= "<a href='system-email.jsp>" %>"/>
<fmt:param value="<%= "</a>" %>"/>
</fmt:message>
</td>
</tr>
</tbody>
</table>
</div>
<% } %>
<% if (doTest || sent) { %>
<% if (success) { %>
<div class="jive-success">
<table cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr>
<td class="jive-icon"><img src="images/success-16x16.gif" width="16" height="16" border="0"></td>
<td class="jive-icon-label"><fmt:message key="system.emailtest.success" /></td>
</tr>
</tbody>
</table>
</div>
<% } else { %>
<div class="jive-error">
<table cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr><td class="jive-icon"><img src="images/error-16x16.gif" width="16" height="16" border="0"></td>
<td class="jive-icon-label">
<fmt:message key="system.emailtest.failure" />
<% if (mex != null) { %>
<% if (mex instanceof AuthenticationFailedException) { %>
<fmt:message key="system.emailtest.failure_authentication" />
<% } else { %>
(Message: <%= mex.getMessage() %>)
<% } %>
<% } %>
</td></tr>
</tbody>
</table>
</div>
<% } %>
<br>
<% } %>
<form action="system-emailtest.jsp" method="post" name="f" onsubmit="return checkClick(this);">
<table cellpadding="3" cellspacing="0" border="0">
<tbody>
<tr>
<td>
<fmt:message key="system.emailtest.mail_server" />:
</td>
<td>
<% String host = JiveGlobals.getProperty("mail.smtp.host");
if (host == null) {
%>
<i><fmt:message key="system.emailtest.host_not_set" /></i>
<%
} else {
%>
<%= host %>:<%= JiveGlobals.getIntProperty("mail.smtp.port", 25) %>
<% if (JiveGlobals.getBooleanProperty("mail.smtp.ssl", false)) { %>
(<fmt:message key="system.emailtest.ssl" />)
<% } %>
<% } %>
</td>
</tr>
<tr>
<td>
<fmt:message key="system.emailtest.from" />:
</td>
<td>
<input type="hidden" name="from" value="<%= from %>">
<%= StringUtils.escapeHTMLTags(from) %>
<span class="jive-description">
(<a href="user-edit-form.jsp?username=admin">Update Address</a>)
</span>
</td>
</tr>
<tr>
<td>
<fmt:message key="system.emailtest.to" />:
</td>
<td>
<input type="text" name="to" value="<%= ((to != null) ? to : "") %>"
size="40" maxlength="100">
</td>
</tr>
<tr>
<td>
<fmt:message key="system.emailtest.subject" />:
</td>
<td>
<input type="text" name="subject" value="<%= ((subject != null) ? subject : "") %>"
size="40" maxlength="100">
</td>
</tr>
<tr valign="top">
<td>
<fmt:message key="system.emailtest.body" />:
</td>
<td>
<textarea name="body" cols="45" rows="5" wrap="virtual"><%= body %></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<br>
<input type="submit" name="test" value="<fmt:message key="system.emailtest.send" />">
<input type="submit" name="cancel" value="<fmt:message key="system.emailtest.cancel" />">
</td>
</tr>
</tbody>
</table>
</form>
<jsp:include page="bottom.jsp" flush="true" />
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment