5

我正在尝试使用 JavaMailSender 在我的 Spring MVC Web 应用程序和 Tomcat 7 中发送注册确认电子邮件,但它总是返回 NullPointerException。谁能帮我找出为什么我得到空指针?以下是我的配置设置:

web.xml

<!-- jndi mail session -->
<resource-ref>
    <description>
        Resource reference to a factory for javax.mail.Session
        instances that may be used for sending electronic mail
        messages, preconfigured to connect to the appropriate
        SMTP server.
    </description>
    <res-ref-name>mail/Session</res-ref-name>
    <res-type>javax.mail.Session</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

应用程序-servlet.xml

    <!-- Mail Sender bean definition -->

<bean id="smtpSession" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/mail/session"/>
</bean>

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="session" ref="smtpSession"/>
     <!--
    <property name="host" value="smtp.gmail.com" />
    <property name="port" value="465" />
    <property name="username" value="abc@gmail.com" />
    <property name="password" value="test123" />
    <property name="protocol" value="smtp" />


    <property name="defaultEncoding" value="UTF-8"/> 

    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.connectiontimeout">5000</prop>
            <prop key="mail.smtp.sendpartial">true</prop>
            <prop key="mail.smtp.userset">true</prop>
            <prop key="mail.mime.charset">UTF-8</prop>
            <prop key="mail.smtp.isSecure">true</prop>
            <prop key="mail.smtp.requiresAuthentication">true</prop>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.port">465</prop>
            <prop  key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
            <prop key="mail.smtp.socketFactory.fallback">false</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
            <prop key="mail.debug">true</prop>
        </props>
    </property>
     -->
</bean>

<bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage">
    <property name="from" value="abc@hotmail.com"/>       
</bean>

context.xml(在 tomcat conf 文件夹中)

<Context path="/MVC" docBase="MVC" debug="5" crossContext="false">
<Resource name="mail/session" 
    auth="Container"
        type="javax.mail.Session"
        username="abc@gmail.com"
        password="test23"
        mail.debug="true"
        mail.user="abc@gmail.com"
        mail.password="test123"
        mail.transport.protocol="smtp"
        mail.smtp.host="smtp.gmail.com"
        mail.smtp.auth="true"
        mail.smtp.port="25"
        mail.smtp.starttls.enable="true"
/>

发送邮件.java

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.MailParseException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;

import com.wbkit.mvc.domain.User;


@Service
public class SendMail 
{
@Autowired
private JavaMailSender mailSender;
@Autowired
private SimpleMailMessage simpleMailMessage;



public void confirmRegistrationMail(User user)
{


    MimeMessage message = mailSender.createMimeMessage();

    try
    {
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setFrom(simpleMailMessage.getFrom());
        helper.setTo(user.getEmail());
        helper.setSubject("MVC Registration");
        helper.setText("Hello " + user.getFirstName() + " "
              + user.getLastName() + ", \n" +
              " Your MVC Account Registration was successful. Your user ID is "
              + user.getEmail() + " and your password is "
              + user.getUserPass() + ".\n" +
              "You can now access the mobile applicatoin or the web application " 
              + " your user name (email) and password.");


    }
    catch (MessagingException e) 
    {
        throw new MailParseException(e);
    }
    mailSender.send(message);


}

public void requireApprovalMail(final User user)
{


}


}

堆栈跟踪:

4-Sep-2012 7:47:16 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [MVC] in context with path [/MVC] threw         exception [Request processing failed; nested exception is java.lang.NullPointerException]  with root cause
java.lang.NullPointerException
at com.wbkit.mvc.util.SendMail.confirmRegistrationMail(SendMail.java:79)
at com.wbkit.mvc.web.main.RegisterController.save(RegisterController.java:276)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at      org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1812)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

com.wbkit.mvc.util.SendMail.confirmRegistrationMail(SendMail.java:79) 处的 java.lang.NullPointerException 指向

MimeMessage message = mailSender.createMimeMessage();

在 SendMail.java 中。我删除了一些我拥有的注释代码。

感谢您的建议和帮助。

4

5 回答 5

1

我有同样的错误。它来自将 JavaMailSender bean 作为自动装配字段的类的手动实例化。在 OP 发布的代码中,这将是SendMail服务类。

这个问题揭示了这一现象:手动创建的对象的自动装配字段没有收到对 JavaMailSender bean 的任何实际引用,因为 Spring 不知道它应该在那时发挥它的魔力。之后就很明显了。

希望这可以帮助某人。

于 2016-09-22T17:32:03.093 回答
0

除了异常的详细信息之外,您似乎已经包含了所有内容。

此外,看起来您只是在编造属性名称。配置中的一堆属性不是有效的 JavaMail 属性。此外,请参阅此常见错误列表。

于 2012-09-04T06:36:25.387 回答
0

在应用程序启动时检查您的日志记录,看看您是否可以看到 JavaMailSender 被连接到 SendMail。您可能必须将 org.springframework 日志记录设置为 DEBUG。

在我看来,您的接线一定是错误的,尽管我还不明白为什么。

于 2012-09-10T06:00:08.653 回答
0

删除@AutowiredfromSimpleMailMessage并在confirmRegistrationMail()方法内部创建它的对象:

public void confirmRegistrationMail(User user) {
    SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
}

这可能是因为建议使用@Autowirefor 接口。例如JavaMailSender

于 2020-07-09T20:00:40.557 回答
-1

在您已完成自动装配的助手类上编写 @Service 注释。然后在您使用该类的类中使用@autowire 注释。

于 2015-01-23T00:41:25.807 回答