Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 11:16 PM
James Hong
Guest
 
Posts: n/a
Default Help need to send email using java applet in html page

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.internet.*;
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.String
*/
protected void displayMsg(String newMsgLine) {
//String strMsg = gettxtMsg().getText();
//strMsg += "\n" + newMsgLine;
//gettxtMsg().setText(strMsg);
}
/**
* Insert the method's description here.
* Creation date: (11/4/2003 9:44:04 AM)
*/
protected void emptyMsg() {
//gettxtMsg().setText("");
}
/**
* Returns information about this applet.
* @return a string of information about this applet
*/
public String getAppletInfo() {
return "SimpleEMail\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.String
*/
public java.lang.String getFrom() {
return strFrom;
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:51:20 PM)
* @return java.lang.String
*/
public java.lang.String getMessage() {
return strMessage;
}
/**
* Returns parameters defined by this applet.
* @return an array of descriptions of the receiver's parameters
*/
public java.lang.String[][] getParameterInfo() {
String[][] info = {
{"smtphost", "String", ""},
{"recipients","String",""},
{"from", "String", ""},
{"subject","String",""},
{"message", "String", ""}
};
return info;
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:51:20 PM)
* @return java.lang.String
*/
public java.lang.String getRecipients() {
return strRecipients;
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:46:49 PM)
* @return java.lang.String
*/
public java.lang.String getSMTPHost() {
return strSMTPHost;
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:51:20 PM)
* @return java.lang.String
*/
public java.lang.String getSubject() {
return strSubject;
}
/**
* Called whenever the part throws an exception.
* @param exception java.lang.Throwable
*/
private void handleException(java.lang.Throwable exception) {

/* Uncomment the following lines to print uncaught exceptions to stdout */
// System.out.println("--------- UNCAUGHT EXCEPTION ---------");
// exception.printStackTrace(System.out);
}
/**
* Initializes the applet.
*
* @see #start
* @see #stop
* @see #destroy
*/
public void init() {
try {
super.init();
setName("SimpleEMail");
setLayout(new java.awt.BorderLayout());
setSize(209, 21);
setVisible(false);
// user code begin {1}
// strSMTPHost = this.getParameter("smtphost");
// strRecipients = this.getParameter("recipients");
// strFrom = this.getParameter("from");
// strSubject = this.getParameter("subject");
// strMessage = this.getParameter("message");
// user code end
} catch (java.lang.Throwable 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.String[]
*/
public static void main(java.lang.String[] args) {
try {
Frame frame = new java.awt.Frame();
SimpleEMail aSimpleEMail;
Class iiCls = Class.forName("Celcom.Client.SimpleEMail");
ClassLoader iiClsLoader = iiCls.getClassLoader();
aSimpleEMail =
(SimpleEMail)java.beans.Beans.instantiate(iiClsLoa der,"Celcom.Client.SimpleE
Mail");
frame.add("Center", aSimpleEMail);
frame.setSize(aSimpleEMail.getSize());
frame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
};
});
frame.show();
java.awt.Insets insets = frame.getInsets();
frame.setSize(frame.getWidth() + insets.left + insets.right,
frame.getHeight() + insets.top + insets.bottom);
frame.setVisible(true);
} catch (Throwable exception) {
System.err.println("Exception occurred in main() of java.applet.Applet");
exception.printStackTrace(System.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 MessagingException
{
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.getDefaultInstance(props, null);
session.setDebug(debug);

// create a message
Message msg = new MimeMessage(session);

// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);

InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.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.StringTokenizer stkRecipientsList = null;
String recipients[] = null;
int nCnt = 0;

emptyMsg();
// verify smtp host
displayMsg("SMTP host : " + getSMTPHost());
if (getSMTPHost().trim().equalsIgnoreCase("")){
return "Blank SMTP host.";
}

// verify From EMail
displayMsg("From Email : " + getFrom());
if (getSMTPHost().trim().equalsIgnoreCase("")){
return "Blank from email address.";
}

// verify Recipients EMail
displayMsg("Recipients Email : " + getRecipients());
stkRecipientsList = new java.util.StringTokenizer(getRecipients(), ";");
if (stkRecipientsList.countTokens() == 0){
return "No recipients email found.";
}

// verify Subject
displayMsg("Subject : " + getSubject());

// verify Message
displayMsg("Message body : \n" + getMessage());


recipients = new String[stkRecipientsList.countTokens()];
while(stkRecipientsList.hasMoreTokens()) {
recipients[nCnt] = stkRecipientsList.nextToken();
nCnt += 1;
}

try {
postMail(getSMTPHost(), recipients, getSubject(), getMessage(),
getFrom());
} catch (MessagingException exc) {
return exc.getMessage();
}

return "";
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:51:20 PM)
* @param newFrom java.lang.String
*/
public void setFrom(java.lang.String newFrom) {
strFrom = newFrom;
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:51:20 PM)
* @param newMessage java.lang.String
*/
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.String
*/
public void setRecipients(java.lang.String newRecipients) {
strRecipients = newRecipients;
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:46:49 PM)
* @param newSMTPHost java.lang.String
*/
public void setSMTPHost(java.lang.String newSMTPHost) {
strSMTPHost = newSMTPHost;
}
/**
* Insert the method's description here.
* Creation date: (11/3/2003 5:51:20 PM)
* @param newSubject java.lang.String
*/
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>SimpleEMail</TITLE>
<script language="JavaScript">
function sendingEMail(){
window.SimpleEMail.setSMTPHost(window.smtphost.val ue);
window.SimpleEMail.setFrom(window.fromemail.value) ;
window.SimpleEMail.setRecipients(window.toemail.va lue);
window.SimpleEMail.setSubject(window.subject.value );
window.SimpleEMail.setMessage(window.message.inner Text);
window.SimpleEMail.sendEMail();
alert("done");
}
</script>
</HEAD>
<BODY>
<H1>SimpleEMail</H1>
<P>
<APPLET style="LEFT: 0px; WIDTH: 144px; TOP: 0px; HEIGHT: 14px" height=14
archive=CelcomClient.jar width=144 code=Celcom.Client.SimpleEMail.class
name=SimpleEMail>
<PARAM NAME="from" VALUE="test@mail.com">
<PARAM NAME="smtphost" VALUE="mango">
<PARAM NAME="recipients" VALUE="test@mail.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=test@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=test@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>Message</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><INPUT type=button value="Send Mail"
onclick="javascript:sendingEMail()" id=button1 name=button1><td></td>
</tr>
</TABLE>
</P>
</BODY>
</HTML>

********************************
--
Best Regards,
James Hong


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.