没有发现异常,但邮件未在Spring MVC中发送 [英] No exception found but mail not send in Spring MVC
问题描述
我是 spring MVC
的新手,在通过spring发送电子邮件时遇到问题.不会发生任何异常,但邮件不会发送.
I am new in spring MVC
i have got an issue while send email through spring . no exception will occur but mail not send.
我的applicationContext.xml
my applicationContext.xml
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="username" value="uname" />
<property name="password" value="pass" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.socketFactory.port">465</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.port">465</prop>
</props>
</property>
</bean>
我的控制器类
@Controller
public class WebController {
// System.out.println("suceees");
@Autowired
private JavaMailSender mailSender;
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index() {
return "index";
}
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public String redirect() {
sendMail();
return "redirect:finalPage";
}
@RequestMapping(value = "/finalPage", method = RequestMethod.GET)
public String finalPage() {
return "final";
}
public void sendMail() {
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom("sender");
helper.setTo("receiver");
helper.setSubject("hi");
helper.setText("welcome");
// attach the file
FileSystemResource file = new FileSystemResource(new File("/home/ajmal/Pictures/messi.jpg"));
helper.addAttachment("messi.jpg", file);//image will be sent by this name
mailSender.send(message);
} catch (MailException | MessagingException ex) {
System.err.println("error");
}
}
}
在此先感谢..不会发生任何异常.但是邮件无法发送?
thanks in advance .. no exception will occur. but mail not send ?
推荐答案
Spring Boot 1.2.5有时会遇到相同的问题.看起来与最新版本的Java Mail类似,现在需要另一个属性-将 spring.mail.properties.mail.smtp.ssl.enable
设置为 true
.请参阅这篇文章了解详情.
We had into the same problem sometime back, with Spring Boot 1.2.5. Looks like with the latest version of Java Mail, now another property is needed - spring.mail.properties.mail.smtp.ssl.enable
to be set as true
. See this post for details.
此外,当我测试我的应用程序时,我发现仅提供常规的gmail密码已不再起作用.我需要一个两步验证帐户,并且必须使用
Also, when I tested my application, I saw that merely giving the regular gmail password didn't anymore work. I needed a 2-step verified account, and had to use an application password.
这篇关于没有发现异常,但邮件未在Spring MVC中发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!