FileDocCategorySizeDatePackage
PlainMailer.javaAPI DocApache Ant 1.704500Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.taskdefs.email

PlainMailer

public class PlainMailer extends Mailer
Class responsible for sending email through raw protocol methods.
since
Ant 1.5

Fields Summary
Constructors Summary
Methods Summary
protected voidattach(java.io.File file, java.io.PrintStream out)
Attaches a file to this email

param
file The file to attache
param
out The message stream to add to
throws
IOException if errors occur

        if (!file.exists() || !file.canRead()) {
            throw new BuildException("File \"" + file.getName()
                 + "\" does not exist or is not "
                 + "readable.");
        }

        if (includeFileNames) {
            out.println();

            String filename = file.getName();
            int filenamelength = filename.length();

            out.println(filename);
            for (int star = 0; star < filenamelength; star++) {
                out.print('=");
            }
            out.println();
        }

        int length;
        final int maxBuf = 1024;
        byte[] buf = new byte[maxBuf];
        FileInputStream finstr = new FileInputStream(file);

        try {
            BufferedInputStream in = new BufferedInputStream(finstr, buf.length);

            while ((length = in.read(buf)) != -1) {
                out.write(buf, 0, length);
            }
        } finally {
            finstr.close();
        }
    
public voidsend()
Sends the email using the apache MailMessage class.

see
org.apache.tools.mail.MailMessage

        try {
            MailMessage mailMessage = new MailMessage(host, port);

            mailMessage.from(from.toString());

            Enumeration e;

            e = replyToList.elements();
            while (e.hasMoreElements()) {
                mailMessage.replyto(e.nextElement().toString());
            }
            e = toList.elements();
            while (e.hasMoreElements()) {
                mailMessage.to(e.nextElement().toString());
            }
            e = ccList.elements();
            while (e.hasMoreElements()) {
                mailMessage.cc(e.nextElement().toString());
            }
            e = bccList.elements();
            while (e.hasMoreElements()) {
                mailMessage.bcc(e.nextElement().toString());
            }
            if (subject != null) {
                mailMessage.setSubject(subject);
            }
            mailMessage.setHeader("Date", getDate());
            if (message.getCharset() != null) {
                mailMessage.setHeader("Content-Type", message.getMimeType()
                    + "; charset=\"" + message.getCharset() + "\"");
            } else {
                mailMessage.setHeader("Content-Type", message.getMimeType());
            }
            e = headers.elements();
            while (e.hasMoreElements()) {
                Header h = (Header) e.nextElement();
                mailMessage.setHeader(h.getName(), h.getValue());
            }
            PrintStream out = mailMessage.getPrintStream();
            message.print(out);

            e = files.elements();
            while (e.hasMoreElements()) {
                attach((File) e.nextElement(), out);
            }
            mailMessage.sendAndClose();
        } catch (IOException ioe) {
            throw new BuildException("IO error sending mail", ioe);
        }