ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alexander Fischuk" <afisc...@gmail.com>
Subject RE: mail task
Date Tue, 13 Mar 2007 16:46:10 GMT
I did not perform too much testing ...  but what worked for me is the
following hack in MimeMailer.send() method 

This solution is tested on ant-1.6.5 code under java-1.5 JVM.
Of cause you have to run the code with   
		-Dmail.smtp.localhost=<correct EHLO greeting addr>



code snippet  START -----------------------------------------------------


    /**
     * Send the email.
     *
     * @throws BuildException if the email can't be sent.
     */
    public void send() {
        try {
            Properties props = new Properties();

// -------------------------------------------------------------------------
            // hack to make it work with stupid LAN setup


            if(System.getProperties().getProperty("mail.smtp.localhost") !=
null){
            	props.put("mail.smtp.localhost",
System.getProperties().getProperty("mail.smtp.localhost"));
            }



            // end of hack
// -------------------------------------------------------------------------
            
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", String.valueOf(port));

            // Aside, the JDK is clearly unaware of the Scottish
            // 'session', which involves excessive quantities of
            // alcohol :-)
            Session sesh;
            Authenticator auth;
            if (SSL) {
                try {
                    Provider p = (Provider) Class.forName(
 
"com.sun.net.ssl.internal.ssl.Provider").newInstance();
                    Security.addProvider(p);
                } catch (Exception e) {
                    throw new BuildException("could not instantiate ssl "
                        + "security provider, check that you have JSSE in "
                        + "your classpath");
                }
                // SMTP provider
                props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
                props.put("mail.smtp.socketFactory.fallback", "false");
            }
            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);
            MimeMultipart attachments = new MimeMultipart();

            //set the sender
            if (from.getName() == null) {
                msg.setFrom(new InternetAddress(from.getAddress()));
            } else {
                msg.setFrom(new InternetAddress(from.getAddress(),
                    from.getName()));
            }
            // set the reply to addresses
            msg.setReplyTo(internetAddresses(replyToList));
            msg.setRecipients(Message.RecipientType.TO,
                internetAddresses(toList));
            msg.setRecipients(Message.RecipientType.CC,
                internetAddresses(ccList));
            msg.setRecipients(Message.RecipientType.BCC,
                internetAddresses(bccList));

            // Choosing character set of the mail message
            // First: looking it from MimeType
            String charset =
parseCharSetFromMimeType(message.getMimeType());
            if (charset != null) {
                // Assign/reassign message charset from MimeType
                message.setCharset(charset);
            } else {
                // Next: looking if charset having explicit definition
                charset = message.getCharset();
                if (charset == null) {
                    // Using default
                    charset = DEFAULT_CHARSET;
                    message.setCharset(charset);
                }
            }
            // Using javax.activation.DataSource paradigm
            StringDataSource sds = new StringDataSource();
            sds.setContentType(message.getMimeType());
            sds.setCharset(charset);

            if (subject != null) {
                msg.setSubject(subject, charset);
            }
            msg.addHeader("Date", getDate());

            for (Iterator iter = headers.iterator(); iter.hasNext();) {
                Header h = (Header) iter.next();
                msg.addHeader(h.getName(), h.getValue());
            }
            PrintStream out = new PrintStream(sds.getOutputStream());
            message.print(out);
            out.close();

            MimeBodyPart textbody = new MimeBodyPart();
            textbody.setDataHandler(new DataHandler(sds));
            attachments.addBodyPart(textbody);

            Enumeration e = files.elements();

            while (e.hasMoreElements()) {
                File file = (File) e.nextElement();

                MimeBodyPart body;

                body = new MimeBodyPart();
                if (!file.exists() || !file.canRead()) {
                    throw new BuildException("File \"" +
file.getAbsolutePath()
                         + "\" does not exist or is not "
                         + "readable.");
                }
                FileDataSource fileData = new FileDataSource(file);
                DataHandler fileDataHandler = new DataHandler(fileData);

                body.setDataHandler(fileDataHandler);
                body.setFileName(file.getName());
                attachments.addBodyPart(body);
            }
            msg.setContent(attachments);
            Transport.send(msg);
        } catch (MessagingException e) {
            throw new BuildException("Problem while sending mime mail:", e);
        } catch (IOException e) {
            throw new BuildException("Problem while sending mime mail:", e);
        }
    }


Code snippet END
----------------------------------------------------------------------------
---

-----Original Message-----
From: Steve Loughran [mailto:stevel@apache.org] 
Sent: Tuesday, March 13, 2007 6:05 PM
To: Ant Users List
Subject: Re: mail task

Alexander Fischuk wrote:
> Hi,
> In my network setup ant email task does not work properly.
> 
> When I try to send email it gives me an error message 
> something like "550 EHLO command requires valid address..."
> 
> The origins of the error are in MimeMailer  and javamail
> This class does not take into account the property "mail.smtp.localhost"
> that is recommended solution for EHLO problem with javamail.
> 
> Am I missing something? 
> Is this a bug or it is designed to work this way?

You've just discovered something new, I think. Its impossible to test 
against other people's network configs, and your network has just broken 
Ant.

(pause)

http://java.sun.com/products/javamail/NOTES113.txt

There's a property  "mail.stmp.ehlo"	which should be set to false to 
skip EHLO login. could be a misspelling, or not.

Do this as a JVM option, not an ant property, with

export ANT_OPTS="-Dmail.smtp.ehlo=false"

The other is the task is working, but you arent setting a valid address. 
for that, I suggest you use your favourite search engine to understand 
what the error message means and how to fix it.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message