Java 未发现异常,但Spring MVC中未发送邮件
我是spring MVC的新手,通过spring发送电子邮件时遇到问题。不会发生异常,但邮件不会发送 my applicationContext.xmlJava 未发现异常,但Spring MVC中未发送邮件,java,spring,email,spring-mvc,Java,Spring,Email,Spring Mvc,我是spring MVC的新手,通过spring发送电子邮件时遇到问题。不会发生异常,但邮件不会发送 my applicationContext.xml <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.gmail.com" /> <prop
<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>
提前谢谢。。不会出现例外情况。但是邮件不发送吗?我们在SpringBoot 1.2.5中遇到了同样的问题。看起来最新版本的Java Mail需要另一个属性-
spring.Mail.properties.Mail.smtp.ssl.enable
设置为true
。有关详细信息,请参阅
另外,当我测试我的应用程序时,我发现仅仅给出常规的gmail密码已经不起作用了。我需要一个帐户,并且必须使用。您是否尝试使用简单的Java程序发送邮件?在您的实际代码中,您是否使用实际邮件id替换
发件人
&收件人
?我使用实际邮件id运行
@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");
}
}
}