如何在春季启动应用程序中发送不回复的电子邮件

时间:2018-10-30 06:35:37

标签: email spring-boot

我试图在项目人员配置发生变化时向项目经理发送电子邮件。为此,我想从no-reply@example.com发送电子邮件,如何使用Spring Boot做到这一点?

预先感谢您的建议和答案。

尝试过这样的事情:

@RequestMapping(value = "/sendEmail", method = { RequestMethod.GET }, produces = { "application/json" })
    public ResponseEntity<CustomResponse> sendEmail() {

        LOG.debug("Start sendEmail with data, toAddress");

        HttpHeaders httpHeaders = commonUtil.setHeaders();

        HttpStatus httpStatus = HttpStatus.OK;

        CustomResponse customResp = new CustomResponse();

        String description;

        try {

            String to = "bmanikanta415@gmail.com";

            String from = "no-reply@example.com";

            Properties props = System.getProperties();

            Session session = Session.getDefaultInstance(props, null);

            Message msg = new MimeMessage(session);

            msg.setFrom(new InternetAddress(from));

            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

            msg.setSubject("Your Example.com account has been activated");

            msg.setText("This is a test");

            Transport.send(msg);

            description = Constants.SUCCESS;

            customResp = commonUtil.getCustomResponse(httpStatus, description, description);

        } catch (

        Exception e) {

            LOG.error("Exception in sendEmail: {}", e);

            description = Constants.FAILED;

            customResp = commonUtil.getCustomResponse(httpStatus, description, description);

        }

        ResponseEntity<CustomResponse> responseEntity = new ResponseEntity<CustomResponse>(customResp, httpHeaders,
                httpStatus);

        LOG.debug("End sendEmail with return data: {}", responseEntity);

        return responseEntity;

    }

0 个答案:

没有答案