-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnrollController.java
115 lines (93 loc) · 4.05 KB
/
EnrollController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.leapfrog.academy.controller;
import com.leapfrog.academy.entity.Course;
import com.leapfrog.academy.entity.Enquiry;
import com.leapfrog.academy.service.CourseService;
import com.leapfrog.academy.service.EnquiryService;
import com.leapfrog.academy.service.impl.CourseServiceImpl;
import com.leapfrog.academy.service.impl.EnquiryServiceImpl;
import com.leapfrog.academy.system.Controller;
import java.io.IOException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author admin
*/
@WebServlet(name = "enroll",urlPatterns = {"/enroll/*"})
public class EnrollController extends Controller {
private EnquiryService enquiryService =new EnquiryServiceImpl();
private CourseService courseService =new CourseServiceImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
request.setAttribute("courses",courseService.getAll(false));
}catch(Exception e){
System.out.println(e.getMessage());
}
request.getRequestDispatcher("/WEB-INF/Views/front/enroll/index.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
Enquiry enquiry=new Enquiry();
enquiry.setFirstName(request.getParameter("first_name"));
enquiry.setLastName(request.getParameter("last_name"));
enquiry.setEmail(request.getParameter("email"));
enquiry.setContactNo(request.getParameter("contact_no"));
Course course=new Course();
course.setId(Integer.parseInt(request.getParameter("course")));
enquiry.setCourse(course);
enquiry.setMessage(request.getParameter("message"));
enquiryService.insert(enquiry);
sendEmail(enquiry);
response.getWriter().println("<h1>Thank you sir</h1>");
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public void sendEmail(Enquiry e){
// Recipient's email ID needs to be mentioned.
String to = "lifeof069@gmail.com";
// Sender's email ID needs to be mentioned
String from = e.getEmail();
// Assuming you are sending email from localhost
String host = "smtp.wlink.com.np";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject("Enrollment for");
// Now set the actual message
message.setText(e.getMessage());
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}