Z
Zhao Wang
Hello, everybody: I want to send email using Java applet (using Javamail
API) through Web (I installed J2SE-j2sdk1.4.1_02 and also jaf-1.0.2 and
javamail1.3.2), after I used signed applet, it ran perfectly by
appletviewer(I can received the email), but when running the applet through
the browser(IE6.0), the java consol shows 'java applet notinited'. And I
know the reason is due to .set sentence.
msg.setContent(message.getText(), "text/plain");
msg.setFrom(from);
msg.setRecipient(Message.RecipientType.TO, to);
msg.setSubject(subjectField.getText());
Can anybody tell me how to solve the problem? I was confused with this for
quite many days. I'd appriciate your help. Thank you.
The source code is followed:
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*; import javax.mail.*; import javax.mail.internet.*;
import javax.activation.*;
//<applet code=SMTPApplet.class width=400 height=300></applet>
public class SMTPApplet extends Applet
{
private Button sendButton = new Button("Send Message");
private Label fromLabel = new Label("From: ");
private Label subjectLabel = new Label("Subject: ");
private TextField fromField = new TextField(40);
private TextField subjectField = new TextField(40);
private TextArea message = new TextArea(30, 60);
private String toAddress = "";
public SMTPApplet()
{
this.setLayout(new BorderLayout());
Panel north = new Panel();
north.setLayout(new GridLayout(3,1));
Panel n1 = new Panel();
n1.add(fromLabel);
n1.add(fromField);
north.add(n1);
Panel n2 = new Panel();
n2.add(subjectLabel);
n2.add(subjectField);
north.add(n2);
this.add(north, BorderLayout.NORTH);
message.setFont(new Font("Monospaced", Font.PLAIN, 12));
this.add(message, BorderLayout.CENTER);
Panel south = new Panel();
south.setLayout(new FlowLayout(FlowLayout.CENTER));
south.add(sendButton);
sendButton.addActionListener(new SendAction());
this.add(south, BorderLayout.SOUTH);
}
public void init()
{
String subject = this.getParameter("subject");
if(subject == null) subject = "";
subjectField.setText(subject);
toAddress = this.getParameter("to");
if(toAddress == null) toAddress = "";
String fromAddress = this.getParameter("from");
if(fromAddress == null) fromAddress = "";
fromField.setText(fromAddress);
}
class SendAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
try
{
Properties props = new Properties();
props.put("mail.host", "10.1.1.1");
Session mailConnection = Session.getInstance(props,
null);
final MimeMessage msg = new MimeMessage(mailConnection);
Address to = new InternetAddress(toAddress);
Address from = new InternetAddress(fromField.getText());
msg.setContent(message.getText(), "text/plain");
msg.setFrom(from);
msg.setRecipient(Message.RecipientType.TO, to);
msg.setSubject(subjectField.getText());
Runnable r = new Runnable()
{
public void run()
{
try
{
Transport.send(msg);
}
catch(Exception e)
{
e.printStackTrace();
}
}
};
Thread t = new Thread(r);
t.start();
message.setText("");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
API) through Web (I installed J2SE-j2sdk1.4.1_02 and also jaf-1.0.2 and
javamail1.3.2), after I used signed applet, it ran perfectly by
appletviewer(I can received the email), but when running the applet through
the browser(IE6.0), the java consol shows 'java applet notinited'. And I
know the reason is due to .set sentence.
msg.setContent(message.getText(), "text/plain");
msg.setFrom(from);
msg.setRecipient(Message.RecipientType.TO, to);
msg.setSubject(subjectField.getText());
Can anybody tell me how to solve the problem? I was confused with this for
quite many days. I'd appriciate your help. Thank you.
The source code is followed:
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*; import javax.mail.*; import javax.mail.internet.*;
import javax.activation.*;
//<applet code=SMTPApplet.class width=400 height=300></applet>
public class SMTPApplet extends Applet
{
private Button sendButton = new Button("Send Message");
private Label fromLabel = new Label("From: ");
private Label subjectLabel = new Label("Subject: ");
private TextField fromField = new TextField(40);
private TextField subjectField = new TextField(40);
private TextArea message = new TextArea(30, 60);
private String toAddress = "";
public SMTPApplet()
{
this.setLayout(new BorderLayout());
Panel north = new Panel();
north.setLayout(new GridLayout(3,1));
Panel n1 = new Panel();
n1.add(fromLabel);
n1.add(fromField);
north.add(n1);
Panel n2 = new Panel();
n2.add(subjectLabel);
n2.add(subjectField);
north.add(n2);
this.add(north, BorderLayout.NORTH);
message.setFont(new Font("Monospaced", Font.PLAIN, 12));
this.add(message, BorderLayout.CENTER);
Panel south = new Panel();
south.setLayout(new FlowLayout(FlowLayout.CENTER));
south.add(sendButton);
sendButton.addActionListener(new SendAction());
this.add(south, BorderLayout.SOUTH);
}
public void init()
{
String subject = this.getParameter("subject");
if(subject == null) subject = "";
subjectField.setText(subject);
toAddress = this.getParameter("to");
if(toAddress == null) toAddress = "";
String fromAddress = this.getParameter("from");
if(fromAddress == null) fromAddress = "";
fromField.setText(fromAddress);
}
class SendAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
try
{
Properties props = new Properties();
props.put("mail.host", "10.1.1.1");
Session mailConnection = Session.getInstance(props,
null);
final MimeMessage msg = new MimeMessage(mailConnection);
Address to = new InternetAddress(toAddress);
Address from = new InternetAddress(fromField.getText());
msg.setContent(message.getText(), "text/plain");
msg.setFrom(from);
msg.setRecipient(Message.RecipientType.TO, to);
msg.setSubject(subjectField.getText());
Runnable r = new Runnable()
{
public void run()
{
try
{
Transport.send(msg);
}
catch(Exception e)
{
e.printStackTrace();
}
}
};
Thread t = new Thread(r);
t.start();
message.setText("");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}