简体   繁体   English

发送带有附件的邮件-angularJS + Java Spring

[英]Sending mail with attachment - angularJS + Java Spring

I've got working email sender, but without attachments. 我有工作正常的电子邮件发件人,但没有附件。 I tried something like this: 我尝试过这样的事情:

Simple input in HTML: HTML的简单输入:

<input type="file" simple-change="uploadFiles($event)"/>

JS file picker: JS文件选择器:

    $scope.uploadFiles = function (event) {
    $scope.file= event.target.files[0];
};

And JS email sender: 和JS电子邮件发件人:

$scope.sendEmail = function(){
    $scope.emailData = new EmailData();
    $scope.emailData.to = "example@gmail.com";
    $scope.emailData.from = "boss@gmail.com";
    $scope.emailData.subject = "Errors";
    $scope.emailData.title = $scope.topic;
    $scope.emailData.description = $scope.description;
    $scope.emailData.template = "templateErrors";

    $http.post("sendemail/attachment", $scope.emailData, $scope.file, {headers: {'Content-Type': undefined} })
    .then(function (response) {
        $scope.succes =  true;
    },
    function(fail) {
        $scope.error = true;
    });
}

Java Controller: Java控制器:

@RequestMapping(value = "/attachment", method = RequestMethod.POST)
public EmailStatus sendEmailAttach(@RequestBody EmailData emailData, CommonsMultipartFile file) {
    return emailSenderAttachment.sendDataAttach(emailData, file);
}

but still got error 500: ""message":"org/apache/commons/fileupload/FileUploadException"" 但仍然出现错误500:“”消息“:” org / apache / commons / fileupload / FileUploadException“”

Please help :( 请帮忙 :(

Try below code use $upload.upload instead of $http.post for upload file to server. 尝试以下代码,使用$upload.upload而不是$http.post将文件上传到服务器。

AngularJs AngularJs

$upload.upload({
    method : 'POST',
    url : 'sendemail/attachment',
    file : $scope.file,
    data : $scope.emailData
}).success(function(response) {
    console.log("Success");
}).error(function(error) {
    console.log("Error");
});

Spring Controller 弹簧控制器

@RequestMapping(value = "/attachment", method = RequestMethod.POST)
public EmailStatus sendEmailAttach(@RequestParam("file") MultipartFile file, EmailData emailData) {
    return emailSenderAttachment.sendDataAttach(emailData, file);
}

For more details you can refer my answer from this post Multipart File Upload using AngularJS and SpringMVC 有关更多详细信息,您可以从本文中使用AngularJS和SpringMVC的“分段文件上传”中引用我的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM