java - SpringFramework邮件发送JAVA

标签 java spring

我正在尝试编写一个使用 spring 框架发送电子邮件的简单应用程序。我找到了 google 主机 - smtp.gmail.com 并尝试使用 id 但我有这个异常(exception)。我确信我的 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);

    }
}

最佳答案

我发现了以下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.

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

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

关于java - SpringFramework邮件发送JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39945921/

相关文章:

java - 尝试用Java创建一个堆栈计算器

java - 在 JTextComponent 中禁用双击选择

java - 用 Java 将任意 URL 的 HTML 内容保存在文本文件中

java - Spring MVC 中特定路径变量的可能值列表

java - @RestController 中的动态 @RequestParam

java - Spring应用程序如何处理注销

java - 使用 swipeview 时如何将 Java 文件链接到 UI xml 文件?

javascript - 将音频文件/blob 从 UI 发送到 Servlet 以保存在服务器上。

java - spring mvc中对象的绑定(bind)列表

spring - 在代理中转发主机名和路径