如何使用spring创建自动回复gmail?

时间:2017-06-15 18:42:14

标签: java spring hibernate jsp

当我使用crud操作保存员工时,它工作正常,但是一旦我调用此方法: mailSender.send(消息); 我收到500错误。

我应该在使用crud操作保存员工时,一旦保存,然后将自动回复邮件发送给对应的员工?

/*Employee.class*/ @Entity
@Table(name = "EMP_TBL")
public class Employee implements Serializable {


private static final long serialVersionUID = -3465813074586302847L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;



@Column
private String username;

@Column
private String firstname;

@Column
private String lastname;

@Lob
private Blob content;
public Blob getContent() {
    return content;
}

public void setContent(Blob content) {
    this.content = content;
}

@Column
private String mobile;

public String getMobile() {
    return mobile;
}

public void setMobile(String mobile) {
    this.mobile = mobile;
}

@Column
private String email;

@Column
private String payable_amount;

@Column
private String plans;

@Column
private String id_proof;



public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getFirstname() {
    return firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

public String getPayable_amount() {
    return payable_amount;
}

public void setPayable_amount(String payable_amount) {
    this.payable_amount = payable_amount;
}

public String getPlans() {
    return plans;
}

public void setPlans(String plans) {
    this.plans = plans;
}

public String getId_proof() {
    return id_proof;
}

public void setId_proof(String id_proof) {
    this.id_proof = id_proof;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}



public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}


}

/*EmployeeService.class*/
public interface EmployeeService {

public void addEmployee(Employee employee);

public List<Employee> getAllEmployees();

public void deleteEmployee(Integer employeeId);

public Employee getEmployee(int employeeid);

public Employee updateEmployee(Employee employee);

public void sendMail(String dear, String content);

}
 /*EmployeeServiceImpl.class*/
 @Service
 @Transactional
 public class EmployeeServiceImpl implements EmployeeService {

@Autowired
private EmployeeDao employeeDAOImpl;
Employee Employee;
private JavaMailSender mailSender;
private SimpleMailMessage simpleMailMessage;

@Transactional
public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
    this.simpleMailMessage = simpleMailMessage;
}

@Transactional
public void setMailSender(JavaMailSender mailSender) {
    this.mailSender = mailSender;
}
@Transactional
public void addEmployee(Employee employee) {
    employeeDAOImpl.addEmployee(employee);
}

@Transactional
public List<Employee> getAllEmployees() {
    return employeeDAOImpl.getAllEmployees();
}

@Transactional
public void deleteEmployee(Integer employeeId) {
    employeeDAOImpl.deleteEmployee(employeeId);
}
@Transactional
public Employee getEmployee(int empid) {
    return employeeDAOImpl.getEmployee(empid);
}
@Transactional
public Employee updateEmployee(Employee employee) {

    return employeeDAOImpl.updateEmployee(employee);
}
@Transactional
public void setEmployeeDAO(EmployeeDao employeeDAO) {
    this.employeeDAOImpl = employeeDAO;
}
@Transactional
public void sendMail(String dear, String content) {
MimeMessage message = mailSender.createMimeMessage();

    try{
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setFrom(simpleMailMessage.getFrom());
        helper.setTo(Employee.getEmail());
        helper.setSubject(simpleMailMessage.getSubject());
        helper.setText(String.format(
                simpleMailMessage.getText(), dear, content));

        FileSystemResource file = new FileSystemResource("D:\\log.pdf");

        helper.addAttachment(file.getFilename(), file);


    }catch (MessagingException e) {
        throw new MailParseException(e);
    }
    mailSender.send(message);


   }

   }
  /*EmployeeController.class*/

  @Controller
  @RequestMapping("/")
  public class EmployeeController {

private static final Logger logger = Logger
        .getLogger(EmployeeController.class);

public EmployeeController() {
    System.out.println("EmployeeController()");
}

@Autowired
private EmployeeService employeeServiceImpl;
private JavaMailSender mailSender;
private MimeMessage message;

@RequestMapping(value = "/list")
public ModelAndView listEmployee(ModelAndView model) throws IOException {
    List<Employee> listEmployee = employeeServiceImpl.getAllEmployees();
    model.addObject("listEmployee", listEmployee);
    model.setViewName("home");
    return model;
}

**@RequestMapping(value = "/newEmployee", method = RequestMethod.GET)
public ModelAndView newContact(ModelAndView model) {
    Employee employee = new Employee();

    model.addObject("employee", employee);
    model.setViewName("EmployeeForm");
    mailSender.send(message);
    return model;
}

@RequestMapping(value = "/saveEmployee", method = RequestMethod.POST)
public ModelAndView saveEmployee(@ModelAttribute Employee employee) {
     mailSender.send(message);
    if (employee.getId() == 0) { // if employee id is 0 then creating the
        // employee other updating the employee

        employeeServiceImpl.addEmployee(employee);
    } else {
        employeeServiceImpl.updateEmployee(employee);
    }

    return new ModelAndView("redirect:/list");`enter code here`
}**

@RequestMapping(value = "/deleteEmployee", method = RequestMethod.GET)
public ModelAndView deleteEmployee(HttpServletRequest request) {
    int employeeId = Integer.parseInt(request.getParameter("id"));
    employeeServiceImpl.deleteEmployee(employeeId);
    return new ModelAndView("redirect:/list");
}

@RequestMapping(value = "/editEmployee", method = RequestMethod.GET)
public ModelAndView editContact(HttpServletRequest request) {
    int employeeId = Integer.parseInt(request.getParameter("id"));
    Employee employee = employeeServiceImpl.getEmployee(employeeId);
    ModelAndView model = new ModelAndView("EmployeeForm");

    model.addObject("employee", employee);

    return model;
}
@RequestMapping(value = "/start" )
public String showPdf(Map model) {


    return "start";
}

@RequestMapping(value = "/contact" )
public String showcontact(Map model) {


    return "contact";
}
@RequestMapping(value = "/about" )
public String showabout(Map model) {


    return "about";
}
}
[I am getting this error while I am save the employee][1]

Jun 15, 2017 11:25:58 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [springmvc-dispatcher] in context with path [/SpringSecurityPasswordEncodingWithBcryptXMLExample] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException at com.websystique.springsecurity.controller.EmployeeController.saveEmployee(EmployeeController.java:56) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:316) at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126) at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:122) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:168) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:48) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:158) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:205) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:120) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213) at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176) at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1533) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1489) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)

[1]: https://i.stack.imgur.com/d27yt.png

1 个答案:

答案 0 :(得分:0)

对于mailSender,您缺少 ADD INDEX test_table_composite_idx (foo, bar)

应该是

@Autowired