同步方法在smtp java中不起作用

时间:2018-01-27 06:41:33

标签: java email

我试图在使用smtp java发送邮件的同时制作Aysnchronous方法,但它总是引发异常

[SimpleAsyncTaskExecutor-3] ERROR             
o.s.a.i.SimpleAsyncUncaughtExceptionHandler -
    Unexpected error occurred invoking async method 'public java.lang.String
com.vedanta.vpmt.web.service.EmailLogService.sendEmailNotification(java.lang.String,java.lang.String,java.lang.String,java.lang.String)'.
java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.  at
org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)

代码就像

@Component
 @Slf4j
 @EnableAsync
 public class EmailLogService {
  @Async
    public String sendEmailNotification(String from, String to, String   subject, String text) {
    String message = "";
    if (to == null) {
        message = "EmailAddress can not be null";
    }
    int status;
    HashMap<String, String> map = new HashMap<>();
    map.put("from", from);
    map.put("to", to);
    map.put("subject", subject);
    map.put("text", text);
    try {
        JsonNode response = restServiceUtil.sendMailRequest(URLConstants.SEND_EMAIL, map, null, HttpMethod.POST);
        status = response.get(VedantaConstant.STATUS_CODE).intValue();
        if (status != 200) {
            message = "API not responded while sending email notification";
        }
        String data = response.get(VedConstant.DATA).toString();
        String mailStatus = OBJECT_MAPPER.readValue(data, new TypeReference<String>() {
        });
        EmailLog emailLog = new EmailLog();
        if (mailStatus.equalsIgnoreCase("success")) {
            emailLog.setStatus(1);
        }
        emailLog.setAssignedUserEmailid(from);
        emailLog.setCreatedBy(from);
        emailLog.setStage(1);
        emailLog.setCreatedOn(new Date());
        save(emailLog);
        message = mailStatus;
    } catch (VedantaWebException | IOException e) {
        String errorCode = e.getMessage();
        if (errorCode.equals("401")) {
            log.error("Token Expired.");
            throw new VedWebException("Error while fetching form listing", 401);
        }
        log.error("API not responded while sending email notification.");
        message = "API not responded while sending email notification";
    }
    return message;
}

0 个答案:

没有答案