Эта базовая настройка работала нормально:
Импорт mail.jar и активации.jar в папку WEB_INF / lib внутри проекта.
get mail.jar от JavaMail (последняя версия с официального сайта) .
получить активации.jar с http://www.oracle.com/technetwork/java/javase/jaf-136260.html
1. Первый jsp: emailForm.jsp
Это форма, используемая для передачи информации об отправителе, деталях получателя, теме и сообщении в emailUtility
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Send email</title>
</head>
<body>
<form action="emailUtility.jsp" method="post">
<table border="0" width="35%" align="center">
<caption><h2>Send email using SMTP</h2></caption>
<tr>
<td width="50%">Sender address </td>
<td><input type="text" name="from" size="50"/></td>
</tr>
<tr>
<td width="50%">Recipient address </td>
<td><input type="text" name="to" size="50"/></td>
</tr>
<tr>
<td>Subject </td>
<td><input type="text" name="subject" size="50"/></td>
</tr>
<tr>
<td>Message Text </td>
<td><input type="text" name="messageText"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Send"/></td>
</tr>
</table>
</form>
</body>
</html>
2. Второй jsp: emailUtility.jsp
Это действие формы, упомянутое в предыдущем jsp (emailForm.jsp).
<html>
<head>
<title>email utility</title>
</head>
<body>
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%
String host = "smtp.gmail.com";
String to = request.getParameter("to");
String from = request.getParameter("from");
String subject = request.getParameter("subject");
String messageText = request.getParameter("messageText");
boolean sessionDebug = false;
// Create some properties and get the default Session.
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.debug", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
Session mailSession = Session.getDefaultInstance(props,
new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"youremail@gmail.com", "password_here");// Specify the Username and the PassWord
}
});
// Set debug on the Session
// Passing false will not echo debug info, and passing True will.
mailSession.setDebug(sessionDebug);
// Instantiate a new MimeMessage and fill it with the
// required information.
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
// Hand the message to the default transport service
// for delivery.
Transport.send(msg);
out.println("Mail was sent to " + to);
out.println(" from " + from);
out.println(" using host " + host + ".");
%>
</table>
</body>
</html>
3. Перейти по следующему адресу
http://localhost:8080/projectname/emailForm.jsp
4. Перезапустите сервер, если он выдаст ошибку сервера.