Newsgroup: it.comp.java
Titolo: Form mail da un applet standalone


ICQ #29245894
----------------form.java----------------------------------------------
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class Form extends Frame
{
  TextField Nome,Email;
  TextArea Messaggio;
  Label _nome,_email,_messaggio;
  Button submit,cancel;
  Mailer mail = new Mailer("
mail.iol.it
","
enisthe...@iol.it
");
  Applet parent;
  public Form(Applet p)
  {
    parent = p;
    ListenerBottoni Listener = new ListenerBottoni();
    Nome = new TextField();
    Email = new TextField();
    _messaggio = new Label("Messaggio");
    _messaggio.setForeground(Color.white);
    _messaggio.setVisible(true);
    Messaggio = new
TextArea(null,100,100,TextArea.SCROLLBARS_VERTICAL_ONLY);
    _nome = new Label("Nome");
    _nome.setForeground(Color.white);
    _nome.setVisible(true);
    _email = new Label("Indirizzo e-mail");
    _email.setForeground(Color.white);
    _email.setVisible(true);
    submit = new Button("Send");
    _email.setForeground(Color.white);
    cancel = new Button("Cancel");
    cancel.setForeground(Color.white);
    cancel.setBackground(new Color(0000255));
    submit.setBackground(new Color(0000255));
    submit.setForeground(Color.white);
    submit.addActionListener(Listener);
    cancel.addActionListener(Listener);
    _nome.setBounds(20,30,150,20);
    Nome.setBounds(20,55,150,20);
    _email.setBounds(20,80,150,20);
    Email.setBounds(20,105,150,20);
    _messaggio.setBounds(20,130,150,20);
    Messaggio.setBounds(20,155,210,105);
    submit.setBounds(180,55,50,30);
    cancel.setBounds(180,95,50,30);
    setLayout(null);
    setTitle("Send e-mail message");
    setBounds(30,30,250,295);
    setVisible(true);
    setBackground(new Color(0000255));
    setResizable(false);
    add(Nome);
    add(Email);
    add(Messaggio);
    add(_nome);
    add(_messaggio);
    add(_email);
    add(submit);
    add(cancel);
    addWindowListener(new ascoltatore());
  }
  class ascoltatore extends WindowAdapter
    public void windowClosed(WindowEvent e)
      try
      {
        System.exit(0);
      }
      catch(Exception ex){}
    }
    public void windowClosing( WindowEvent e)
      Form.this.dispose();
      parent.stop();
      parent.destroy();
    }
  }
  class ListenerBottoni implements ActionListener
  {
    public synchronized void actionPerformed(ActionEvent e)
    {
      if(e.getActionCommand().equals("Send"))
      {
if(!(Nome.getText().equals("")||Email.getText().equals("")||Messaggio.getTex
t().equals("")))
        {
mail.sendMail(Nome.getText()+"\n"+Email.getText()+"\n"+Messaggio.getText());
        }
        else
        {
          Errore err = new Errore(Form.this,"Bisogna riempire tutti i campi
per spedire la mail");
        }
      }
      if(e.getActionCommand().equals("Cancel"))
      {
        Nome.setText(null);
        Email.setText(null);
        Messaggio.setText(null);
      }
    }
  }
  class Mailer
  {
    String to;
    String cc;
    String data;
    String subject;
    String msg;
    String serv;
    int ch;
    public Mailer(String server, String verso)
    {
      serv = server;
      to = verso;
    }
    public synchronized void sendMail(String message)
    {
      try
      {
        cc = null;
 subject = "posta dalla tua pagina web";
 msg = message;
 Socket S = new Socket(serv, 25);
 DataOutputStream OS = new DataOutputStream(S.getOutputStream());
 BufferedReader IS = new BufferedReader(new
InputStreamReader(S.getInputStream()));
 lookFor(IS, "220");
 OS.writeBytes("HELO 
pippo.com
\n");
 lookFor(IS, "250");
 OS.writeBytes("MAIL FROM : " + "web_page" + "\n");
 lookFor(IS, "250");
 OS.writeBytes("RCPT TO : " + to +"\n");
 lookFor(IS,"250");
 OS.writeBytes ("DATA\n");
 lookFor(IS, "354");
 OS.writeBytes("Subject: " + subject + "\n");
 OS.writeBytes("From: " + "web_page" + "\n");
 OS.writeBytes("To: " + to + "\n");
 OS.writeBytes(msg + "\n");
 OS.writeBytes(".\n");
 lookFor(IS, "250");
 OS.writeBytes("QUIT\n");
 IS.close();
 S.close();
      }
      catch (Exception e) {
          Messaggio.setText(e.toString());
          Errore err = new Errore(Form.this,e);}
    }
    public synchronized void lookFor(BufferedReader IS, String match) throws
Exception
    {
      String str;
      try
      {
 str = IS.readLine();
      }
      catch (IOException e) {throw(e); }
      if (str.startsWith(match)){}
      else throw(new Exception("Risposta del server errata."));
    }
  }
  public static void main(String argv[])
  {
    Form f = new Form(null);
  }
}
-----------------------------------------------------------------------
---------------------load.java-----------------------------------------
import java.applet.*;
public class load extends Applet
{
  public load()
  {
  }
  public void init()
  {
  }
  public void start()
  {
    Form f = new Form(this);
  }
}
-----------------------------------------------------------------------
------------------errore.java------------------------------------------
import java.awt.*;
import java.awt.event.*;
class Errore extends Dialog implements ActionListener
{
  Label err;
  Button ok;
  public Errore(Frame parent,Exception e)
  {
    super(parent);
    setLayout(new FlowLayout());
    setBounds(350,200,350,100);
    err = new Label("Si è verificato un errore di natura sconosciuta nella
connessione");
    err.setBounds(30,30,300,50);
    err.setForeground(Color.white);
    add(err);
    ok = new Button("    OK    ");
    ok.setBounds(125,85,50,20);
    ok.addActionListener(this);
    add(ok);
    WindowAdapter asc = new WindowAdapter()
    {
      public void windowClosed (WindowEvent evt)
      {
        Errore.this.setVisible(false);
      }
      public void windowClosing (WindowEvent evt)
      {
        Errore.this.setVisible(false);
      }
    };
    addWindowListener(asc);
    setVisible(true);
  }
  public Errore(Frame parent,String s)
  {
    super(parent);
    setLayout(new FlowLayout());
    setBounds(350,200,350,100);
    err = new Label(s);
    err.setBounds(30,30,300,50);
    err.setForeground(Color.white);
    add(err);
    ok = new Button("    OK    ");
    ok.setBounds(125,85,50,20);
    ok.addActionListener(this);
    add(ok);
    WindowAdapter asc = new WindowAdapter()
    {
      public void windowClosed (WindowEvent evt)
      {
        Errore.this.setVisible(false);
      }
      public void windowClosing (WindowEvent evt)
      {
        Errore.this.setVisible(false);
      }
    };
    addWindowListener(asc);
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e)
  {
    if(e.getActionCommand().equals("    OK    "))
    {
      setVisible(false);
    }
  }
}
-----------------------------------------------------------------------

Troverei utile ricevere copia dei messaggi d'errore.
Mi viene comunque da pensare che se provi l'applet
da netscape, vieni fermata dalla sandbox che ti impedisce
di connetterti a computer diversi da quello di provenienza
dell'applet.
Ciao.