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.
I think
this.message
is null.You should initialize it first.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.
You will need to create a Bean of JavaMailSender as I have done it in config code snippet below.
The statement below solved my null error: