----- Original Message -----
From: Servlets MList <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, October 16, 1999 1:51 AM
Subject: Re: Mail from servlet


> Hi Rashid,
>
> You can send mail from servlet eihter using Java Mail API or using sun
supplied
> SmtpClient  class.

This is the simplest way that I know (and I tried Java Mail; got it working,
but it's rather finicky):

   public void sendMail( String server,
      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 = "154.5.20.132";        file://There was a problem with using:
InetAddress.getLocalHost().toString();

  // Ouverture du socket (connection au mailServer)
  //   et des streams de lecture et d'ecriture

  socket = new Socket(server,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
  String heloTest = "HELO "+host;

  out.println(heloTest);
  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 ;
}

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to