Monday 17 January 2011

Taming the Spring JavaMailSender / GMail beastie

Programatically sending email in Spring (I'm talking about Spring 3.0.x here) really isn't too hard, you just wire up the JavaMailSenderImpl and off you go, right ?

Well, yes ... except when you want to use a GMail type account.

It's not that it's hard, it's just that there's a lot of examples out there that nearly work .... so here's the defintive guide that worked for me.

We'll get onto the spring configuration in a minute, but first a word on jars. To make this work you really need the latest and greatest JavaMail (1.4.3), and (if you don't have it JAF (1.1.1)). Go here for mail goodness: http://www.oracle.com/technetwork/java/index-jsp-139225.html

Now, onto the configuration. NB: This is only the spring config, I'm not going into any detail about how to use the MailSender, if you need help with that, go google.

There are two different configurations, depending on the google mail type you are using. If you are using Google Apps mail (think http://mail.google.com/a/yourdomain.co.uk), then this is for you:


     
    
    
    
    
    
        
            true
            true
        
    



If you are using good ol' GMail, then this is for you:


     
    
    
    
    
    
        
            true
            true
        
    


Oh, and here's a final nugget to keep you going ... If it's all going south, and you need to peek under the proverbial, then add this to the javaMailProperties section, and you'll get protocol level debug to STDOUT - which can be really helpful.


    
        ...
        true
        ...
    

No comments:

Post a Comment