【发布时间】:2016-05-18 15:11:36
【问题描述】:
我正在尝试使用我的 Spring Boot 应用程序连接到我的电子邮件服务器。当我从命令提示符 telnet localhost 25 时,我看到以下内容 -
220 Microsoft ESMTP MAIL 服务在星期三准备就绪 , 2016 年 5 月 18 日 11:09:30 -0400
但是,当我尝试从我的程序连接时,我看到以下错误
引起:org.springframework.mail.MailSendException:邮件服务器 连接失败;嵌套异常是 javax.mail.MessagingException: 无法连接到 SMTP 主机:localhost,端口:25;嵌套的 例外是:java.net.SocketException:权限被拒绝:连接。 失败的消息:javax.mail.MessagingException:无法连接到 SMTP主机:localhost,端口:25;嵌套异常是: java.net.SocketException:权限被拒绝:连接在
代码
public class MailUtil {
@Autowired
private JavaMailSender javaMailSender;
@Value("${email.from}")
private String from;
@Value("${email.subject}")
private String subject;
public void send() {
MimeMessage mail = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mail, true);
helper.setTo(emailAddress);
helper.setFrom(from);
helper.setSubject(subject);
helper.setText("Lorem ipsum dolor sit amet [...]");
} catch (MessagingException e) {
e.printStackTrace();
} finally {}
javaMailSender.send(mail);
}
}
配置
#Email Settings
spring.mail.host=localhost
spring.mail.port=25
【问题讨论】:
标签: java spring spring-boot