Vicky
Vicky

Reputation: 9585

how to send mail Spring implemention using gmail smtp?

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

Upvotes: 3

Views: 7573

Answers (4)

Rytis Guntulis
Rytis Guntulis

Reputation: 331

You need to add lines:

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
sender.setJavaMailProperties(props);

Properties class is java.util.Properties.

First time you will get error "... Please log in via your web browser and then try again. ...", so you will need go to your google email box and read new letter. There will be link to turn off security setting.

Upvotes: 1

brisssou
brisssou

Reputation: 549

in a shorter, revised version of Saurabh post, you can simply:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl" p:host="smtp.gmail.com"
    p:port="587" p:username="[email protected]" p:password="aSmartPassWord">
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
            <prop key="mail.smtp.timeout">8500</prop>
        </props>
    </property>
</bean>

Upvotes: 7

skaffman
skaffman

Reputation: 403441

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).

Upvotes: 1

user262976
user262976

Reputation: 2074

here are a few examplasr:

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

Upvotes: 0

Related Questions