Author: sebawagner Date: Sat Jul 20 05:12:57 2013 New Revision: 1505091 URL: http://svn.apache.org/r1505091 Log: Some improvements around mail sending: - Bump JavaMail library to 1.4.7 - Add configurable SMTP connection timeout (default is to infinite and will result in endless hanging mail threads if the mail configuration is wrong) - Add configuration parameter to SMTP: If TLS is enabled, do accept any certificate. - Configure Spring handled mail queue to check every 60 seconds (SMTP timeout default is 30 seconds)
Added: openmeetings/trunk/singlewebapp/src/org/apache/openmeetings/test/user/TestMailSending.java Modified: openmeetings/trunk/singlewebapp/WebContent/WEB-INF/openmeetings-applicationContext.xml openmeetings/trunk/singlewebapp/ivy.xml openmeetings/trunk/singlewebapp/src/org/apache/openmeetings/installation/ImportInitvalues.java openmeetings/trunk/singlewebapp/src/org/apache/openmeetings/utils/mail/MailHandler.java Modified: openmeetings/trunk/singlewebapp/WebContent/WEB-INF/openmeetings-applicationContext.xml URL: http://svn.apache.org/viewvc/openmeetings/trunk/singlewebapp/WebContent/WEB-INF/openmeetings-applicationContext.xml?rev=1505091&r1=1505090&r2=1505091&view=diff ============================================================================== --- openmeetings/trunk/singlewebapp/WebContent/WEB-INF/openmeetings-applicationContext.xml (original) +++ openmeetings/trunk/singlewebapp/WebContent/WEB-INF/openmeetings-applicationContext.xml Sat Jul 20 05:12:57 2013 @@ -210,10 +210,10 @@ <ref bean="sendMails" /> </property> <property name="startDelay"> - <value>180000</value> <!-- 3 minute --> + <value>60000</value> <!-- 1 minute --> </property> <property name="repeatInterval"> - <value>180000</value> <!-- 3 minutes --> + <value>60000</value> <!-- 1 minute --> </property> </bean> Modified: openmeetings/trunk/singlewebapp/ivy.xml URL: http://svn.apache.org/viewvc/openmeetings/trunk/singlewebapp/ivy.xml?rev=1505091&r1=1505090&r2=1505091&view=diff ============================================================================== --- openmeetings/trunk/singlewebapp/ivy.xml (original) +++ openmeetings/trunk/singlewebapp/ivy.xml Sat Jul 20 05:12:57 2013 @@ -216,7 +216,7 @@ <dependency org="xpp3" name="xpp3" rev="1.1.4c" conf="openmeetings->*" transitive="false"> <include type="jar" /> </dependency> - <dependency org="javax.mail" name="mail" rev="1.4.4" conf="openmeetings->*" transitive="false"> + <dependency org="javax.mail" name="mail" rev="1.4.7" conf="openmeetings->*" transitive="false"> <include type="jar" /> </dependency> <dependency org="org.simpleframework" name="simple-xml" rev="2.7" conf="openmeetings->*" transitive="false"> Modified: openmeetings/trunk/singlewebapp/src/org/apache/openmeetings/installation/ImportInitvalues.java URL: http://svn.apache.org/viewvc/openmeetings/trunk/singlewebapp/src/org/apache/openmeetings/installation/ImportInitvalues.java?rev=1505091&r1=1505090&r2=1505091&view=diff ============================================================================== --- openmeetings/trunk/singlewebapp/src/org/apache/openmeetings/installation/ImportInitvalues.java (original) +++ openmeetings/trunk/singlewebapp/src/org/apache/openmeetings/installation/ImportInitvalues.java Sat Jul 20 05:12:57 2013 @@ -295,24 +295,30 @@ public class ImportInitvalues { // frontend configurationDao.add("default_domain_id", "1", null, ""); - // "smtp.xmlcrm.org" + configurationDao.add("smtp_server", cfg.smtpServer, null, "this is the smtp server to send messages"); - // 25 + configurationDao.add("smtp_port", "" + cfg.smtpPort, null, "this is the smtp server port normally 25"); - // "openmeeti...@xmlcrm.org" + configurationDao.add("system_email_addr", cfg.mailReferer, null, "all send EMails by the system will have this address"); - // "openmeeti...@xmlcrm.org" + configurationDao.add("email_username", cfg.mailAuthName, null, "System auth email username"); - // + configurationDao.add("email_userpass", cfg.mailAuthPass, null, "System auth email password"); configurationDao.add("mail.smtp.starttls.enable", cfg.mailUseTls, null, "Enable TLS 1=true, 0=false"); + + configurationDao.add("mail.smtp.connection.timeout", "30000", null, + "Socket connection timeout value in milliseconds. Default is infinite timeout."); + + configurationDao.add("mail.smtp.timeout", "30000", null, + "Socket I/O timeout value in milliseconds. Default is infinite timeout."); configurationDao.add("application.name", ConfigurationDao.DEFAULT_APP_NAME, null, Added: openmeetings/trunk/singlewebapp/src/org/apache/openmeetings/test/user/TestMailSending.java URL: http://svn.apache.org/viewvc/openmeetings/trunk/singlewebapp/src/org/apache/openmeetings/test/user/TestMailSending.java?rev=1505091&view=auto ============================================================================== --- openmeetings/trunk/singlewebapp/src/org/apache/openmeetings/test/user/TestMailSending.java (added) +++ openmeetings/trunk/singlewebapp/src/org/apache/openmeetings/test/user/TestMailSending.java Sat Jul 20 05:12:57 2013 @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License") + you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.openmeetings.test.user; + +import java.io.IOException; +import java.util.Date; +import java.util.Properties; + +import javax.activation.DataHandler; +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; +import javax.mail.util.ByteArrayDataSource; + +import org.apache.openmeetings.utils.mail.SmtpAuthenticator; + +import com.sun.mail.util.MailSSLSocketFactory; + +/** + * + * @author swagner + * + */ +public class TestMailSending { + + //Example GMail email server data + private String smtpServer = "smtp.gmail.com"; + private String smtpPort = "25"; + private String from = "t...@apache.org"; + private String mailAuthUser = "t...@gmail.com"; + private String mailAuthPass = "*******"; + private boolean mailTls = true; + + /** + * @Test + * + * We don't want this test to be executed in every test suite, it is more for manual testing + * handy to check your server and the JavaMail setting properties + * + */ + public void doTestSendEmail() { + try{ + + Transport.send(getMimeMessage()); + + } catch (Exception err) { + err.printStackTrace(); + } + + } + + private MimeMessage getMimeMessage() throws Exception { + // Building MimeMessage + MimeMessage msg = getBasicMimeMessage(); + msg.setSubject("getSubject()"); + msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse("seba.wag...@gmail.com", false)); + + return appendBody(msg); + } + + private MimeMessage appendBody(MimeMessage msg) throws MessagingException, IOException { + // -- Set the subject and body text -- + msg.setDataHandler(new DataHandler(new ByteArrayDataSource("getBody", "text/html; charset=\"utf-8\""))); + + // -- Set some other header information -- + msg.setHeader("X-Mailer", "XML-Mail"); + msg.setSentDate(new Date()); + + return msg; + } + + private MimeMessage getBasicMimeMessage() throws Exception { + System.out.println("getBasicMimeMessage"); + Properties props = new Properties(System.getProperties()); + + props.put("mail.smtp.connectiontimeout", 30000); //default timeout is 30 seconds, javaMail default is "infinite" + props.put("mail.smtp.timeout", 30000); //default timeout is 30 seconds, javaMail default is "infinite" + + props.put("mail.smtp.host", smtpServer); + props.put("mail.smtp.port", smtpPort); + + if (mailTls) { + props.put("mail.smtp.starttls.enable", "true"); + MailSSLSocketFactory sf = new MailSSLSocketFactory(); + sf.setTrustAllHosts(true); + props.put("mail.smtp.ssl.socketFactory", sf); + } + + // Check for Authentication + Session session = null; + if (mailAuthUser != null && mailAuthUser.length() > 0 + && mailAuthPass != null && mailAuthPass.length() > 0) { + // use SMTP Authentication + props.put("mail.smtp.auth", "true"); + session = Session.getInstance(props, new SmtpAuthenticator(mailAuthUser, mailAuthPass)); + } else { + // not use SMTP Authentication + session = Session.getInstance(props, null); + } + + // Building MimeMessage + MimeMessage msg = new MimeMessage(session); + msg.setFrom(new InternetAddress(from)); + return msg; + } +} Modified: openmeetings/trunk/singlewebapp/src/org/apache/openmeetings/utils/mail/MailHandler.java URL: http://svn.apache.org/viewvc/openmeetings/trunk/singlewebapp/src/org/apache/openmeetings/utils/mail/MailHandler.java?rev=1505091&r1=1505090&r2=1505091&view=diff ============================================================================== --- openmeetings/trunk/singlewebapp/src/org/apache/openmeetings/utils/mail/MailHandler.java (original) +++ openmeetings/trunk/singlewebapp/src/org/apache/openmeetings/utils/mail/MailHandler.java Sat Jul 20 05:12:57 2013 @@ -50,10 +50,15 @@ import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.task.TaskExecutor; +import com.sun.mail.util.MailSSLSocketFactory; + /** * * @author swagner * + * For a documentation about Javax mail please see fro example: + * http://connector.sourceforge.net/doc-files/Properties.html + * */ public class MailHandler { private static final Logger log = Red5LoggerFactory.getLogger( @@ -74,6 +79,8 @@ public class MailHandler { private String mailAuthPass; private boolean mailTls; private boolean mailAddReplyTo; + private int smtpConnectionTimeOut; + private int smtpTimeOut; private void init() { smtpServer = cfgDao.getConfValue("smtp_server", String.class, null); @@ -83,6 +90,8 @@ public class MailHandler { mailAuthPass = cfgDao.getConfValue("email_userpass", String.class, null); mailTls = "1".equals(cfgDao.getConfValue("mail.smtp.starttls.enable", String.class, "0")); mailAddReplyTo = "1".equals(cfgDao.getConfValue("inviter.email.as.replyto", String.class, "1")); + smtpConnectionTimeOut = cfgDao.getConfValue("mail.smtp.connection.timeout", Integer.class, "30000"); + smtpTimeOut = cfgDao.getConfValue("mail.smtp.timeout", Integer.class, "30000"); } protected MimeMessage appendIcsBody(MimeMessage msg, MailMessage m) throws Exception { @@ -138,9 +147,15 @@ public class MailHandler { props.put("mail.smtp.host", smtpServer); props.put("mail.smtp.port", smtpPort); + + props.put("mail.smtp.connectiontimeout", smtpConnectionTimeOut); + props.put("mail.smtp.timeout", smtpTimeOut); if (mailTls) { props.put("mail.smtp.starttls.enable", "true"); + MailSSLSocketFactory sf = new MailSSLSocketFactory(); + sf.setTrustAllHosts(true); + props.put("mail.smtp.ssl.socketFactory", sf); } // Check for Authentication @@ -240,6 +255,7 @@ public class MailHandler { init(); log.debug("sendMails enter ..."); List<MailMessage> list = mailMessageDao.get(0, 1); + log.debug("Numebr of emails in init queue " + list.size()); while (!list.isEmpty()) { send(list.get(0), true); list = mailMessageDao.get(0, 1);