Help please, I try to sending an email from my html page using the java
applet.
but it give error on most of the PC only very few work, what is the error i
make
the java applet show as below
*************** *************** ****
package Celcom.Client;
import java.applet.*;
import java.awt.*;
import javax.mail.*;
import javax.mail.inte rnet.*;
import java.util.*;
/**
* Insert the type's description here.
* Creation date: (11/3/2003 5:19:42 PM)
* @author: Administrator
*/
public class SimpleEMail extends Applet {
//User define veriable
private String strSMTPHost = "";
private String strRecipients = "";
private String strFrom = "";
private String strSubject = "";
private String strMessage = "";
/**
* Cleans up whatever resources are being held. If the applet is active
* it is stopped.
*
* @see #init
* @see #start
* @see #stop
*/
public void destroy() {
super.destroy() ;
// insert code to release resources here
}
/**
* Insert the method's description here.
* Creation date: (11/4/2003 9:44:56 AM)
* @param newMsgLine java.lang.Strin g
*/
protected void displayMsg(Stri ng newMsgLine) {
//String strMsg = gettxtMsg().get Text();
//strMsg += "\n" + newMsgLine;
//gettxtMsg().set Text(strMsg);
}
/**
* Insert the method's description here.
* Creation date: (11/4/2003 9:44:04 AM)
*/
protected void emptyMsg() {
//gettxtMsg().set Text("");
}
/**
* Returns information about this applet.
* @return a string of information about this applet
*/
public String getAppletInfo() {
return "SimpleEMai l\n" +
"\n" +
"Insert the type's description here.\n" +
"Creation date: (11/3/2003 5:18:15 PM)\n" +
"@author: James Hong\n" +
"";
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:51:20 PM)
* @return java.lang.Strin g
*/
public java.lang.Strin g getFrom() {
return strFrom;
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:51:20 PM)
* @return java.lang.Strin g
*/
public java.lang.Strin g getMessage() {
return strMessage;
}
/**
* Returns parameters defined by this applet.
* @return an array of descriptions of the receiver's parameters
*/
public java.lang.Strin g[][] getParameterInf o() {
String[][] info = {
{"smtphost", "String", ""},
{"recipients"," String",""},
{"from", "String", ""},
{"subject","Str ing",""},
{"message", "String", ""}
};
return info;
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:51:20 PM)
* @return java.lang.Strin g
*/
public java.lang.Strin g getRecipients() {
return strRecipients;
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:46:49 PM)
* @return java.lang.Strin g
*/
public java.lang.Strin g getSMTPHost() {
return strSMTPHost;
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:51:20 PM)
* @return java.lang.Strin g
*/
public java.lang.Strin g getSubject() {
return strSubject;
}
/**
* Called whenever the part throws an exception.
* @param exception java.lang.Throw able
*/
private void handleException (java.lang.Thro wable exception) {
/* Uncomment the following lines to print uncaught exceptions to stdout */
// System.out.prin tln("--------- UNCAUGHT EXCEPTION ---------");
// exception.print StackTrace(Syst em.out);
}
/**
* Initializes the applet.
*
* @see #start
* @see #stop
* @see #destroy
*/
public void init() {
try {
super.init();
setName("Simple EMail");
setLayout(new java.awt.Border Layout());
setSize(209, 21);
setVisible(fals e);
// user code begin {1}
// strSMTPHost = this.getParamet er("smtphost") ;
// strRecipients = this.getParamet er("recipients" );
// strFrom = this.getParamet er("from");
// strSubject = this.getParamet er("subject");
// strMessage = this.getParamet er("message");
// user code end
} catch (java.lang.Thro wable ivjExc) {
// user code begin {2}
// user code end
handleException (ivjExc);
}
}
/**
* main entrypoint - starts the part when it is run as an application
* @param args java.lang.Strin g[]
*/
public static void main(java.lang. String[] args) {
try {
Frame frame = new java.awt.Frame( );
SimpleEMail aSimpleEMail;
Class iiCls = Class.forName(" Celcom.Client.S impleEMail");
ClassLoader iiClsLoader = iiCls.getClassL oader();
aSimpleEMail =
(SimpleEMail)ja va.beans.Beans. instantiate(iiC lsLoader,"Celco m.Client.Simple E
Mail");
frame.add("Cent er", aSimpleEMail);
frame.setSize(a SimpleEMail.get Size());
frame.addWindow Listener(new java.awt.event. WindowAdapter() {
public void windowClosing(j ava.awt.event.W indowEvent e) {
System.exit(0);
};
});
frame.show();
java.awt.Insets insets = frame.getInsets ();
frame.setSize(f rame.getWidth() + insets.left + insets.right,
frame.getHeight () + insets.top + insets.bottom);
frame.setVisibl e(true);
} catch (Throwable exception) {
System.err.prin tln("Exception occurred in main() of java.applet.App let");
exception.print StackTrace(Syst em.out);
}
}
/**
* Paints the applet.
* If the applet does not need to be painted (e.g. if it is only a container
for other
* awt components) then this method can be safely removed.
*
* @param g the specified Graphics window
* @see #update
*/
public void paint(Graphics g) {
super.paint(g);
// insert code to paint the applet here
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:52:32 AM)
*/
protected void postMail(String smtphost, String recipients[], String
subject, String message , String from) throws MessagingExcept ion
{
boolean debug = false;
//Set the host smtp address
Properties props = new Properties();
//props.put("mail .smtp.host", "smtp.jcom.net" );
props.put("mail .smtp.host", smtphost);
// create some properties and get the default Session
Session session = Session.getDefa ultInstance(pro ps, null);
session.setDebu g(debug);
// create a message
Message msg = new MimeMessage(ses sion);
// set the from and to address
InternetAddress addressFrom = new InternetAddress (from);
msg.setFrom(add ressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.leng th];
for (int i = 0; i < recipients.leng th; i++)
{
addressTo[i] = new InternetAddress (recipients[i]);
}
msg.setRecipien ts(Message.Reci pientType.TO, addressTo);
// Optional : You can also set your custom headers in the Email if you
Want
// msg.addHeader(" MyHeaderName", "myHeaderValue" );
// Setting the Subject and Content Type
msg.setSubject( subject);
msg.setContent( message, "text/html");
Transport.send( msg);
}
/**
* Comment
*/
public String sendEMail() {
java.util.Strin gTokenizer stkRecipientsLi st = null;
String recipients[] = null;
int nCnt = 0;
emptyMsg();
// verify smtp host
displayMsg("SMT P host : " + getSMTPHost());
if (getSMTPHost(). trim().equalsIg noreCase("")){
return "Blank SMTP host.";
}
// verify From EMail
displayMsg("Fro m Email : " + getFrom());
if (getSMTPHost(). trim().equalsIg noreCase("")){
return "Blank from email address.";
}
// verify Recipients EMail
displayMsg("Rec ipients Email : " + getRecipients() );
stkRecipientsLi st = new java.util.Strin gTokenizer(getR ecipients(), ";");
if (stkRecipientsL ist.countTokens () == 0){
return "No recipients email found.";
}
// verify Subject
displayMsg("Sub ject : " + getSubject());
// verify Message
displayMsg("Mes sage body : \n" + getMessage());
recipients = new String[stkRecipientsLi st.countTokens( )];
while(stkRecipi entsList.hasMor eTokens()) {
recipients[nCnt] = stkRecipientsLi st.nextToken();
nCnt += 1;
}
try {
postMail(getSMT PHost(), recipients, getSubject(), getMessage(),
getFrom());
} catch (MessagingExcep tion exc) {
return exc.getMessage( );
}
return "";
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:51:20 PM)
* @param newFrom java.lang.Strin g
*/
public void setFrom(java.la ng.String newFrom) {
strFrom = newFrom;
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:51:20 PM)
* @param newMessage java.lang.Strin g
*/
public void setMessage(java .lang.String newMessage) {
strMessage = newMessage;
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:51:20 PM)
* @param newRecipients java.lang.Strin g
*/
public void setRecipients(j ava.lang.String newRecipients) {
strRecipients = newRecipients;
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:46:49 PM)
* @param newSMTPHost java.lang.Strin g
*/
public void setSMTPHost(jav a.lang.String newSMTPHost) {
strSMTPHost = newSMTPHost;
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:51:20 PM)
* @param newSubject java.lang.Strin g
*/
public void setSubject(java .lang.String newSubject) {
strSubject = newSubject;
}
/**
* Called to start the applet. You never need to call this method
* directly, it is called when the applet's document is visited.
* @see #init
* @see #stop
* @see #destroy
*/
public void start() {
super.start();
// insert any code to be run when the applet starts here
}
/**
* Called to stop the applet. It is called when the applet's document is
* no longer on the screen. It is guaranteed to be called before destroy()
* is called. You never need to call this method directly.
* @see #init
* @see #start
* @see #destroy
*/
public void stop() {
super.stop();
// insert any code to be run when the applet is stopped here
}
}
*************** *************** **
and the html page as below
*************** *************** **
<HTML>
<HEAD>
<TITLE>SimpleEM ail</TITLE>
<script language="JavaS cript">
function sendingEMail(){
window.SimpleEM ail.setSMTPHost (window.smtphos t.value);
window.SimpleEM ail.setFrom(win dow.fromemail.v alue);
window.SimpleEM ail.setRecipien ts(window.toema il.value);
window.SimpleEM ail.setSubject( window.subject. value);
window.SimpleEM ail.setMessage( window.message. innerText);
window.SimpleEM ail.sendEMail() ;
alert("done");
}
</script>
</HEAD>
<BODY>
<H1>SimpleEMail </H1>
<P>
<APPLET style="LEFT: 0px; WIDTH: 144px; TOP: 0px; HEIGHT: 14px" height=14
archive=CelcomC lient.jar width=144 code=Celcom.Cli ent.SimpleEMail .class
name=SimpleEMai l>
<PARAM NAME="from" VALUE="te**@mai l.com">
<PARAM NAME="smtphost" VALUE="mango">
<PARAM NAME="recipient s" VALUE="te**@mai l.com;">
<PARAM NAME="subject" VALUE="Testing" >
<PARAM NAME="message" VALUE="Hello">
</APPLET>
</P>
<P>
<TABLE>
<tr>
<td>Smtp Host</td><td align=middle> : </td>
<td><INPUT style="WIDTH: 255px; HEIGHT: 22px" size=32 id=smtphost
name=smtphost value=smtp.mail .com></td>
<tr>
<tr>
<td>From Email</td><td align=middle> : </td>
<td><INPUT style="WIDTH: 255px; HEIGHT: 22px" size=32 id=fromemail
name=fromemail value=te**@mail .com></td>
<tr>
<tr>
<td>To Email</td><td align=middle> : </td>
<td><INPUT style="WIDTH: 255px; HEIGHT: 22px" size=32 id=toemail
name=toemail value=te**@mail .com;></td>
<tr>
<tr>
<td>Subject</td><td align=middle> : </td>
<td><INPUT style="WIDTH: 255px; HEIGHT: 22px" size=32 id=subject
name=subject value=Testing></td>
<tr>
<tr>
<td valign=top>Mess age</td><td align=middle valign=top> : </td>
<td><TEXTAREA id=message style="WIDTH: 253px; HEIGHT: 88px" name=message
rows=4 cols=27>Hello</TEXTAREA></td>
<tr>
<tr>
<td align=middle colspan=3><INPU T type=button value="Send Mail"
onclick="javasc ript:sendingEMa il()" id=button1 name=button1><t d></td>
</tr>
</TABLE>
</P>
</BODY>
</HTML>
*************** *************** **
--
Best Regards,
James Hong