Je me dis que pour vous éviter de trop chercher le code sur le tuto il est peut être mieux que je le poste ici. Il y a 3 fichiers, 2 JSP pour l'interface et une Classe Mail.java.
Classe Mail.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jMail;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
/**
*
* @author Agraj
*/
public class Mail {
private String to;
private String from;
private String message;
private String subject;
private String smtpServ;
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getSmtpServ() {
return smtpServ;
}
public void setSmtpServ(String smtpServ) {
this.smtpServ = smtpServ;
}
public int sendMail(){
try
{
Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host",smtpServ);
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
// -- We could include CC recipients too --
// if (cc != null)
// msg.setRecipients(Message.RecipientType.CC
// ,InternetAddress.parse(cc, false));
// -- Set the subject and body text --
msg.setSubject(subject);
msg.setText(message);
// -- Set some other header information --
msg.setHeader("MyMail", "Java Mail Test");
msg.setSentDate(new Date());
// -- Send the message --
Transport.send(msg);
System.out.println("Message sent to"+to+" OK.");
return 0;
}
catch (Exception ex)
{
ex.printStackTrace();
System.out.println("Exception "+ex);
return -1;
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
@Override
public PasswordAuthentication getPasswordAuthentication() {
String username = "prenom.nom@gmail.com";
String password = "soadnemesis";
return new PasswordAuthentication(username, password);
}
}
}
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title> Java Mail </title>
</head>
<form action="sendMail.jsp" method="POST">
Send Mail ,
----
To, :,
,
----
Subject, :,
,
----
Message, :,
<textarea name="message" rows="8" cols="30">
</textarea>,
----
</form>
</html>
sendMail.jsp
<%--
Document : sendMail
Created on : 29 Apr, 2008, 5:53:41 PM
Author : Agraj
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="mail" scope="session" class="jMail.Mail" />
<jsp:setProperty name="mail" property="to" param="to" />
<jsp:setProperty name="mail" property="from" value="Java.Mail.CA@gmail.com" />
<jsp:setProperty name="mail" property="smtpServ" value="smtp.gmail.com" />
<jsp:setProperty name="mail" property="subject" param="subject" />
<jsp:setProperty name="mail" property="message" param="message" />
<!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=UTF-8">
<title> Send Mail </title>
</head>
<%
String to = mail.getTo();
int result;
result = mail.sendMail();
if(result == 0){
out.println(" Mail Successfully Sent to "+to);
}
else{
out.println(" Mail NOT Sent to "+to);
}
%>
</html>