0

I am trying to write a simple application which is sending email using spring framework. I found google host - smtp.gmail.com and a tried to used id but i have this exception. I am sure that my gmail credentials are correct. Please if you know how to fix this, write e comment.

Thank you!

[main] INFO org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@17f6480: startup date [Sun Oct 09 19:22:27 EEST 2016]; root of context hierarchy
[main] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [config.xml]
DEBUG: JavaMail version 1.4ea
DEBUG: java.io.FileNotFoundException: /usr/lib/jvm/java-8-oracle/jre/lib/javamail.providers (No such file or directory)
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.address.map
DEBUG: java.io.FileNotFoundException: /usr/lib/jvm/java-8-oracle/jre/lib/javamail.address.map (No such file or directory)
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 smtp.gmail.com ESMTP g6sm1334170wjy.3 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

EHLO nenov
250-smtp.gmail.com at your service, [78.83.59.144]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO nenov
250-smtp.gmail.com at your service, [78.83.59.144]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
bmVub3YubWFpbEBnbWFpbC5jb20=
334 UGFzc3dvcmQ6
bnVsbHBvaW50ZXI5MA==
534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbsc
534-5.7.14 rXV_hpmvWxIMqP_8LiYKzraKerlPQqlL6IQjP_h_Sv1MT3473Sgzidpx477v8Li5aysp1V
534-5.7.14 KoCp36cralg7_ilb0NfdlavrnVCzSk37L2-Lk5Y0WF2c3CoB3_TaYSvBIMML-XmrTCWyHb
534-5.7.14 kVw5kw8LH_k2Eet7acKpJFpcz06GBA4oigTzYjTL7UQLZPVNZjUyLKRGnVCYktqyc-XQU1
534-5.7.14 yZNNNg1_MZrx3PqP3O_wjGUGFo8F4> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/78754 g6sm1334170wjy.3 - gsmtp

config

 <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd 
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

        <context:component-scan base-package="com.package" />

        <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
            <property name="host" value="smtp.gmail.com" />
            <property name="port" value="587" />
            <property name="username" value="username@gmail.com" />
            <property name="password" value="password" />

            <property name="javaMailProperties">
                <props>
                    <prop key="mail.transport.protocol">smtp</prop>
                    <prop key="mail.smtp.auth">true</prop>
                    <prop key="mail.smtp.starttls.enable">true</prop>
                    <prop key="mail.debug">true</prop>
                </props>
            </property>
        </bean>
    </beans>

Service

@Service("sendEmail")
public class EmailApi {

    @Autowired
    private MailSender mailSender;

    public void readyToSendEmail(String toAddress, String fromAddress, String subject, String msgBody) {

        SimpleMailMessage simpleMailMsg = new SimpleMailMessage();
        simpleMailMsg.setFrom(fromAddress);
        simpleMailMsg.setTo(toAddress);
        simpleMailMsg.setSubject(subject);
        simpleMailMsg.setText(msgBody);
        mailSender.send(simpleMailMsg);
    }

}

Main

public class SendEmailTest {

    @SuppressWarnings("resource")
    public static void main(String[] args) {

        String confFile = "config.xml";
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(confFile);

        EmailApi emailApi = (EmailApi) context.getBean("sendEmail");
        String toAddr = "email1";
        String fromAddr = "email2";

        String subject = "subject";

        String body = "Hello, MATE :)";
        emailApi.readyToSendEmail(toAddr, fromAddr, subject, body);

    }
}
cool
  • 33
  • 8
  • I think Google is blocking you. This link may help you: [Change account access for less secure apps](https://support.google.com/accounts/answer/6010255) – Tobías Oct 09 '16 at 16:52

1 Answers1

1

I found the following answer:

Sometime/first time google prevent the sign in to your account by any third party application or using your code. when you will sign in to your account in browser, you will get a message "Google prevents a Suspicious attempt to sign in to your account"...

click on the "Was it you" and allow the sign in.

This fits to the message in your (excellent) log:

Please log in via your web browser and ... then try again.

Community
  • 1
  • 1
mm759
  • 1,404
  • 1
  • 9
  • 7