【问题标题】:How to send mail via outlook using spring boot?如何使用 Spring Boot 通过 Outlook 发送邮件?
【发布时间】:2017-11-08 08:46:05
【问题描述】:

我的 application.properties 文件包含以下配置:-

spring.mail.properties.mail.smtp.connecttimeout=5000
  spring.mail.properties.mail.smtp.timeout=3000
  spring.mail.properties.mail.smtp.writetimeout=5000
  spring.mail.host=smtp.office365.com
  spring.mail.password=password
  spring.mail.port=587
  spring.mail.username=test@outlook.com
  spring.mail.properties.mail.smtp.starttls.enable=true
  security.require-ssl=true
  spring.mail.properties.mail.smpt.auth=true

用于实现邮件服务器的 Java 类是:

@Component
public class SmtpMailSender {
@Autowired
private JavaMailSender javaMailSender;

public void sendMail(String to, String subject, String body) throws MessagingException {
    MimeMessage message = javaMailSender.createMimeMessage();
    MimeMessageHelper helper;
    helper = new MimeMessageHelper(message, true);//true indicates multipart message
    helper.setSubject(subject);
    helper.setTo(to);
    helper.setText(body, true);//true indicates body is html
    javaMailSender.send(message);
}
}

我的控制器类是:

@RestController
public class MailController {

@Autowired
SmtpMailSender smtpMailSender;

@RequestMapping(path = "/api/mail/send")
public void sendMail() throws MessagingException {
    smtpMailSender.sendMail("test123@outlook.com", "testmail", "hello!");
}
}

当我发送获取请求 (/api/mail/send) 时出现以下错误:

{
"timestamp": 1496815958863,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.mail.MailAuthenticationException",
"message": "Authentication failed; nested exception is 
javax.mail.AuthenticationFailedException: ;\n  nested exception 
is:\n\tjavax.mail.MessagingException: Exception reading response;\n  nested 
exception is:\n\tjava.net.SocketTimeoutException: Read timed out",
"path": "/api/mail/send"
}

任何帮助将不胜感激。

【问题讨论】:

  • 邮件服务器连接失败;嵌套异常是 com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp-mail.outlook.com,995;感谢您的帮助。
  • @user7294900 我尝试了给定链接中提供的解决方案,但没有成功。感谢您的帮助
  • 我现在收到此错误:{ "timestamp": 1496828104173, "status": 500, "error": "Internal Server Error", "exception": "org.springframework.mail.MailSendException ", "message": "失败的消息: javax.mail.MessagingException: 异常读取响应;\n 嵌套异常是:\n\tjava.net.SocketTimeoutException: 读取超时", "path": "/api/mail/发送”}
  • setFrom() 解决了我的问题。你应该接受答案。如果有任何问题,请发布它。请记住,如果您在公司外部,则不能将其发送到组织外部。并记住大多数邮件端口(如 25、587)被组织阻止。因此,请尝试禁用代理并尝试使用组织网络以外的其他网络关闭代理

标签: java spring spring-boot outlook office365


【解决方案1】:

您必须使用setFrom 方法指定sender 才能在outlook.com 上执行身份验证:

@Component
public class SmtpMailSender {

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender javaMailSender;

    public void sendMail(String to, String subject, String body) throws MessagingException {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper;
        helper = new MimeMessageHelper(message, true);//true indicates multipart message

        helper.setFrom(from) // <--- THIS IS IMPORTANT

        helper.setSubject(subject);
        helper.setTo(to);
        helper.setText(body, true);//true indicates body is html
        javaMailSender.send(message);
    }
}

outlook.com 会检查您是否没有试图假装自己是其他人。

【讨论】:

  • 我的也缺少发件人。感谢您的提示
  • 感谢您的提示,这引起了很多问题。
【解决方案2】:

application.properties

spring.mail.properties.mail.smtp.connecttimeout=5000
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.writetimeout=5000
spring.mail.host=smtp.office365.com
spring.mail.password=password
spring.mail.port=587
spring.mail.username=senderemail
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smpt.auth=true
support.email=senderemail

用于实现邮件服务器的 Java 类是:

@Component
public class SmtpMailSender {
@Autowired
private JavaMailSender javaMailSender;

public void sendMail(String to, String subject, String body) throws MessagingException {
    MimeMessage message = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);//true indicates multipart message
    helper.setSubject(subject);
    helper.setTo(to);
    helper.setText(body, true);//true indicates body is html
    helper.setFrom(env.getProperty("support.email")); //set sender email and get it from application properties
    helper.addAttachment("filename", new ClassPathResource("\\static\\path")); //You can add email attachment 
    javaMailSender.send(message);
}
}

控制器类

@RestController
public class MailController {

@Autowired
SmtpMailSender smtpMailSender;

@RequestMapping(path = "/api/mail/send")
public void sendMail() throws MessagingException {
    smtpMailSender.sendMail("test123@outlook.com", "testmail", "hello!");
}
}

请试试这个,如果它有效,请告诉我们

【讨论】:

  • 这里使用的电子邮件 ID 是什么?
【解决方案3】:

您需要这些选项:

spring:
  mail:
    protocol: smtp
    host: smtp.gmail.com
    username: email@..
    password: password..email
    port: 587
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: false
    test-connection: false  

如果它仍然没有运行,您必须确保您的源电子邮件可以通过允许不安全的连接来发送电子邮件。如果不是,它可能是代理。

【讨论】:

  • post是配置office365为邮件服务器。
【解决方案4】:

application.properties

spring.mail.properties.mail.smtp.connecttimeout=5000
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.writetimeout=5000
spring.mail.host=smtp-mail.outlook.com
spring.mail.password=password
spring.mail.username=username
spring.mail.properties.mail.store.protocol=pop3
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smpt.auth=true

您应该使用 POP3 协议而不是 IMAP 协议连接到 Outlook.com

这是JavaMAil Frequently Asked Question

【讨论】:

    猜你喜欢
    • 2017-10-15
    • 1970-01-01
    • 2020-05-01
    • 2015-11-13
    • 2018-07-05
    • 2021-01-11
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多