Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用spring创建自动响应gmail?_Java_Spring_Hibernate_Jsp - Fatal编程技术网

Java 如何使用spring创建自动响应gmail?

Java 如何使用spring创建自动响应gmail?,java,spring,hibernate,jsp,Java,Spring,Hibernate,Jsp,当我使用crud操作保存员工时,它工作正常,但一旦我调用此方法:mailssender.send(message)我得到500个错误 当我使用crud操作保存员工时,我应该是,一旦保存,自动回复邮件将发送给对应的员工 /*Employee.class*/ @Entity @Table(name = "EMP_TBL") public class Employee implements Serializable { private static final long serialVersion

当我使用crud操作保存员工时,它工作正常,但一旦我调用此方法:mailssender.send(message)我得到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]

您缺少邮件发件人的自动连线

应该是

@Autowired
private JavaMailSender mailSender;

您可以发布完整的错误日志吗?HTTP状态500-请求处理失败;嵌套异常为java.lang.NullPointerException类型异常报告消息请求处理失败;嵌套异常为java.lang.NullPointerException说明服务器遇到内部错误,无法满足此请求。异常org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是java.lang.NullPointerException这不是完整日志,但它是日志的一部分,没有足够的信息提供帮助。最好;-)现在,我添加了完整的错误日志。当我保存新员工时,我需要向员工表中的对应员工发送自动邮件。您可以检查您在问题中发布的代码吗?我的回答就是基于这个。
@Autowired
private JavaMailSender mailSender;