我试图在用户的帐户注册后向其发送电子邮件,我可以让我的代码在单独的项目中工作(简单的表单输入等),但是当我尝试将相同的代码添加到我的应用程序时,它会出现以下错误
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException error
Could not autowire method: public void com.finalProject.school.controller.AppController.setMailSender(org.springframework.mail.javamail.JavaMailSender); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSender] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
这是我将新用户添加到数据库的代码(工作正常)以及发送电子邮件的代码,这就是给我带来错误的代码
@RequestMapping(value = { "/newuser" }, method = RequestMethod.POST)
public String saveUser(@Valid User user, BindingResult result, @RequestParam("firstName") String firstName,
@RequestParam("password") String password, @RequestParam("email") String emailAddress,
ModelMap model, Model model2) {
if (result.hasErrors()) {
return "registration";
}
if(!userService.isUserSSOUnique(user.getId(), user.getSsoId())){
FieldError ssoError =new FieldError("user","ssoId",messageSource.getMessage("non.unique.ssoId", new String[]{user.getSsoId()}, Locale.getDefault()));
result.addError(ssoError);
return "registration";
}
String status = null;
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true,"UTF-8");
helper.setFrom("Administrator");
helper.setTo(emailAddress);
helper.setSubject("Registration confirmation");
Map<String, Object> params = new HashMap<String, Object>();
params.put("firstName", firstName);
params.put("password", password);
String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "spring/emailRegistration.vm", "UTF-8", params);
helper.setText(text, true);
mailSender.send(message);
status = "Confirmation email is sent to your address (" + emailAddress + ")";
} catch (MessagingException e) {
status = "There was an error in email sending. Please check your email address: " + emailAddress;
}
model2.addAttribute("message", status);
userService.saveUser(user);
model.addAttribute("success", "User " + user.getFirstName() + " "+ user.getLastName() + " registered successfully");
model.addAttribute("loggedinuser", getPrincipal());
//return "success";
return "registrationsuccess";
}
有什么办法可以解决这个问题吗?
应用程序配置类
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.finalProject.school" />
最佳答案
如果您使用的是 Spring Boot,您可能没有在 application.properties
文件中包含以下属性。
spring.mail.host = smtp.gmail.com
spring.mail.username = abc@abc.com
spring.mail.password = xyz
spring.mail.port=587
spring.mail.properties.mail.smtp.starttls.enable = true
spring.mail.protocol=smtp
更新
好的。将以下内容放入您的配置文件中。
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="myemail1@gmail.com" />
<property name="password" value="password!" />
<!-- The name of the property, following JavaBean naming conventions -->
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
关于java - Spring MVC 注册时发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48481165/