Enviar mail con java mediante applet:
Código HTML para el programa CheckMail.java
<html><body>
<center>
<applet code="CheckMail.class" width=600 height=450>
</applet>
</center>
</body></html>
Código HTML para el programa SendMail.java
<html><body bgcolor="#ffffff">
<center>
<applet code="SendMail.class" width=600 height=450>
<param name="RECIPIENT" value="your_email@your_org.com">
<!-- Alternative text, for non-Java browsers: -->
<h2><a href="mailto:your_email@your_org.com">Click here to send e-mail</a></h2>
</applet>
</center>
</body></html>
Código fuente para el applet SendMail.java
/*
* SendMail.java
*
* Presents a form to the user, and sends the information entered, in addition
* to "hidden" information in the form, to a predefined recipient via e-mail.
* Can be run either as a browser remote applet, or standalone.
*
* HTML parameters:
*
* RECIPIENT the e-mail address of the recipient (if not run from a browser,
* the recipient must be hardcoded in the main routine).
*
*/
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.applet.*;
import java.io.*;
import java.util.Date;
/* The applet. */
public class SendMail extends Applet
{
// The e-mail address all messages are to be sent to; specified in HTML
String webmasterEmail = null;
String serverHostName = null;
boolean standalone = false;
int smtpPort = 25;
Socket socket;
PrintStream ps;
DataInputStream dis;
InetAddress rina;
InetAddress lina;
Form form;
/* Initialize the applet. */
public void init()
{
setBackground(Color.white);
form = new Form(this);
add(form);
resize(600, 450);
if (serverHostName == null) serverHostName = getCodeBase().getHost();
if (webmasterEmail == null) webmasterEmail = getParameter("RECIPIENT");
}
/** Show status to the user. */
public void showStatus(String s)
{
System.out.println(s);
if (standalone) return;
super.showStatus(s);
}
/* Send an e-mail message. */
public void send()
throws IOException, Exception
{
// Open connection to SMTP server
socket = new Socket(serverHostName, smtpPort);
// Send the form contents as a message
try
{
rina = socket.getInetAddress();
lina = rina.getLocalHost();
ps = new PrintStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
// Send message
sendline("HELO " + lina.toString());
sendline("MAIL FROM:" + form.email());
sendline("RCPT TO:" + webmasterEmail);
sendline("DATA");
sendline(form.message());
sendline(".");
}
catch (Exception ex)
{
socket.close();
throw ex;
}
// Close connection
socket.close();
}
// Send a line of data to the server,
// and retrieve the handshake
void sendline(String data)
throws IOException
{
System.out.println("sendline out:" + data);
ps.println(data);
ps.flush();
String s = dis.readLine();
System.out.println("sendline in:" + s);
}
// Main routine,
// for standalone program execution
public static void main(String args[])
{
SendMail ap = new SendMail();
ap.serverHostName = "www.your_organization.com";
ap.webmasterEmail = "recipient@your_organization.com";
ap.standalone = true;
ClosableFrame fr = new ClosableFrame("SendMail");
ap.init();
fr.add("Center", ap);
fr.resize(600, 450);
fr.show();
ap.start();
}
}
// A form for obtaining user input.
//Customize this for your application, just
// as you would customize an
//HTML form for a Web-based e-mail application.
class Form extends Panel
{
SendMail applet;
// The form's elements...
Label nameLabel;
TextField nameField;
Label emailLabel;
TextField emailField;
Label orgLabel;
TextField orgField;
Label msgBodyLabel;
TextArea msgBodyArea;
Button sendButton;
// The constructor
public Form(SendMail ap)
{
applet = ap;
setBackground(Color.white);
setLayout(new GridLayout(2, 1));
// Create a panel to put the text fields and button on
Panel p = new Panel();
p.setLayout(new GridLayout(8, 1));
// Instantiate all the elements, and add them to their containers...
p.add(sendButton = new Button("Send"));
p.add(nameLabel = new Label("Your Name:"));
p.add(nameField = new TextField(60));
p.add(emailLabel = new Label("Your E-mail address:"));
p.add(emailField = new TextField(60));
p.add(orgLabel = new Label("Your Organization:"));
p.add(orgField = new TextField(60));
p.add(msgBodyLabel = new Label("Your Message:"));
add(p);
add(msgBodyArea = new TextArea(3, 60));
// Set the size of the form
resize(550, 400);
}
/* Return the value in the e-mail address field in the form */
public String email()
{ return emailField.getText(); }
// Return the contents of the body of the form,
//including any "hidden" fields.
public String message()
{
String m = "";
m += nameLabel.getText();
m += nameField.getText();
m += "\n";
m += orgLabel.getText();
m += orgField.getText();
m += "\n";
m += "Web Origin:";
if (!applet.standalone) m += applet.getDocumentBase();
m += "\n";
m += "Date Sent:";
m += (new Date()).toString();
m += "\n";
m += msgBodyLabel.getText();
m += msgBodyArea.getText();
m += "\n";
return m;
}
// Respond to the button click event:
// send the message.
public boolean handleEvent(Event e)
{
if ((e.target == sendButton) && (e.id == Event.ACTION_EVENT))
{
// User clicked the Send button; send the message
try
{ applet.send();}
catch (Exception ex)
{
applet.showStatus("Error; message send failed:\n " + ex.toString());
return true;
}
applet.showStatus("Message sent");
return true;
}
// Not an event to handle; let the super class try
return super.handleEvent(e);
}
}
// Create a frame that can be closed;
//only used if run standalone.
class ClosableFrame extends Frame
{
public ClosableFrame(String t)
{ super(t); }
public boolean handleEvent(Event e)
{
if (e.id == Event.WINDOW_DESTROY) System.exit(0);
return super.handleEvent(e);
}
}
Código para el applet CheckMail.java
/*
* CheckMail.java
*
* Presents a form to the user for entry of e-mail id and password, and check's
* if the user has e-mail.
*
* Can be run either as a browser remote applet, or standalone.
*
*/
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.applet.*;
import java.io.*;
import java.util.Date;
/**
* The applet.
*/
public class CheckMail extends Applet
{
String serverHostName = null;
boolean standalone = false;
int popPort = 110;
Socket socket;
PrintStream ps;
DataInputStream dis;
InetAddress rina;
InetAddress lina;
CheckMailForm form;
/**
* Initialize the applet.
*/
public void init()
{
form = new CheckMailForm(this);
add(form);
form.resize(300, 300);
setBackground(Color.blue);
if (serverHostName == null) serverHostName = getCodeBase().getHost();
}
/**
* Show status text to the user.
*/
public void showStatus(String s)
{
System.out.println(s);
if (standalone) return;
super.showStatus(s);
}
/**
* Perform check for e-mail.
*/
public void checkForMail()
throws IOException, NumberFormatException, Exception
{
showStatus("Checking for mail on " + serverHostName);
// Open connection
socket = new Socket(serverHostName, popPort);
String rs;
try
{
rina = socket.getInetAddress();
lina = rina.getLocalHost();
ps = new PrintStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
// Check for messages
sendline("USER " + form.getId());
rs = sendline("PASS " + form.getPswd());
if (rs.charAt(0) != '+') throw new Exception("Incorrect password");
rs = sendline("STAT");
}
catch (Exception ex)
{
socket.close();
throw ex;
}
// Close connection
socket.close();
// Parse result
int r = 0;
r = Integer.parseInt(rs.substring(4, rs.indexOf(" messages")));
// Update result field
form.resultField.setText(Integer.toString(r));
}
/**
* Send a line of data to the server, and return the handshake.
*/
String sendline(String data)
throws IOException
{
System.out.println("sendline out:" + data);
ps.println(data);
ps.flush();
String s = dis.readLine();
System.out.println("sendline in:" + s);
return s;
}
/**
* Main routine, for running as a standalone application.
*/
public static void main(String args[])
{
CheckMail ap = new CheckMail();
ap.serverHostName = "ids2.idsonline.com";
ap.standalone = true;
ClosableFrame fr = new ClosableFrame("CheckMail");
ap.init();
fr.add("Center", ap);
fr.resize(300, 300);
fr.show();
ap.start();
}
}
/**
* Form for obtaining e-mail user id and password from the user.
*/
class CheckMailForm extends Panel
{
CheckMail applet;
// The form's elements...
Label idLabel;
TextField idField;
Label pswdLabel;
TextField pswdField;
Button button;
Label resultLabel;
TextField resultField;
/**
* The constructor.
*/
public CheckMailForm(CheckMail ap)
{
applet = ap;
setBackground(Color.blue);
setLayout(new GridLayout(7, 1));
// Instantiate all the elements, and add them to the form...
add(button = new Button("Check For Mail"));
add(idLabel = new Label("Id:"));
add(idField = new TextField(20));
add(pswdLabel = new Label("Password:"));
add(pswdField = new TextField(20));
pswdField.setEchoCharacter('*');
add(resultLabel = new Label("Messages Waiting:"));
add(resultField = new TextField(6));
resultField.setEditable(false);
// Set the size of the form
resize(250, 250);
}
/**
* Return the value of the ID field in the form.
*/
public String getId()
{
return idField.getText();
}
/**
* Return the value of the password field in the form.
*/
public String getPswd()
{
return pswdField.getText();
}
/**
* Respond to the button click event: check for messages.
*/
public boolean handleEvent(Event e)
{
if ((e.target == button) && (e.id == Event.ACTION_EVENT))
{
try { applet.checkForMail(); }
catch (Exception ex)
{
applet.showStatus("Error; unable to check for messages:\n "
+ ex.toString());
return true;
}
applet.showStatus("Completed.");
return true;
}
return super.handleEvent(e);
}
}
Para ver este enlace Registrate o Inicia SesionFuente