classpath-inetlib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Classpath-inetlib] Null pointer exception in gnu.inet.smtp.SMTPConnecti


From: Øyvind Harboe
Subject: [Classpath-inetlib] Null pointer exception in gnu.inet.smtp.SMTPConnection.authenticate
Date: Thu, 15 Jan 2004 17:27:23 +0100

I'm having trouble with SMTP authentication.

- My application (below) works with Sun's javamail-1.3.1
- The method call below returns null, which causes an exception that is
eventually converted to a an AuthenticationException.

    SaslClient sasl = Sasl.createSaslClient(m, null, "smtp",
                                              socket.getInetAddress().
                                              getHostName(), p, ch);
- As near as I can tell from the documentation, null is a valid return
value from createSaslClient() and hence it is surprising that
authenticate() does not handle the case.
- If I uncomment authLogin() and plonk it into authenticate(), my app
works fine.

Øyvind

import java.net.UnknownHostException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



public class Send
{
        /** 
         * not a pretty function call definition, but it isn't supposed to be.
         * A "thin" interface between email sending capabilities and the C++
caller.
         */
        public static int send(String smtp, 
        final String login, 
        final String pw, 
        String from, 
        String[] to, 
        String[] bcc,  
        String subject, 
        String body)
        {
                int ok=1; //generic error
                try
                {
                        /** Set up session props and session. */                
                        Properties sessionProps = System.getProperties();
                        sessionProps.put("mail.smtp.host", smtp);
                        if (login.length()>0||pw.length()>0)
                        {       
                                sessionProps.put("mail.smtp.auth", "true");
                        }

                        Session m_session = 
Session.getDefaultInstance(sessionProps);
                        m_session.setDebug(true);
                        
                        MimeMessage message = new MimeMessage(m_session);
                        message.setFrom(new InternetAddress(from));
                        
                        for (int i=0; i<to.length; i++)
                        {
                                message.addRecipients(Message.RecipientType.TO, 
to[i]);
                        }
                        for (int i=0; i<bcc.length; i++)
                        {
                                
message.addRecipients(Message.RecipientType.BCC, bcc[i]);
                        }
                                                
                        message.setSubject(subject, "UTF8");
                        
                        message.setText(body, "UTF8");
                        Transport transport =  m_session.getTransport("smtp");
                        transport.connect(smtp, login, pw);
                        try
                        {
                                transport.sendMessage(message, 
message.getAllRecipients());
                        }
                        finally
                        {
                                transport.close();
                        }
                        
                        ok=0;
                }
                catch (AddressException e)
                {
                        // I haven't seen this thrown.
                        ok=3;
                }
                catch (NoSuchProviderException e)
                {
                        int x=0;
                }
                catch (javax.mail.AuthenticationFailedException e)
                {
                        // bad username or password
                        ok=2;
                }
                catch (SendFailedException e)
                {
                        // the server exists, but does not respond. anti-spam?
                        int x=0;
                }
                catch (MessagingException e)
                {
                        Exception nested=e.getNextException();
                        if (nested!=null)
                        {       
                                try
                                {
                                        UnknownHostException 
host=(UnknownHostException)nested;
                                        
                                        // there is no such host. Typo.
                                        ok=4;
                                } catch (ClassCastException e2)
                                {
                                }
                        } else
                        {
                                // the server exists, but does not respond or 
fails. Antispam?                          
                        }
                }
                return 0;
        }
}







reply via email to

[Prev in Thread] Current Thread [Next in Thread]