java - 使用spring框架发送邮件时出现错误

标签 java spring smtp

我正在尝试使用 spring 框架发送电子邮件。我在尝试运行代码时收到以下错误。我使用intellij IDE。

Exception in thread "main" java.lang.IllegalArgumentException: The 'original' message argument cannot be null
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.mail.SimpleMailMessage.<init>(SimpleMailMessage.java:73)
at com.howtodoinjava.demo.model.SimpleOrderManager.placeOrder(SimpleOrderManager.java:34)
at com.howtodoinjava.demo.model.SimpleOrderManager.main(SimpleOrderManager.java:48)

这是我的代码

package com.howtodoinjava.demo.model;

//import org.springframework.core.annotation.Order;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

公共(public)类 SimpleOrderManager {

private MailSender mailSender;
private SimpleMailMessage templateMessage;

//public String order;
//public String customer;

public void setMailSender(MailSender mailSender) {
    this.mailSender = mailSender;
}

public void setTemplateMessage(SimpleMailMessage templateMessage) {
    this.templateMessage = templateMessage;
}

public void placeOrder() {

    // Do the business calculations...

    // Call the collaborators to persist the order...

    // Create a thread safe "copy" of the template message and customize it
    SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
    msg.setTo("mygmail@gmail.com");
    msg.setText("Message");
    try{
        this.mailSender.send(msg);
    }
    catch (MailException ex) {
        // simply log it and go on...
        System.err.println(ex.getMessage());
    }
}

public static void main(String []args){
     SimpleOrderManager obj = new SimpleOrderManager();
     obj.placeOrder();
}

}

我现在已经对消息和电子邮件地址进行了硬编码。

编辑:

现在删除 this.templateMessage 后出现此错误。

Exception in thread "main" java.lang.NullPointerException
at com.howtodoinjava.demo.model.SimpleOrderManager.placeOrder(SimpleOrderManager.java:32)
at com.howtodoinjava.demo.model.SimpleOrderManager.main(SimpleOrderManager.java:42)

这是我的 spring-servlet.xml 文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:component-scan base-package="com.howtodoinjava.demo" />
<mvc:annotation-driven />

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="mail.mycompany.com"/>
</bean>

<!-- this is a template message that we can pre-load with default state -->
<bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
    <property name="from" value="customerservice@mycompany.com"/>
    <property name="subject" value="Your order"/>
</bean>

<bean id="orderManager" class="com.mycompany.businessapp.support.SimpleOrderManager">
    <property name="mailSender" ref="mailSender"/>
    <property name="templateMessage" ref="templateMessage"/>
</bean>

最佳答案

假设您正在关注 official Spring tutorial ,您忘记添加 beans 定义。

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="mail.mycompany.com"/>
</bean>

<!-- this is a template message that we can pre-load with default state -->
<bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
    <property name="from" value="customerservice@mycompany.com"/>
    <property name="subject" value="Your order"/>
</bean>

<bean id="orderManager" class="com.mycompany.businessapp.support.SimpleOrderManager">
    <property name="mailSender" ref="mailSender"/>
    <property name="templateMessage" ref="templateMessage"/>
</bean>

在您的代码中,this.templateMessagenull ,这会导致异常。 希望对您有帮助!

关于java - 使用spring框架发送邮件时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45876169/

相关文章:

c# - 如何将现有的 sendgrid 帐户链接到 Azure

properties - 属性文件中的log4j StringToMatch

java - 未能延迟初始化角色 : br. com.coderi.adi.model.Usuario.papeis 的集合,无法初始化代理 - 无 session

java - 如何在spring mvc @Controller中返回错误消息

spring - 方法不允许 405,在 grails 从 2.5.2 升级到 3.3.11 后点击 POST 请求时

java - kubernetes 中的 UnknownHostException

java - 无法将 java.lang.String 类型的对象转换为 com.mycare.OtherClass 类型

java - 如何使用 Selenium 定位不稳定的表元素?

java - 定义在将 Spring 事务管理与 hibernate 一起使用时不在事务中运行的方法

Perl Mail::Sendmail 无法连接到 SMTP 服务器