Browse Source

Allow port specification for the mail task.

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-ffa450edef68
master
Stephane Bailliez 23 years ago
parent
commit
2d37437702
3 changed files with 56 additions and 13 deletions
  1. +5
    -0
      docs/manual/CoreTasks/mail.html
  2. +14
    -5
      src/main/org/apache/tools/ant/taskdefs/SendEmail.java
  3. +37
    -8
      src/main/org/apache/tools/mail/MailMessage.java

+ 5
- 0
docs/manual/CoreTasks/mail.html View File

@@ -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 align="center" valign="top">No, default to &quot;localhost&quot;</td>
</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>
<td valign="top">subject</td>
<td valign="top">Email subject line.</td>


+ 14
- 5
src/main/org/apache/tools/ant/taskdefs/SendEmail.java View File

@@ -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);
}
}



+ 37
- 8
src/main/org/apache/tools/mail/MailMessage.java View File

@@ -86,6 +86,7 @@ import java.util.Enumeration;
* String bcc = "bcc@you.com";
* &nbsp;
* 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()));


Loading…
Cancel
Save