SpringFramework邮件发送JAVA

时间:2016-10-09 16:34:59

标签: java spring

我正在尝试编写一个使用spring框架发送电子邮件的简单应用程序。我发现谷歌主机 - smtp.gmail.com和一个尝试使用id但我有这个例外。我确信我的gmail凭据是正确的。如果你知道如何解决这个问题,请写下评论。

谢谢!

[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

配置

 <?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("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);
    }

}

主要

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

    }
}

1 个答案:

答案 0 :(得分:1)

我找到了以下answer

  

有时/第一次谷歌会阻止任何第三方应用程序或使用您的代码登录您的帐户。当您在浏览器中登录自己的帐户时,系统会收到一条消息“Google阻止可疑的尝试登录您的帐户”...

     

点击“是你吗”并允许登录。

这适合您(优秀)日志中的消息:

  

请通过网络浏览器登录...然后重试。