-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathMail.java
122 lines (94 loc) · 3.29 KB
/
Mail.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
116
117
118
119
120
121
122
//package your.package;
import java.io.IOException;
import javax.annotation.Resource;
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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Rewrite of mail.php from Contactable, popup contact form jQuery plugin.
*
* @see https://github.com/philipbeel/contactable/
* @see https://github.com/philipbeel/contactable/blob/master/mail.php
*
* @author Libor Jelinek, http://devblog.virtage.com/libor-jelinek/
*/
@WebServlet("/mail")
public class Mail extends HttpServlet {
private static final long serialVersionUID = 1L;
// === Obtain JNDI javax.mail.Session resource ===
// a) Servlet 3.0+
@Resource(name="mail/Session")
private Session session;
// b) Servlet < 3.0
// private Session session;
// @Override
// public void init() throws ServletException {
// try {
// Context initContext = new InitialContext();
// Context envContext = (Context) initContext.lookup("java:/comp/env");
// this.session = (Session) envContext.lookup("mail/Session");
// } catch (Exception ex) {
// ex.printStackTrace();
// }
// }
// For Tomcat you may supply javax.mail.Session as JNDI in context.xml.
/*
<Context path="/" reloadable="true">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- javax.mail.Session for Gmail / Google Apps -->
<Resource name="mail/Session"
auth="Container"
type="javax.mail.Session"
description=""
mail.debug="false"
mail.smtp.host="smtp.gmail.com"
mail.smtp.auth="true"
mail.smtp.starttls.enable="true"
mail.smtp.port="587"
mail.smtp.user="<username>"
password="<password>"
/>
</Context>
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String name = req.getParameter("name");
String email = req.getParameter("email");
String issue = req.getParameter("issue");
String message = req.getParameter("message");
String subject = req.getParameter("subject");
// Set manually "to"
String to = "[email protected]";
// or retrieve from <context-param>
//String to = req.getServletContext().getInitParameter("SOME-PARAM-NAME");
String body = "<div>" +
"<p><strong>Name:</strong> " + name + " <br />" +
"<strong>E-mail:</strong> " + email + " <br />" +
"<strong>Issue:</strong> " + issue + " </p>" +
"<p><strong>Message:</strong> " + message + " </p>" +
"<p><strong>Sending IP:</strong> " + req.getRemoteAddr() + " <br />" +
"<strong>Sent via:</strong> " + req.getServerName() + " </p>" +
"</div>";
resp.setContentType("application/json; charset=utf-8");
Message msg = new MimeMessage(session);
try {
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setContent(body, "text/html; charset=UTF-8");
Transport.send(msg);
} catch (MessagingException e) {
resp.getWriter().print("{\"response\":\"failure\"}");
}
resp.getWriter().print("{\"response\":\"success\"}");
}
}