发送邮件测试
This commit is contained in:
@ -13,9 +13,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
|
||||
@RestController
|
||||
@Api(tags = "短信接口")
|
||||
@Api(tags = "短信邮件接口")
|
||||
@RequestMapping("/v1/sms")
|
||||
public class BizSmsEmailController {
|
||||
|
||||
@ -28,9 +29,9 @@ public class BizSmsEmailController {
|
||||
return BaseResponse.success(msgService.sendMsg(msgVO));
|
||||
}
|
||||
|
||||
@ApiOperation("发送短信")
|
||||
@ApiOperation("发送邮件")
|
||||
@PostMapping("/sendEmail")
|
||||
public BaseResponse<Boolean> saveMsg(@RequestBody @Validated BizSendEmailVO emailVO) {
|
||||
public BaseResponse<Boolean> saveMsg(@RequestBody @Validated BizSendEmailVO emailVO) throws IOException {
|
||||
return BaseResponse.success(msgService.sendEmail(emailVO));
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@ import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import java.util.List;
|
||||
@ -45,7 +46,10 @@ public class BizSendEmailVO {
|
||||
@ApiModelProperty(value = "URL方式或附件文件方式二选一:\n" +
|
||||
"varchar:附件URL长度不超过1000\n" +
|
||||
"File:附件文件(限制2M)\n")
|
||||
private String file;
|
||||
private String fileUrl;
|
||||
|
||||
@ApiModelProperty(value = "文件流")
|
||||
private MultipartFile file;
|
||||
|
||||
@ApiModelProperty(value = "批量发送邮箱")
|
||||
private List<String> sendToList;
|
||||
|
@ -5,10 +5,12 @@ import com.chinaunicom.mall.ebtp.extend.bizshortmessageemail.entity.BizSendEmail
|
||||
import com.chinaunicom.mall.ebtp.extend.bizshortmessageemail.entity.BizSendMsgVO;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Component
|
||||
public interface BizSmsEmailService {
|
||||
|
||||
Boolean sendMsg(BizSendMsgVO msgVO);
|
||||
|
||||
Boolean sendEmail(BizSendEmailVO emailVO);
|
||||
Boolean sendEmail(BizSendEmailVO emailVO) throws IOException;
|
||||
}
|
||||
|
@ -1,5 +1,10 @@
|
||||
package com.chinaunicom.mall.ebtp.extend.bizshortmessageemail.service.impl;
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import org.apache.commons.fileupload.FileItem;
|
||||
import org.apache.commons.fileupload.disk.DiskFileItem;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.common.base.entity.BaseResponse;
|
||||
@ -14,6 +19,7 @@ import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@ -50,7 +56,7 @@ public class BizSmsEmailServiceImpl implements BizSmsEmailService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean sendEmail(BizSendEmailVO emailVO) {
|
||||
public Boolean sendEmail(BizSendEmailVO emailVO) throws IOException {
|
||||
EshopMailPendingPO po = new EshopMailPendingPO();
|
||||
|
||||
po.setSendCenter("ebtp");
|
||||
@ -62,12 +68,16 @@ public class BizSmsEmailServiceImpl implements BizSmsEmailService {
|
||||
} else {
|
||||
po.setSendTo(emailVO.getSendTo());
|
||||
}
|
||||
//邮件内容
|
||||
//邮件主题
|
||||
po.setSubject(emailVO.getSubject());
|
||||
//邮件内容
|
||||
po.setMsg(emailVO.getMsg());
|
||||
po.setFileUrl("");
|
||||
|
||||
po.setFileType(emailVO.getFileType());
|
||||
if ("1".equals(emailVO.getFileType())) {
|
||||
po.setFileUrl(emailVO.getFileUrl());
|
||||
} else if ("2".equals(emailVO.getFileType())){
|
||||
po.setFile(this.getFile());
|
||||
}
|
||||
|
||||
logger.info("调用邮件发送接口入参:[{}]", po);
|
||||
BaseResponse baseResponse = notificationFeignClient.sendMail(po);
|
||||
@ -76,4 +86,32 @@ public class BizSmsEmailServiceImpl implements BizSmsEmailService {
|
||||
|
||||
return baseResponse.isSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @Description 返回MultipartFile文件
|
||||
* @return org.springframework.web.multipart.MultipartFile
|
||||
* @date 2019/1/5 11:08
|
||||
* @auther dell
|
||||
*/
|
||||
private MultipartFile getFile() throws IOException {
|
||||
String filePath = "D:\\文档\\3.0能力间调用接口-通知中心.docx";
|
||||
File file = new File(filePath);
|
||||
FileItem fileItem = new DiskFileItem("copyfile.txt", Files.probeContentType(file.toPath()),false,file.getName(),(int)file.length(),file.getParentFile());
|
||||
byte[] buffer = new byte[4096];
|
||||
int n;
|
||||
try (InputStream inputStream = new FileInputStream(file); OutputStream os = fileItem.getOutputStream()){
|
||||
while ( (n = inputStream.read(buffer,0,4096)) != -1){
|
||||
os.write(buffer,0,n);
|
||||
}
|
||||
//也可以用IOUtils.copy(inputStream,os);
|
||||
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
|
||||
System.out.println(multipartFile.getName());
|
||||
return multipartFile;
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user