2016-10-09 2 views
0

Ich versuche, eine einfache Anwendung zu schreiben, die E-Mail mit Spring Framework sendet. Ich fand Google Host - smtp.gmail.com und eine versuchte ID zu verwenden, aber ich habe diese Ausnahme. Ich bin mir sicher, dass meine Google Mail-Zugangsdaten korrekt sind. Bitte, wenn Sie wissen, wie Sie das beheben können, schreiben Sie einen Kommentar.SpringFramework Mail senden JAVA

Vielen Dank!

[main] INFO org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org[email protected]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="[email protected]" /> 
      <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> 

Dienst

@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); 
    } 

} 

Haupt

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

    } 
} 
+0

Ich denke, Google blockiert Sie. Dieser Link kann Ihnen helfen: [Ändern des Kontozugriffs für weniger sichere Apps] (https://support.google.com/accounts/answer/6010255) –

Antwort

1

fand ich den folgenden answer:

Irgendwann/erstmals Google das Zeichen in Ihrem Konto von Dritter Anwendung oder mit Ihrem Code verhindern. Wenn Sie sich im Browser in Ihrem Konto anmelden, erhalten Sie die Meldung "Google verhindert einen verdächtigen Versuch, sich in Ihrem Konto anzumelden" ...

Klicken Sie auf "War es Sie" und erlauben Sie das Anmelden.

Dies passt zu der Nachricht in Ihrem (ausgezeichnet) log:

Bitte melden Sie sich über Ihren Web-Browser und ... versuchen Sie es erneut.

+0

Wie dumm ich bin ... Danke! – cool