🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.lenguajes.java
Article #29649

Re: Envio de correo con java

#29649
From: Javi Tobal
Date: Thu, 20 Nov 1997 00:00
384 lines
12542 bytes
On Wed, 19 Nov 1997 07:28:41 GMT, ovaj@latinmail.com (Juan) wrote:

>Necesito ayuda, tengo que enviar un archivo de texto mediante un mail
>y quisiera hacerlo sin necesidad de tener que abrir el formulario de
>correo electr�nico ni tener que seleccionar el archivo.
>
>Si alguien sabe como hacerlo mediante un Applet o Javascript me hara 
>un gran favor.
>
>Gracias.
>




He aqu� el fuente de un programa en Java que env�a correo electr�nico.
Como ver�s es muy sencillito.

saludos,


/*
*  H. Carvey
*  jMailer v. 0.01
*  Java standalone application to allow user to connect
*  to SMTP port on a server and send mail via that port
*
*  THIS APPLICATION IS PROVIDED WITHOUT ANY IMPLIED OR
*  SPECIFIED WARANTEE.  ANONYMITY, NEITHER IMPLIED NOR
*  SPECIFIED, IS NOT GUARANTEED IN THE USE OF THIS
*  APPLICATION.  THIS APPLICATION SHOULD NOT BE USED FOR
*  MALICIOUS PURPOSES OR PURPOSES OF DECEIT.
*
*  To use this application: read the ReadMe file
*
*  Platform: Symantec Cafe 1.51/Microsoft Windows95
*
*  Date: 6 Oct 96
*/

import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import Expn;
import SendMail;

public class jMailer extends Frame {

// list components necessary for the entire interface
    Button send,clear,expn,close,about;
    TextField helo, mail_from, rcpt_to, subj;
    TextArea db_mess, data;
    SendMail t_send;
    Expn t_expn;
// containers for all components
    Panel panel1, panel2, panel3, buttonpanel;
// layout manager
    GridBagLayout gridbag = new GridBagLayout();
// constructor
    public jMailer(String title) {

        super(title);
// buttons
        send = new Button("Send Mail");
        clear = new Button("Clear");
        close = new Button("Close");
        expn = new Button("EXPN");
        about = new Button("About");
// textfields
        helo = new TextField(25);
        helo.setEditable(true);
        mail_from = new TextField(25);
        mail_from.setEditable(true);
        rcpt_to = new TextField(25);
        rcpt_to.setEditable(true);
        subj = new TextField(25);
        subj.setEditable(true);
// textareas
        db_mess = new TextArea(5,40);
        db_mess.setEditable(false);
        data = new TextArea(10,60);
        data.setEditable(true);

        panel1 = new Panel();
        panel1.setLayout(gridbag);
        constrain(panel1, new Label("Connect To Server: "),0,0,1,1);
        constrain(panel1, helo,0,1,1,1);
        constrain(panel1, new Label("UserName: "),0,2,1,1);
        constrain(panel1, mail_from,0,3,1,1);
        constrain(panel1, new Label("Send Email To: "),0,4,1,1);
        constrain(panel1, rcpt_to, 0,5,1,1);
        constrain(panel1, new Label("Subj: "),0,6,1,1);
        constrain(panel1, subj,0,7,1,1);

        panel2 = new Panel();
        panel2.setLayout(gridbag);
        constrain(panel2, new Label("Email Body: "),0,0,1,1);
        constrain(panel2, data, 0,1,1,3,
GridBagConstraints.HORIZONTAL,
                  GridBagConstraints.NORTH,1.0,0.0,0,0,0,0);

        panel3 = new Panel();
        panel3.setLayout(gridbag);
        constrain(panel3, new Label("Debug Messages: "),0,0,1,1);
        constrain(panel3, db_mess, 0,1,1,3,
GridBagConstraints.HORIZONTAL,
                  GridBagConstraints.NORTH,1.0,0.0,0,0,0,0);

        buttonpanel = new Panel();
        buttonpanel.setLayout(gridbag);
        constrain(buttonpanel,send,0,0,1,1,GridBagConstraints.NONE,
                  GridBagConstraints.CENTER,0.3,0.0,0,0,10,0);
        constrain(buttonpanel,clear,1,0,1,1,GridBagConstraints.NONE,
                  GridBagConstraints.CENTER,0.3,0.0,0,0,10,0);
        constrain(buttonpanel,expn,2,0,1,1,GridBagConstraints.NONE,
                  GridBagConstraints.CENTER,0.3,0.0,0,0,10,0);
        constrain(buttonpanel,close,3,0,1,1,GridBagConstraints.NONE,
                  GridBagConstraints.CENTER,0.3,0.0,0,0,10,0);
        constrain(buttonpanel,about,4,0,1,1,GridBagConstraints.NONE,
                  GridBagConstraints.CENTER,0.3,0.0,0,0,10,0);

// arrange panels in Frame
        this.setLayout(gridbag);
        constrain(this,panel1,0,0,1,1,GridBagConstraints.VERTICAL,
                GridBagConstraints.NORTHWEST, 0.0,1.0,10,10,5,5);
        constrain(this,panel2,1,0,1,1,GridBagConstraints.BOTH,
                GridBagConstraints.CENTER, 1.0,1.0,10,10,5,10);
        constrain(this,panel3,0,1,2,1,GridBagConstraints.HORIZONTAL,
                GridBagConstraints.CENTER,1.0,0.0,5,0,0,0);

constrain(this,buttonpanel,0,2,2,1,GridBagConstraints.HORIZONTAL,
                GridBagConstraints.CENTER,1.0,0.0,5,0,0,0);

    }

// main constrain methods added here....
    public void constrain(Container container, Component component,
                        int grid_x, int grid_y, int grid_width, int
grid_height,
                        int fill, int anchor, double weight_x, double
weight_y,
                        int top, int left, int bottom, int right)
    {
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = grid_x; c.gridy = grid_y;
        c.gridwidth = grid_width; c.gridheight = grid_height;
        c.fill = fill; c.anchor = anchor;
        c.weightx = weight_x; c.weighty = weight_y;
        if (top+bottom+left+right > 0)
            c.insets = new Insets(top,left,bottom,right);


((GridBagLayout)container.getLayout()).setConstraints(component,c);
        container.add(component);
    }

