使用Spring和AngularJS发送电子邮件?

时间:2018-04-28 04:44:57

标签: java angularjs spring email javamail

我不知道该怎么做。我搜索了很多教程和指南,但没有一个有效。我不知道为什么,但我认为它们已经过时了,这让事情变得如此复杂。

我有一个使用AngularJS和Spring的Maven项目。我希望将Gmail用作我的发件人。基于我的研究,我必须在使用ng-click

后在我的AngularJS控制器中执行类似的操作
$scope.sendEmail = function(){
        $scope.emailData.to = "to@gmail.com";
        $scope.emailData.subject = "Forgot Password";
        $scope.emailData.body = "Description here"

        EmailService.sendEmail($scope.emailData).then(function (response) {
            if(response.status=200){
                //sweetalert here
            }
        });


    }

我的EmailService

angular.module('EmailService', []).factory('EmailService', ["$http", function ($http) {
    return {
        sendEmail: function (email) {
            return $http.post("/email/", email).then(function successCallback(response) {

                return response;
            }, function errorCallback(response) {
                return response;
                //error
            })
        },

    }
}])

我的Java中有一个看起来像这样的控制器:

@RestController
@RequestMapping("/email")
public class EmailController {

    @Autowired
    EmailServiceImpl emailService;

    @RequestMapping( method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Email sendEmail(@RequestBody Email email) {
        return emailService.create(email);
    }
 }

Email.java包含

    private String toAddress;
    private String subject;
    private String body;

使用getter和setter,我的EmailServiceImpl.java

@Service("emailService")
public class EmailServiceImpl {

    public Email create(Email email) {

        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("smtp.gmail.com");
        mailSender.setPort(587);

        mailSender.setUsername("my.gmail@gmail.com");
        mailSender.setPassword("password");

        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.debug", "true");


        return email;
    }
}

那么下一步是什么?

注意:我正在使用JPA而我正在使用1.5.6.RELEASE版本。

0 个答案:

没有答案