tMail - Envoie d'un Mail
Main page
Home
[ Recherche | Home Page | Index Java tips ]


PrevRetour IndexNext
  • But...
    Ce petit source permet l'envoie d'un mail à partir d'une applet ou d'une application. Il reste tres limité mais c'est une base de depart.
    Le principe est tres simple, il suffit d'ouvrir un Socket sur le serveur hebergeant l'applet en utilisant le port 25 (port mail de defaut). Cela implique qu'un serveur mail tourne bien sur ce port.

  • Applet tMail : Exemple d'utilisation de ce source...

  • tMail.java : Source de l'applet...

  • Source d'une methode d'envoie de mail...
       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 ;
       }
    
    

  • Utilisation de cette methode...
         try {
    	sendMail("rbl@mygale.org",
                     "test@rbl.com",
                     "test tMail",
                     "Test de l'applet tMail, pour ......"
                    ) ;
         }
         catch(UnknownHostException uhe)
         {    /* Traitement de UnknownHostException */ }
         catch(ProtocolException pe)
         {    /* Traitement du ProtocolException */    }
         catch(IOException ioe)
         {    /* Traitement de IOException */          }
    

  • Telechargement d'un applet exemple...



Copyright © 1996..2003, BERTHOU. Tous droits réservés.
Dernière modification le 03 Mars 2003 18H20

C.N.I.L.
n° 707410