Spring MVC - 发送电子邮件,Bean创建

时间:2016-10-26 13:29:48

标签: email spring-mvc

我试图设置我的Spring MVC应用程序来发送电子邮件,但我认为我在配置中做错了什么

在我的AppConfig中,我实例化Bean:

@Bean
    public JavaMailSender mailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setDefaultEncoding("UTF-8");


        return mailSender;
    }

然后在我的application.properties中我有:

mail.port= 465
mail.host= smtp.gmail.com
mail.username= username
mail.password= password
emailTo = info@beingsmart.it
emailSubject = ALERT - ePark Server 

mail.smtp.auth = true
mail.smtp.starttls.enable = true

mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.properties.mail.smtp.socketFactory.fallback = false

当我尝试发送电子邮件时

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.mail.javamail.JavaMailSender]: Factory method 'mailSender' threw exception; nested exception is java.lang.NoClassDefFoundError: javax/mail/AuthenticationFailedException
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 49 more
Caused by: java.lang.NoClassDefFoundError: javax/mail/AuthenticationFailedException
    at it.besmart.config.AppConfig.mailSender(AppConfig.java:128)
    at it.besmart.config.AppConfig$$EnhancerBySpringCGLIB$$b2a09c0.CGLIB$mailSender$7(<generated>)
    at it.besmart.config.AppConfig$$EnhancerBySpringCGLIB$$b2a09c0$$FastClassBySpringCGLIB$$dd65f050.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:318)
    at it.besmart.config.AppConfig$$EnhancerBySpringCGLIB$$b2a09c0.mailSender(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 50 more
Caused by: java.lang.ClassNotFoundException: javax.mail.AuthenticationFailedException
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
    ... 61 more

EDIT 我将javax邮件添加到我的pom

<dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.5.6</version>
        </dependency>

现在,在尝试发送电子邮件时,我有一个不同的例外

java.lang.NoClassDefFoundError: com/sun/mail/util/MessageRemovedIOException

它看起来像是一个身份验证错误,但我认为JavaMailSender没有在我的application.properties中进行设置,可能是吗?

由于

1 个答案:

答案 0 :(得分:0)

似乎缺少依赖性而不是其他任何东西。如果使用spring boot具有依赖性:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

如果你不这样做,我建议你看看那个POM,找出丢失的东西。

在您的特定情况下,您似乎需要:

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.5.5</version>
</dependency>

通用建议:

  1. ClassNotFoundException表示您需要的类不在类路径中。它通常缺少依赖
  2. Google查找哪个jar包含缺少的类
  3. 在maven central上找到它的库
  4. 添加到依赖项