Article View: es.comp.lenguajes.java
Article #29409Re: Como enviar e-mail con un applet
From: Javi Tobal
Date: Fri, 12 Dec 1997 00:00
Date: Fri, 12 Dec 1997 00:00
391 lines
13131 bytes
13131 bytes
On 9 Dec 1997 10:25:23 GMT, i1081656@petra.euitio.uniovi.es (Pedro
Cortizo Casta~on) wrote:
> Esa es la pregunta. Alguien podria ayudarme ???
>
> Me gustaria construir un applet que, introduciendo la direccion y un texto
>envie un e-amil a la direccion especificada. Pero no tengo NPI de como
>hacerlo.
>
> Alguien que ya haya hecho algo parecido podria explicarme los pasos a
>seguir ???
>
> Gracias por adelantado y Salu2:
>
>--
>------------------------------------------------------------------------------
> ## ### ## ##
> ##### ## ##
> ### ## ## ## ## ### ##### ## ## ###
> ##### ## ## ## ## ## ## ## ## ## ## ##
> ## ### #### ## ## ###### ##### ## ### #
>-------------------------------------------------------------------------------
> Pedro Cortizo Casta~on - i1081656@petra.euitio.uniovi.es
>-------------------------------------------------------------------------------
>
voila !!!
/*
* 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:
<34907fd1.2755688@noticias.ibernet.es>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<66j6aj$dek$1@sci.cpd.uniovi.es>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1116f3,eaa1025238dc755a X-Google-Attributes: gid1116f3,public From: tobal@arrakis.es (Javi Tobal) Subject: Re: Como enviar e-mail con un applet Date: 1997/12/12 Message-ID: <34907fd1.2755688@noticias.ibernet.es> X-Deja-AN: 297426808 References: <66j6aj$dek$1@sci.cpd.uniovi.es> Organization: Unisource Espana NEWS SERVER Newsgroups: es.comp.lenguajes.java