ello I am using SpringBoot to create a web-application and in the relevant code I am allowing a button to be clicked which removes an object from the database, and then sends an email to the email address of that object. Before I added the email part when the button was clicked it worked perfectly and the row was removed from the table, now however I get an error:
java.lang.IllegalStateException: No Scope registered for scope name 'restart'
I think it is to do with the context in the controller: Controller:
@RequestMapping("/rejectApplicant")
@ResponseBody
public String rejectApplicant(@RequestParam String id, HttpServletRequest request, HttpServletResponse response, Model model) {
Long athleteID = Long.parseLong(id);
Optional<Athlete> optionalAthlete = athleteAuditor.findAthleteById(athleteID);
Athlete athlete = optionalAthlete.get();
AbstractApplicationContext context = new AnnotationConfigApplicationContext(
EmailConfig.class);
MailService mailService = context.getBean("mailService", MailServiceImp.class);
System.out.println("////// " + mailService.toString());
String senderEmailId = "[email protected]";
String receiverEmailId = athlete.getEmail();
System.out.println("////// " + receiverEmailId);
String subject = "Your time at Lyons";
String message = "Dear " + athlete.getName() + "How goes it?";
mailService.sendEmail(senderEmailId, receiverEmailId, subject, message);
context.close();
athleteAuditor.deleteAthlete(athleteID);
return "applicants";
}
//CONFIG
@Configuration
@ComponentScan(basePackages = "xda")
public class EmailConfig {
u/Bean
public JavaMailSender getMailSender()
{
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
// Using Gmail SMTP configuration.
mailSender.setHost("smtp.gmail.com");
mailSender.setPort(587);
/*
* Use your gmail id and password
*/
mailSender.setUsername("[email protected]");
mailSender.setPassword("Y");
Properties javaMailProperties = new Properties();
javaMailProperties.put("mail.smtp.starttls.enable", "true");
javaMailProperties.put("mail.smtp.auth", "true");
javaMailProperties.put("mail.transport.protocol", "smtp");
javaMailProperties.put("mail.debug", "true");
mailSender.setJavaMailProperties(javaMailProperties);
return mailSender;
}
}
//MAIL SERVICE IMP
@Service("mailService")
public class MailServiceImp implements MailService
{
u/Autowired
JavaMailSender mailSender;
public void sendEmail(final String senderEmailId, final String receiverEmailId,
final String subject, final String message)
{
MimeMessagePreparator prep = new MimeMessagePreparator()
{
public void prepare(MimeMessage mimeMessage) throws Exception
{
mimeMessage.setFrom(senderEmailId);
mimeMessage.setRecipient(Message.RecipientType.TO,
new InternetAddress(receiverEmailId));
mimeMessage.setSubject(subject);
mimeMessage.setText(message);
}
};
try
{
mailSender.send(prep);
System.out.println("Successfully sent message");
}
catch (Exception exe)
{
exe.printStackTrace();
}
}
}
//mailService
public interface MailService
{
public void sendEmail(final String senderEmailId, final String receiverEmailId,
final String subject, final String message);
}
Sorry there is so much code, I have no idea what is causing it, it is my first time trying to use Spring-Boot-Starter-Mail, thanks for any help!
I think the problem is
@ComponentScan
. I would check if@ComponentScan
is used just one time. From Baeldung Spring Component Scanning: