[英]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.