How to send Spring mail using gmail smtp?

how to send spring mail using gmail smtp?

After executing the main method of getting exeception, the exception in the "main" thread 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("businesscaliber@gmail.com");

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

        sender.send(message);
    }

After put.IAR in class path gets this exception

javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. 21sm3277189pzk.7
+3
source share
5 answers

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

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl" p:host="smtp.gmail.com"
    p:port="587" p:username="donotreply@host.com" 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>
+7
source

As far as I know, GMail only supports encrypted SMTP, and the error message tells you that this is a pretty workaround. You need to configure Spring to use this instead of plaintext SMTP.

. , , JavaMailSenderImpl ( , ).

+1

:

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

- java.util.Properties.

"... -, ...", google . .

+1

, XML,

<int:channel id="outboundMail"></int:channel>

<int-mail:outbound-channel-adapter id="outboundAdapter" 
        channel="outboundMail"
        host="smtp.gmail.com" 
        username="whateverl@gmail.com" 
        password="whatever" 
        port="587"
        java-mail-properties="javaMailProperties"/>

<util:properties id="javaMailProperties">
    <prop key="mail.debug">false</prop>
    <prop key="mail.smtp.auth">true</prop>
    <prop key="mail.smtp.starttls.enable">true</prop>
    <prop key="mail.smtp.timeout">8500</prop>
</util:properties>
0

Source: https://habr.com/ru/post/1732642/


All Articles