    public void constrain(Container container,Component component,
                        int grid_x, int grid_y, int grid_width, int
grid_height)
    {

constrain(container,component,grid_x,grid_y,grid_width,grid_height,

GridBagConstraints.NONE,GridBagConstraints.NORTHWEST,0.0,0.0,0,0,0,0);
    }

    public void constrain(Container container, Component component,
                        int grid_x, int grid_y, int grid_width, int
grid_height,
                        int top, int left, int bottom, int right)
    {

constrain(container,component,grid_x,grid_y,grid_width,grid_height,

GridBagConstraints.NONE,GridBagConstraints.NORTHWEST,0.0,0.0,top,
            left,bottom,right);
    }

// event handling method
    public boolean handleEvent(Event evt) {
        switch(evt.id) {
            case Event.ACTION_EVENT:

                if (evt.target == send) {
                    t_send = new
SendMail(helo,mail_from,rcpt_to,subj,data,db_mess);
                    t_send.start();
                    return true;
                }

                else if (evt.target == clear) {
                    db_mess.setText(" ");
                    data.setText(" ");
                    helo.setText(" ");
                    mail_from.setText(" ");
                    rcpt_to.setText(" ");
                    subj.setText(" ");
                    return true;
                }

                else if (evt.target == expn) {
                    t_expn = new Expn(rcpt_to,db_mess);
                    t_expn.start();
                    return true;
                }

                else if (evt.target == about) {
                    db_mess.setText("jMailer v0.01\n");
                    db_mess.appendText("by H. Carvey
<carvdawg@mbay.net>\n");
                    return true;
                }

                else if (evt.target == close) {
                    System.exit(0);
                }

                break;

            case Event.WINDOW_DESTROY:
                System.exit(0);
                break;

            case Event.KEY_PRESS:
            case Event.KEY_ACTION:
                return false;
            case Event.KEY_RELEASE:
            case Event.KEY_ACTION_RELEASE:
                return false;

            default:
                break;

        }// switch

        return true;

    }// handleEvent method


// main()
    public static void main(String args[]) {
        Frame f = new jMailer("jMailer v. 0.01");
        f.pack();
        f.show();
    }
}

// sendmail class
class SendMail extends Thread {
    Socket s = null;
    int port = 25;
    DataInputStream din;
    PrintStream pout;
    TextField mailhost, u_name, to_mail, subj;
    TextArea content, msg;
// constructor
    public SendMail(TextField field1, TextField field2, TextField
field3, TextField field4,
                    TextArea area1, TextArea area2)
    {

    mailhost = field1;
    u_name = field2;
    to_mail = field3;
    subj = field4;
    content = area1;
    msg = area2;
    }

