gnu-crypto-discuss
[Top][All Lists]
Advanced

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

Re: [GNU Crypto] Using gnu-crypto-discuss to send emails via authorized


From: Øyvind Harboe
Subject: Re: [GNU Crypto] Using gnu-crypto-discuss to send emails via authorized SMTP servers
Date: Thu, 22 Jan 2004 09:51:41 +0100

ons, 21.01.2004 kl. 23.12 skrev Casey Marshall: 
> --BEGIN PGP SIGNED MESSAGE--
> Hash: SHA1
> 
> >>>>> "Øyvind" == Øyvind Harboe <address@hidden> writes:
> 
> Øyvind> I'm trying to compile a Java app w/GCJ that will send UTF8
> Øyvind> encoded emails via an SMTP server that requires
> Øyvind> authentication.
> 
> Øyvind> Can I use gnu-crypto?
> 
> Øyvind> My intial attempts failed because Sasl.createSasClient() in
> Øyvind> gnu.inet.smtp.SMTPConnection.authenticate() returns null.
> 
> I didn't write those, so I'm not sure of where the problem is.
> 
> Sasl.createSaslClient() will return null if there is a fatal
> configuration problem (i.e. if a Factory class cannot be found). 

I take this as a "Yes GNU Crypto can be used for SMTP authentication" :

Which version do I need of GNU Crypto?

> Are you sure you have set things up properly?

I'm pretty sure that I'm not doing it correctly, but knowing that I'm
trying to do something that is at all possible is a big push in the
right direction. :-)

There are two scenarios:

- Development environment. Eclipse + Suns JRE. Here I'd like to debug
GNU Classpath, Classpathx and Crypto. This is the only thing I've tried
so far.
- GCJ deployment. Not yet tested w/GNU Classpathx.

My test app:

// compile w/GCJ
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)
        {
// get GNU Classpath to pick up:
// security.provider.1=gnu.crypto.jce.GnuCrypto
                System.setProperty ("gnu.classpath.home.url", "e:\\foo\bar");
                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]