antoine 2003/05/27 08:25:49
Modified: . WHATSNEW src/main/org/apache/tools/ant/listener MailLogger.java src/main/org/apache/tools/ant/taskdefs/email EmailTask.java Mailer.java MimeMailer.java Added: src/etc/testcases/taskdefs mail.xml src/testcases/org/apache/tools/ant/taskdefs EmailTaskTest.java Log: Implementation of SMTP-Auth for MimeMail, available through <mail/> and MailLogger. PR: 5969 Revision Changes Path 1.429 +6 -0 ant/WHATSNEW Index: WHATSNEW =================================================================== RCS file: /home/cvs/ant/WHATSNEW,v retrieving revision 1.428 retrieving revision 1.429 diff -u -r1.428 -r1.429 --- WHATSNEW 27 May 2003 08:49:41 -0000 1.428 +++ WHATSNEW 27 May 2003 15:25:47 -0000 1.429 @@ -301,6 +301,12 @@ * <mail> has a new attribute encoding. Bugzilla Report 15434. +* <mail> has new attributes user and password for SMTP auth. +maillogger can also use this. +The implementation only with JavaMail (encoding="MIME"). +Implementation with plain mail remains to do. +Bugzilla Report 5969. + * <zipfileset> can now be defined in the main body of a project and referred to with refid="xyz". Bugzilla Report 17007. 1.1 ant/src/etc/testcases/taskdefs/mail.xml Index: mail.xml =================================================================== <?xml version="1.0"?> <project name="mail-test" basedir="." default="test1"> <target name="test1"> <!-- this test is supposed to bring a build exception because user and password is not allowed with plain encoding --> <mail host="localhost" port="25" from="[EMAIL PROTECTED]" to="[EMAIL PROTECTED]" subject="hello" encoding="plain" user="joe" password="secret"> <message> Hi Laura, how are you doing ? </message> </mail> </target> </project> 1.1 ant/src/testcases/org/apache/tools/ant/taskdefs/EmailTaskTest.java Index: EmailTaskTest.java =================================================================== /* * The Apache Software License, Version 1.1 * * Copyright (c) 2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "Ant" and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ package org.apache.tools.ant.taskdefs; import java.io.File; import org.apache.tools.ant.BuildFileTest; /** * TODO : develop these testcases - the email task needs to have attributes allowing * to simulate sending mail and to catch the output in text files or streams * @author <a href="mailto:[EMAIL PROTECTED]">Antoine Levy-Lambert</a> */ public class EmailTaskTest extends BuildFileTest { public EmailTaskTest(String name) { super(name); } public void setUp() { configureProject("src/etc/testcases/taskdefs/mail.xml"); } public void test1() { expectBuildException("test1", "SMTP auth only possible with MIME mail"); } } 1.17 +59 -7 ant/src/main/org/apache/tools/ant/listener/MailLogger.java Index: MailLogger.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/listener/MailLogger.java,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- MailLogger.java 15 May 2003 11:30:26 -0000 1.16 +++ MailLogger.java 27 May 2003 15:25:48 -0000 1.17 @@ -57,13 +57,14 @@ import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; -import java.util.Enumeration; -import java.util.Hashtable; -import java.util.Properties; -import java.util.StringTokenizer; +import java.util.*; + import org.apache.tools.ant.BuildEvent; import org.apache.tools.ant.DefaultLogger; import org.apache.tools.ant.Project; +import org.apache.tools.ant.taskdefs.email.MimeMailer; +import org.apache.tools.ant.taskdefs.email.EmailAddress; +import org.apache.tools.ant.taskdefs.email.Message; import org.apache.tools.ant.util.DateUtils; import org.apache.tools.ant.util.StringUtils; import org.apache.tools.mail.MailMessage; @@ -96,6 +97,8 @@ * * @author Erik Hatcher * <a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Antoine Levy-Lambert</a> + * */ public class MailLogger extends DefaultLogger { /** Buffer in which the message is constructed prior to sending */ @@ -152,13 +155,19 @@ String mailhost = getValue(properties, "mailhost", "localhost"); int port = Integer.parseInt(getValue(properties,"port",String.valueOf(MailMessage.DEFAULT_PORT))); + String user = getValue(properties, "user", null); + String password = getValue(properties, "password", null); String from = getValue(properties, "from", null); String replytoList = getValue(properties,"replyto",""); String toList = getValue(properties, prefix + ".to", null); String subject = getValue(properties, prefix + ".subject", (success) ? "Build Success" : "Build Failure"); - - sendMail(mailhost, port, from, replytoList, toList, subject, buffer.substring(0)); + if (user==null && password==null) { + sendMail(mailhost, port, from, replytoList, toList, subject, buffer.substring(0)); + } + else { + sendMimeMail(event.getProject(), mailhost, port, user, password, from, replytoList, toList, subject, buffer.substring(0)); + } } catch (Exception e) { System.out.println("MailLogger failed to send e-mail!"); e.printStackTrace(System.err); @@ -207,7 +216,6 @@ /** * Send the mail - * * @param mailhost mail server * @param port mail server port number * @param from from address @@ -240,6 +248,50 @@ ps.println(message); mailMessage.sendAndClose(); + } + /** + * Send the mail (MimeMail) + * @param project current ant project + * @param host mail server + * @param port mail server port number + * @param user user name for SMTP auth + * @param password password for SMTP auth + * @param from from address + * @param replyToString comma-separated replyto list + * @param toString comma-separated recipient list + * @param subject mail subject + * @param message mail body + * @exception IOException thrown if sending message fails + */ + private void sendMimeMail(Project project, String host, int port, String user, String password, String from, String replyToString, String toString, + String subject, String message) throws IOException { + // convert the replyTo string into a vector of emailaddresses + Vector replyToList = vectorizeEmailAddresses(replyToString); + MimeMailer mailer=new MimeMailer(); + mailer.setHost(host); + mailer.setPort(port); + mailer.setUser(user); + mailer.setPassword(password); + Message mymessage = new Message(message); + mymessage.setProject(project); + mailer.setMessage(mymessage); + mailer.setFrom(new EmailAddress(from)); + mailer.setReplyToList(replyToList); + Vector toList = vectorizeEmailAddresses(toString); + mailer.setToList(toList); + mailer.setCcList(new Vector()); + mailer.setBccList(new Vector()); + mailer.setFiles(new Vector()); + mailer.setSubject(subject); + mailer.send(); + } + private Vector vectorizeEmailAddresses(String listString) { + Vector emailList = new Vector(); + StringTokenizer tokens = new StringTokenizer(listString, ","); + while (tokens.hasMoreTokens()) { + emailList.addElement(new EmailAddress(tokens.nextToken())); + } + return emailList; } } 1.19 +27 -1 ant/src/main/org/apache/tools/ant/taskdefs/email/EmailTask.java Index: EmailTask.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/email/EmailTask.java,v retrieving revision 1.18 retrieving revision 1.19 diff -u -r1.18 -r1.19 --- EmailTask.java 15 May 2003 11:30:26 -0000 1.18 +++ EmailTask.java 27 May 2003 15:25:48 -0000 1.19 @@ -142,7 +142,28 @@ private boolean debugonly=false; /** a location where to print the email message */ private File debugoutput; + /** User for SMTP auth */ + private String user=null; + /** Password for SMTP auth */ + private String password=null; + /** + * sets the user for SMTP auth; this requires JavaMail + * @param user + * @since ant 1.6 + */ + public void setUser(String user) { + this.user = user; + } + + /** + * sets the password for SMTP auth; this requires JavaMail + * @param password + * @since ant 1.6 + */ + public void setPassword(String password) { + this.password = password; + } /** * Allows the build writer to choose the preferred encoding method @@ -431,7 +452,6 @@ // prepare for the auto select mechanism boolean autoFound = false; - // try MIME format if (encoding.equals(MIME) || (encoding.equals(AUTO) && !autoFound)) { @@ -445,6 +465,10 @@ log("Failed to initialise MIME mail: "+e.getMessage(),Project.MSG_WARN); } } + // SMTP auth only allowed with MIME mail + if (autoFound==false && ((user !=null) || (password != null)) && (encoding.equals(UU) || encoding.equals(PLAIN))) { + throw new BuildException("SMTP auth only possible with MIME mail"); + } // try UU format if (encoding.equals(UU) @@ -538,6 +562,8 @@ // pass the params to the mailer mailer.setHost(host); mailer.setPort(port); + mailer.setUser(user); + mailer.setPassword(password); mailer.setMessage(message); mailer.setFrom(from); mailer.setReplyToList(replyToList); 1.10 +19 -0 ant/src/main/org/apache/tools/ant/taskdefs/email/Mailer.java Index: Mailer.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/email/Mailer.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- Mailer.java 15 May 2003 12:22:47 -0000 1.9 +++ Mailer.java 27 May 2003 15:25:48 -0000 1.10 @@ -67,6 +67,8 @@ abstract class Mailer { protected String host = null; protected int port = -1; + protected String user = null; + protected String password = null; protected Message message; protected EmailAddress from; protected Vector replyToList = null; @@ -97,6 +99,23 @@ this.port = port; } + /** + * Sets the user for smtp auth + * + * @param user + */ + public void setUser(String user) { + this.user = user; + } + + /** + * Sets the password for smtp auth + * + * @param password + */ + public void setPassword(String password) { + this.password = password; + } /** * Sets the message 1.11 +27 -6 ant/src/main/org/apache/tools/ant/taskdefs/email/MimeMailer.java Index: MimeMailer.java =================================================================== RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/email/MimeMailer.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- MimeMailer.java 15 May 2003 11:30:26 -0000 1.10 +++ MimeMailer.java 27 May 2003 15:25:48 -0000 1.11 @@ -70,10 +70,8 @@ import javax.activation.DataHandler; import javax.activation.FileDataSource; +import javax.mail.*; import javax.mail.Message; -import javax.mail.MessagingException; -import javax.mail.Session; -import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; @@ -89,7 +87,7 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Aleksandr Ishutin</a> * @since Ant 1.5 */ -class MimeMailer extends Mailer { +public class MimeMailer extends Mailer { // Default character set private static final String defaultCharset = System.getProperty("file.encoding"); @@ -157,7 +155,16 @@ // Aside, the JDK is clearly unaware of the scottish // 'session', which //involves excessive quantities of // alcohol :-) - Session sesh = Session.getDefaultInstance(props, null); + Session sesh; + Authenticator auth; + if (user==null && password == null) { + sesh = Session.getDefaultInstance(props, null); + } + else { + props.put("mail.smtp.auth", "true"); + auth = new SimpleAuthenticator(user,password); + sesh = Session.getInstance(props,auth); + } //create the message MimeMessage msg = new MimeMessage(sesh); @@ -260,6 +267,7 @@ } return addrs; + } private String parseCharSetFromMimeType(String type){ @@ -271,5 +279,18 @@ token.nextToken();// Skip 'charset=' return token.nextToken(); } -} + static class SimpleAuthenticator extends Authenticator { + private String user=null; + private String password=null; + public SimpleAuthenticator(String user, String password) { + this.user=user; + this.password=password; + } + public PasswordAuthentication getPasswordAuthentication() { + + return new PasswordAuthentication(user, password); + + } + + }}