RFE requested by Andrew McConnell <mcconnell@socketware.com> Heavily based on the patch from Magesh Umasankar <umagesh@rediffmail.com> 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-ffa450edef68master
@@ -45,6 +45,11 @@ body may be specified. To send binary attachments the optional | |||||
<td valign="top">Host name of the mail server.</td> | <td valign="top">Host name of the mail server.</td> | ||||
<td align="center" valign="top">No, default to "localhost"</td> | <td align="center" valign="top">No, default to "localhost"</td> | ||||
</tr> | </tr> | ||||
<tr> | |||||
<td valign="top">mailport</td> | |||||
<td valign="top">Port of the mail server.</td> | |||||
<td align="center" valign="top">No, default to SMTP default (25)</td> | |||||
</tr> | |||||
<tr> | <tr> | ||||
<td valign="top">subject</td> | <td valign="top">subject</td> | ||||
<td valign="top">Email subject line.</td> | <td valign="top">Email subject line.</td> | ||||
@@ -114,6 +114,7 @@ import org.apache.tools.ant.BuildException; | |||||
public class SendEmail extends Task { | public class SendEmail extends Task { | ||||
private String from; | private String from; | ||||
private String mailhost = "localhost"; | private String mailhost = "localhost"; | ||||
private int mailport = MailMessage.DEFAULT_PORT; | |||||
private String message; | private String message; | ||||
private String toList; | private String toList; | ||||
private String subject; | private String subject; | ||||
@@ -149,7 +150,15 @@ public class SendEmail extends Task { | |||||
public void setMailhost(String mailhost) { | public void setMailhost(String mailhost) { | ||||
this.mailhost = 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. | * Sets the message parameter of this build task. | ||||
* | * | ||||
@@ -184,12 +193,12 @@ public class SendEmail extends Task { | |||||
/** | /** | ||||
* Executes this build 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 { | try { | ||||
MailMessage mailMessage = new MailMessage(mailhost); | MailMessage mailMessage = new MailMessage(mailhost); | ||||
mailMessage.setPort(mailport); | |||||
if (from != null) { | if (from != null) { | ||||
mailMessage.from(from); | mailMessage.from(from); | ||||
@@ -251,7 +260,7 @@ public class SendEmail extends Task { | |||||
log("Sending email"); | log("Sending email"); | ||||
mailMessage.sendAndClose(); | mailMessage.sendAndClose(); | ||||
} catch (IOException ioe) { | } catch (IOException ioe) { | ||||
throw new BuildException("IO error sending mail: " + ioe.getMessage()); | |||||
throw new BuildException("IO error sending mail", ioe); | |||||
} | } | ||||
} | } | ||||
@@ -86,6 +86,7 @@ import java.util.Enumeration; | |||||
* String bcc = "bcc@you.com"; | * String bcc = "bcc@you.com"; | ||||
* | * | ||||
* MailMessage msg = new MailMessage(mailhost); | * MailMessage msg = new MailMessage(mailhost); | ||||
* msg.setPort(25); | |||||
* msg.from(from); | * msg.from(from); | ||||
* msg.to(to); | * msg.to(to); | ||||
* msg.cc(cc1); | * msg.cc(cc1); | ||||
@@ -126,13 +127,32 @@ import java.util.Enumeration; | |||||
*/ | */ | ||||
public class MailMessage { | 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. | * Constructs a new MailMessage to send an email. | ||||
@@ -161,6 +181,15 @@ public class MailMessage { | |||||
sendHelo(); | 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 | * Sets the from address. Also sets the "From" header. This method should | ||||
* be called only once. | * be called only once. | ||||
@@ -326,7 +355,7 @@ public class MailMessage { | |||||
// * * * * * Raw protocol methods below here * * * * * | // * * * * * Raw protocol methods below here * * * * * | ||||
void connect() throws IOException { | void connect() throws IOException { | ||||
socket = new Socket(host, 25); | |||||
socket = new Socket(host, port); | |||||
out = new MailPrintStream( | out = new MailPrintStream( | ||||
new BufferedOutputStream( | new BufferedOutputStream( | ||||
socket.getOutputStream())); | socket.getOutputStream())); | ||||