tMail - Send a Mail
Main page
Home
[ Search | Home Page | Index Java tips ]


PrevRetour IndexNext
  • But...
    With this source you can send a mail in a applet (or an application). It's a very simple source, only to learn...
    To send a mail you must only open a socket on your serveur ( port 25 ), and make a simple dialog with this socket.
    A mail serveur must be run on this port !.

  • Applet tMail : Sample mail applet...

  • tMail.java : Source code... (sory but some comment are in French)

  • Sample Source to send a 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 ;
       }
    
    

  • How to use this 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 */          }
    

  • Download tMail applet...     (backup)





Copyright © 1996..2003, BERTHOU. All right reserved.
Last change : 05 March 2003 18H20