vijayk
vijayk

Reputation: 2753

sending email using spring framework get error

Following is the my code to sending the email.

import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

public class TestArrayList {
    private MailSender mailSender;
    public void sendMail(String from, String to, String subject, String msg) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(msg);
        mailSender.send(message);   
    }
    public static void main(String[] args) {
        TestArrayList obj=new TestArrayList();
        obj.sendMail("[email protected]", "[email protected]",  "Testing123", "Testing only \n\n Hello Spring Email Sender");

        }

}

But i got the following error message I don't get it where I m wrong.

Exception in thread "main" java.lang.NullPointerException
    at TestArrayList.sendMail(TestArrayList.java:16)
    at TestArrayList.main(TestArrayList.java:20)

Upvotes: 0

Views: 418

Answers (1)

Ajinkya
Ajinkya

Reputation: 22710

mailSender.send(message);   

Here mailSender is null as it is not initialized.
Initialize it

private MailSender mailSender = new MailSender();  

or use Spring annotation to inject it.

Upvotes: 2

Related Questions