public void sendMail(String to_address, // Destinataire du message
String from_address, // Emeteur du message
String sSu, // Sujet du message
String sMess) // Message
throws IOException, ProtocolException, UnknownHostException
{
Socket socket; // Le Socket
DataInputStream in; // Le stream de lecture du Socket
PrintStream out; // Le stream d'ecriture du Socket
String host; // Identification du poste
String str; // Pour la lecture de donnees
// Identification du poste
// host peut etre force a www.ibm.com ou autre en cas de probleme
host = InetAddress.getLocalHost().toString() ;
// Ouverture du socket (connection au mailServer)
// et des streams de lecture et d'ecriture
socket = new Socket(getDocumentBase().getHost(), 25);
in = new DataInputStream(socket.getInputStream());
out = new PrintStream(socket.getOutputStream());
// lecture du message initial
str = in.readLine();
if (!str.startsWith("220")) throw new ProtocolException(str);
while (str.indexOf('-') == 3) {
str = in.readLine();
if (!str.startsWith("220")) throw new ProtocolException(str);
}
// fin message initial
// Dialogue avec les Serveur de mail
// Envoie de HELO au serveur SMTP
out.println( "HELO " + host );
out.flush() ;
str = in.readLine();
if (!str.startsWith("250")) throw new ProtocolException(str);
// On est connecte au serveur de Mail...
// Envoie du Mail
out.println( "MAIL FROM: " + from_address );
out.flush() ;
str = in.readLine();
if (!str.startsWith("250")) throw new ProtocolException(str);
// A qui envoie t on cela
out.println( "RCPT TO: " + to_address );
out.flush() ;
str = in.readLine();
if (!str.startsWith("250")) throw new ProtocolException(str);
// Est on pret a envoyer les donnees
out.println( "DATA" );
out.flush() ;
str = in.readLine();
if (!str.startsWith("354")) throw new ProtocolException(str);
// Emmeteur - Destinataire - Sujet
out.println("From: " + from_address);
out.println("To: " + to_address);
out.println( "Subject: " + sSu + "\n" );
out.flush() ;
out.println("Comment: Unauthenticated sender");
out.println("X-Mailer: Simple tSmtp");
out.println("");
out.println( sMess ) ;
out.println(".") ;
out.flush() ;
str = in.readLine();
if (!str.startsWith("250")) throw new ProtocolException(str);
out.println("QUIT");
out.flush();
in.close() ;
socket.close() ;
return ;
}
|