    public void run() {

        try {
            s = new Socket(mailhost.getText(),port);

            din = new DataInputStream(s.getInputStream());
            msg.appendText(din.readLine() + "\n");

            pout = new PrintStream(s.getOutputStream());
            pout.println("HELO " + mailhost.getText());
            msg.appendText(din.readLine() + "\n");

            pout.println("MAIL FROM:" + u_name.getText());
            msg.appendText(din.readLine() + "\n");

            pout.println("RCPT TO: <" + to_mail.getText() + ">");
            msg.appendText(din.readLine() + "\n");

            pout.println("DATA");
            pout.println("Received: nowhere");
            pout.println("Date:  31 Dec 1999");
            pout.println("Message-ID: <123456@" + mailhost.getText() +
">");
            pout.println("Return-Path: <yo@mama.com>");
            pout.println("To: <yo@mama.com>");
            pout.println("Subj: " + subj.getText());
            pout.println("Status: X");
            pout.println("X-Mozilla-Status: 0011");
            pout.println("X-Mailer: Netscape Navigator");
            pout.println(content.getText());
            msg.appendText(din.readLine() + "\n");

            pout.println(".");
            msg.appendText(din.readLine() + "\n");

            msg.appendText("Mail sent...\n");
            s.close();
        } // try
        catch(IOException e) {
            msg.setText("Error: " + e + "\n");
        } // catch
    } // run()
}



// class Expn
class Expn extends Thread {
    Socket s = null;
    int port = 25;
    DataInputStream din;
    PrintStream pout;
    TextField user;
    TextArea output;
    StringTokenizer t = null;
    String hostname = null;
    String username = null;
// constructor
    public Expn(TextField in_name,TextArea out_msg){
        user = in_name;
        output = out_msg;
        output.appendText(user.getText() + "\n");
    }

    public void run() {

        try {
            t = new StringTokenizer(user.getText(),"@");
            while (t.hasMoreTokens()) {
                username = t.nextToken();
                hostname = t.nextToken();
            } // while
            s = new Socket(hostname,port);
            din = new DataInputStream(s.getInputStream());
            output.appendText(din.readLine() + "\n");

            pout = new PrintStream(s.getOutputStream());
            pout.println("HELO " + hostname);
            output.appendText(din.readLine() + "\n");

            pout.println("EXPN " + username);
            boolean more = true;
            while(more) {
                String sout = din.readLine();
                if (sout == null) more = false;
                else output.appendText(sout + "\n");
            } // while

            pout.println("VRFY " + username);
            more = true;
            while(more) {
                String sout = din.readLine();
                if (sout == null) more = false;
                else output.appendText(sout + "\n");
            } // while

            s.close();
        } // try
        catch(IOException e) {
            output.setText("Error: " + e + "\n");
        } // catch
    } // run()
}// class

--
/*
 * Javi Tobal
 * Bilbao, Spain.
 * E-mail: tobal@arrakis.es
 *         tobal@homemail.com
 * http://www.arrakis.es/~tobal/
 */
Technical Details
Message-ID: <3472f9ab.4063575@noticias.ibernet.es>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <64u44c$e5$1@talia.mad.ibernet.es>

Show Full Headers
Complete Article Headers
X-Google-Language: PORTUGUESE,ASCII
X-Google-Thread: 1116f3,1d48d50051d9f9cc
X-Google-Attributes: gid1116f3,public
From: tobal@arrakis.es (Javi Tobal)
Subject: Re: Envio de correo con java
Date: 1997/11/20
Message-ID: <3472f9ab.4063575@noticias.ibernet.es>
X-Deja-AN: 291025963
References: <64u44c$e5$1@talia.mad.ibernet.es>
Organization: Unisource Espana NEWS SERVER
Newsgroups: es.comp.lenguajes.java