From 2d374377023ec0870de9276314b355b0545e1cfa Mon Sep 17 00:00:00 2001 From: Stephane Bailliez Date: Thu, 15 Nov 2001 22:11:28 +0000 Subject: [PATCH] Allow port specification for the mail task. RFE requested by Andrew McConnell Heavily based on the patch from Magesh Umasankar Errh, I just realized the full patch was in the first attachment, I though Magesh forgot to update the mail task. Doh ! :-( git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269923 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/CoreTasks/mail.html | 5 +++ .../apache/tools/ant/taskdefs/SendEmail.java | 19 +++++--- .../org/apache/tools/mail/MailMessage.java | 45 +++++++++++++++---- 3 files changed, 56 insertions(+), 13 deletions(-) diff --git a/docs/manual/CoreTasks/mail.html b/docs/manual/CoreTasks/mail.html index 1cb3490fa..5d981404c 100644 --- a/docs/manual/CoreTasks/mail.html +++ b/docs/manual/CoreTasks/mail.html @@ -45,6 +45,11 @@ body may be specified. To send binary attachments the optional Host name of the mail server. No, default to "localhost" + + mailport + Port of the mail server. + No, default to SMTP default (25) + subject Email subject line. diff --git a/src/main/org/apache/tools/ant/taskdefs/SendEmail.java b/src/main/org/apache/tools/ant/taskdefs/SendEmail.java index 8046f88d4..8a4b96903 100644 --- a/src/main/org/apache/tools/ant/taskdefs/SendEmail.java +++ b/src/main/org/apache/tools/ant/taskdefs/SendEmail.java @@ -114,6 +114,7 @@ import org.apache.tools.ant.BuildException; public class SendEmail extends Task { private String from; private String mailhost = "localhost"; + private int mailport = MailMessage.DEFAULT_PORT; private String message; private String toList; private String subject; @@ -149,7 +150,15 @@ public class SendEmail extends Task { public void setMailhost(String mailhost) { this.mailhost = mailhost; } - + + /** + * Sets the mailport parameter of this build task. + * @param value mail port name. + */ + public void setMailport(Integer value){ + this.mailport = value.intValue(); + } + /** * Sets the message parameter of this build task. * @@ -184,12 +193,12 @@ public class SendEmail extends Task { /** * Executes this build task. * - * throws org.apache.tools.ant.BuildException if there is an error during task - * execution. + * @throws BuildException if there is an error during task execution. */ - public void execute() { + public void execute() throws BuildException { try { MailMessage mailMessage = new MailMessage(mailhost); + mailMessage.setPort(mailport); if (from != null) { mailMessage.from(from); @@ -251,7 +260,7 @@ public class SendEmail extends Task { log("Sending email"); mailMessage.sendAndClose(); } catch (IOException ioe) { - throw new BuildException("IO error sending mail: " + ioe.getMessage()); + throw new BuildException("IO error sending mail", ioe); } } diff --git a/src/main/org/apache/tools/mail/MailMessage.java b/src/main/org/apache/tools/mail/MailMessage.java index 9bcf27802..2aaf00ce7 100644 --- a/src/main/org/apache/tools/mail/MailMessage.java +++ b/src/main/org/apache/tools/mail/MailMessage.java @@ -86,6 +86,7 @@ import java.util.Enumeration; * String bcc = "bcc@you.com"; *   * MailMessage msg = new MailMessage(mailhost); + * msg.setPort(25); * msg.from(from); * msg.to(to); * msg.cc(cc1); @@ -126,13 +127,32 @@ import java.util.Enumeration; */ public class MailMessage { - String host; - String from; - Vector to, cc; - Hashtable headers; - MailPrintStream out; - SmtpResponseReader in; - Socket socket; + /** default port for SMTP: 25 */ + public final static int DEFAULT_PORT = 25; + + /** host name for the mail server */ + private String host; + + /** host port for the mail server */ + private int port = DEFAULT_PORT; + + /** sender email address */ + private String from; + + /** list of email addresses to send to */ + private Vector to; + + /** list of email addresses to cc to */ + private Vector cc; + + /** headers to send in the mail */ + private Hashtable headers; + + private MailPrintStream out; + + private SmtpResponseReader in; + + private Socket socket; /** * Constructs a new MailMessage to send an email. @@ -161,6 +181,15 @@ public class MailMessage { sendHelo(); } + /** + * Set the port to connect to the SMTP host. + * @param port the port to use for connection. + * @see #DEFAULT_PORT + */ + public void setPort(int port){ + this.port = port; + } + /** * Sets the from address. Also sets the "From" header. This method should * be called only once. @@ -326,7 +355,7 @@ public class MailMessage { // * * * * * Raw protocol methods below here * * * * * void connect() throws IOException { - socket = new Socket(host, 25); + socket = new Socket(host, port); out = new MailPrintStream( new BufferedOutputStream( socket.getOutputStream()));