0

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)

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

4 Comments

when u tried to initialize mailSender it give Cannot instantiate the type MailSender error message...
@vijayk: Add more details from stacktrace so that we can identify the exact problem.
I m just initiate the mailSender object but it give error when u just type at that time it give error..
@vijayk: I guess MailSender is a interface you need to use a implementation of that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.