Cette configuration de base a bien fonctionné:
importation mail.jar et activation.jar dans dossier WEB_INF/lib à l'intérieur du projet.
obtenir mail.jar de JavaMail (dernière version du site officiel).
obtenir activation.jar de http://www.oracle.com/technetwork/java/javase/jaf-136260.html
1. Tout d'abord de: emailForm.jsp
Ceci est un formulaire utilisé pour transmettre l'expéditeur, le destinataire Détails, le contenu et le message Sujet au 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. Deuxième jsp: emailUtility.jsp
Il s'agit de l'action de formulaire mentionnée dans le fichier jsp précédent (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(
"[email protected]", "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. Aller à l'URL suivante
http://localhost:8080/projectname/emailForm.jsp
4. Redémarrez le serveur si elle donne u erreur de serveur.
S'il vous plaît google pour des questions si triviales avant de poster sur SO. Les liens ci-dessous font partie des dix premiers résultats. –
Google est lié aux résultats populaires. Les gens utilisent StackOverflow pour obtenir l'opinion des gens par opposition aux algorithmes de Mountain View.C'est tout le point de StackOverflow IMHO. – MonoThreaded
Obtenir méta, c'est le point du système de points. bon q/a obtient plus de points, apparaît plus dans les recherches, etc ... mais, oui, de meilleurs résultats (j'espère). idée similaire à la page rank, tho. – Thufir