使用弹簧框架工作发送电子邮件时出错

时间:2017-08-25 07:13:50

标签: 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 class 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>

2 个答案:

答案 0 :(得分:1)

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

addEvo <- function(table,var,var2014,var2015) {
var <- enquo(var)
var2014 <- enquo(var2014)
var2015 <- enquo(var2015)
evoName <- paste0("evo",var)[2]
table %>%
 mutate(!!evoNom := (!!var2015) - (!!var2013)) %>%
  return()

在您的代码中,<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.templateMessage会导致异常。 希望它有所帮助!

答案 1 :(得分:0)

使用错误的构造函数,使用:

SimpleMailMessage msg = new SimpleMailMessage();

代替

SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);

您还需要创建mailSender:

private JavaMailSenderImpl createMailSender() {
    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    javaMailSender.setUsername(configuration.getUsername());
    javaMailSender.setPassword(configuration.getPassword());
    javaMailSender.setHost(configuration.getSmtpHost());
    javaMailSender.setPort(configuration.getSmtpPort());

    javaMailSender.setDefaultEncoding("UTF-8");

    Properties properties = javaMailSender.getJavaMailProperties();
    if (StringUtils.isNotEmpty(configuration.getFrom())) {
        properties.put("mail.smtp.from", configuration.getFrom());
    }

    properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    properties.put("mail.smtp.socketFactory.port", configuration.getSmtpPort());
    properties.put("mail.smtp.socketFactory.fallback", false);
    properties.put("mail.smtp.auth", true);
    properties.put("mail.smtp.starttls.enable", true);
    properties.put("mail.transport.protocol", "smtp");
    properties.put("mail.debug", configuration.isDebug());

    return javaMailSender;
}

只需使用您的值替换配置调用。