简体   繁体   English

java.lang.IllegalArgumentException:“原始”消息参数不能为null

[英]java.lang.IllegalArgumentException: The 'original' message argument cannot be null

I'm trying to send email using Spring. 我正在尝试使用Spring发送电子邮件。 Look at my code: 看我的代码:

public class Provider {

    private MailSender mailSender;
    private SimpleMailMessage message;

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

    public void setMessage(SimpleMailMessage message) {
        this.message = message;
    }

    public static String getTemplateName() {
        return "mainLayout/layout.jsp";
    }


    public void placeOrder() {

        // ... * Do the business calculations....
        // ... * Call the collaborators to persist the order

        // Create a thread safe "sandbox" of the message
        SimpleMailMessage msg = new SimpleMailMessage(this.message);
        msg.setTo("babobka@bk.ru");
        msg.setText("Hello");
        try {
            mailSender.send(msg);
        } catch (MailException ex) {
            // log it and go on
            System.err.println(ex.getMessage());
        }
    }

}

Don't know why it's not working. 不知道为什么它不起作用。 I have no idea what's wrong. 我不知道怎么了 I added all depencies like mail. 我添加了所有邮件等邮件。

SimpleMailMessage msg = new SimpleMailMessage(this.message);

我认为this.message为null。您应该首先对其进行初始化。

I too was getting this same issue. 我也遇到了同样的问题。 Actually You need to initialize the SimpleMailMessage object as below. 实际上,您需要按以下方式初始化SimpleMailMessage对象。

SimpleMailMessage msg = new SimpleMailMessage();

Below is my working code using which I am able to send the mail. 以下是我的工作代码,可用来发送邮件。 I will suggest you to have a try this. 我建议您尝试一下。

@Component
public class MailSenderApp {

@Autowired
JavaMailSender mailSender;

public void sendSimpleMail(int id){
        SimpleMailMessage msg = new SimpleMailMessage(); 
        msg.setCc("kumar@gmail.com");
        msg.setTo("kumar@gmail.com");
        msg.setSubject("Simple Message");
        msg.setText("Hello This is sample Mail to test");
    try{
        this.mailSender.send(msg);
    }
    catch(MailException ex) {
        System.err.println(ex.getMessage());            
    }
}

You will need to create a Bean of JavaMailSender as I have done it in config code snippet below. 您将需要创建JavaMailSender Bean,就像我在下面的配置代码片段中所做的那样。

 /**
 * Mail sender configuration
 * 
 */

@Bean
public JavaMailSender javaMailService() {
    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    javaMailSender.setHost("host");
    javaMailSender.setPort("port");
    javaMailSender.setUsername("username");
    javaMailSender.setPassword("password");
    javaMailSender.setJavaMailProperties(getMailProperties());
    return javaMailSender;
}

/**
 * Property setters for mail
 * 
 */
private Properties getMailProperties() {
    Properties properties = new Properties();
    properties.setProperty("mail.transport.protocol", "smtp");
    properties.setProperty("mail.smtp.auth", "true");
    properties.setProperty("mail.smtp.starttls.enable", "true");
    properties.setProperty("mail.debug", "true");
    return properties;
}

下面的语句解决了我的null错误:

SimpleMailMessage smm=new SimpleMailMessage(); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SWT java.lang.IllegalArgumentException:参数不能为null - SWT java.lang.IllegalArgumentException: Argument cannot be null java.lang.IllegalArgumentException: uri 不能是 null - java.lang.IllegalArgumentException: uri cannot be null 运行测试时获取“java.lang.IllegalArgumentException:解码参数不能为空” - Getting "java.lang.IllegalArgumentException: Decode argument cannot be null" when running tests java.lang.IllegalArgumentException:[断言失败]-此参数是必需的; 它不能为空 - java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null 为什么我得到java.lang.IllegalArgumentException:XPath为null时找不到元素错误消息 - Why do I get java.lang.IllegalArgumentException: Cannot find elements when the XPath is null error message 休眠查询java.lang.IllegalArgumentException:要遍历的节点不能为null - hibernate query java.lang.IllegalArgumentException: node to traverse cannot be null java.lang.IllegalArgumentException:迭代变量不能为null - java.lang.IllegalArgumentException: Iteration variable cannot be null ProfileCredentialsProvider:java.lang.IllegalArgumentException:配置文件不能为空 - ProfileCredentialsProvider: java.lang.IllegalArgumentException: profile file cannot be null Instagram Oauth2 java.lang.IllegalArgumentException: authorizationGrantType 不能是 null - Instagram Oauth2 java.lang.IllegalArgumentException: authorizationGrantType cannot be null java.lang.illegalargumentException:无法设置空的TableModel - java.lang.illegalargumentexception:cannot set a null TableModel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM