views:

434

answers:

3

how to send mail Spring implemention using gmail smtp?

After executing main method getting exeception Exception in thread "main" java.lang.NoClassDefFoundError: javax/activation/FileTypeMap

public static void main(String[] args) {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost("smtp.gmail.com");
        sender.setPort(25);
        sender.setPassword("xxxxxxx");
        sender.setUsername("[email protected]");

        MimeMessage message = sender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message);
            helper.setTo("[email protected]");
            helper.setText("Thank you for ordering!");
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        sender.send(message);
    }

After putting activation.jar in class path getting this exception

javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. 21sm3277189pzk.7
A: 

here are a few examplasr:

http://static.springsource.org/spring/docs/1.2.x/reference/mail.html

after executing the method getting following exceptionException in thread "main" java.lang.NoClassDefFoundError: javax/activation/FileTypeMap at com.businesscaliber.utility.Mailer.main(Mailer.java:13)
Yashwant Chavan
seems like a jar is missing from the classpath... is activation.jar in there?
after adding activation jar getting another execeptionjavax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. 21sm3277189pzk.7
Yashwant Chavan
+1  A: 

To my knowledge, GMail only supports encrypted SMTP, and the error message is telling you that in a rather roundabout way. You'll need to configure Spring to use that instead of plaintext SMTP.

See this answer to a prior question which explains how to configure JavaMailSenderImpl to do this (I haven't tested it for myself, though).

skaffman
+1  A: 

You can see my blog post here at http://codersatwork.wordpress.com/2010/02/14/sending-email-using-gmail-smtp-server-and-spring-mail/ which explains how to use spring mail for sending email via gmail smtp server. Please let me know if this helps.

Saurabh