java.lang.IllegalArgumentException: The 'origi

2019-09-17 11:29发布

I'm trying to send email using 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.

3条回答
2楼-- · 2019-09-17 12:00
SimpleMailMessage msg = new SimpleMailMessage(this.message);

I think this.message is null.You should initialize it first.

查看更多
男人必须洒脱
3楼-- · 2019-09-17 12:09

I too was getting this same issue. Actually You need to initialize the SimpleMailMessage object as below.

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.

 /**
 * 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;
}
查看更多
孤傲高冷的网名
4楼-- · 2019-09-17 12:16

The statement below solved my null error:

SimpleMailMessage smm=new SimpleMailMessage(); 
查看更多
登录 后发表回答