01.
<%@ page
import
=
"java.util.Properties"
%>
02.
<%@ page
import
=
"javax.mail.Message"
%>
03.
<%@ page
import
=
"javax.mail.MessagingException"
%>
04.
<%@ page
import
=
"javax.mail.PasswordAuthentication"
%>
05.
<%@ page
import
=
"javax.mail.Session"
%>
06.
<%@ page
import
=
"javax.mail.Transport"
%>
07.
<%@ page
import
=
"javax.mail.internet.InternetAddress"
%>
08.
<%@ page
import
=
"javax.mail.internet.MimeMessage"
%>
09.
10.
<html>
11.
<head>
12.
<title>ThaiCreate.Com JSP Tutorial</title>
13.
</head>
14.
<body>
15.
<%
16.
17.
final
String auth_host =
"mail.thaicreate.com"
;
18.
final
String auth_port =
"25"
;
19.
final
String auth_email =
"no-reply@thaicreate.com"
;
20.
final
String auth_password =
"password"
;
21.
22.
Properties props =
new
Properties();
23.
props.put(
"mail.smtp.host"
, auth_host);
24.
props.put(
"mail.smtp.socketFactory.port"
, auth_port);
25.
props.put(
"mail.smtp.socketFactory.class"
,
26.
"javax.net.ssl.SSLSocketFactory"
);
27.
props.put(
"mail.smtp.auth"
,
"true"
);
28.
props.put(
"mail.smtp.port"
, auth_port);
29.
30.
31.
try
{
32.
33.
Session mailSession = Session.getInstance(props,
34.
new
javax.mail.Authenticator() {
35.
protected
PasswordAuthentication
36.
getPasswordAuthentication() {
37.
return
new
PasswordAuthentication
38.
(auth_email,auth_password);
39.
}
40.
});
41.
42.
Message message =
new
MimeMessage(mailSession);
43.
44.
message.setFrom(
new
InternetAddress(auth_email));
45.
46.
47.
message.setRecipients(Message.RecipientType.TO,
48.
InternetAddress.parse(
"thicreate@hotmail.com"
));
49.
message.setSubject(
"Test sending mail from JSP"
);
50.
message.setText(
"Hello mr.win, Please do not reply this mail"
);
51.
52.
Transport.send(message);
53.
54.
out.println(
"Mail Send Successfully."
);
55.
56.
}
catch
(MessagingException e) {
57.
throw
new
RuntimeException(e);
58.
}
59.
%>
60.
</body>
61.
</html>