954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java Applet with JavaMail - issue on web

I am working on a java applet that sends information from the applet to a gmail using JavaMail. I downloaded the JavaMail package and using the mail.jar file it does send to the email from my Eclipse workspace.

However, when I ftp the files (ContactForm.java, ContactForm.class, mail.jar, and test.html) to the server. I get a "applet notloaded" error. NOTE: All the files are in the same directory.

Here is the code:

Applet: (ContactForm.java)

package rha;

/*
 * Purpose: Residence Hall Association online "comment card"
 * 			Gets information from the user (email, query, message)
 * 			and SMTPs the information to the RHA email address
 * @author Alex Laughnan
 * Created: August 20, 2010
 * Modified: August 20, 2010
 */

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.util.*;
import java.awt.event.*;
import javax.mail.*;
import javax.mail.internet.*;

public class ContactForm extends Applet implements ActionListener
{
	/*
	 * Set up the variables used in the applet
	 */
	TextField email = new TextField(30); // get the email address
	TextField statusOf = new TextField(30); // give the user feedback
	TextArea message = new TextArea(10,30); // get the user message
	TextField subject = new TextField(30); // get the user comment / inquiry
	Button submit = new Button ("Submit Comment"); // submit button
	
	/*
	 * init() method used to start applet
	 * @see java.applet.Applet#init()
	 */
	public void init()
	{
		// sets the Applet size
		setSize(350, 425);
		// sets the applet color
		Color bg = new Color(69,139,0); // rha website color pattern
		setBackground(bg);
		setLayout (new FlowLayout());
		Label welcome = new Label("Residence Hall Association - Comment Card");
		Font f = new Font ("Verdana", 8, 14);
		welcome.setFont(f);
		add(welcome);
		add(new Label("Application Status:  "));
		statusOf.setText("Running...");
		add(statusOf);
		add(new Label("Email:    "));
		add(email);
		email.setText ("name@address.com");
		add(new Label("Comment: "));
		add(subject);
		add(new Label("Enter Message:"));
		add(message);
		add(submit);
		
		email.requestFocus();
		
		email.addActionListener(this);
		subject.addActionListener(this);
		submit.addActionListener(this);
	} // init
	
	
	public void actionPerformed (ActionEvent event)
	{
		
		/*
		String messageText = message.getText();
		message.setText("Sending Email from: " + email.getText() + '\n' + 
						"Regarding: " + subject.getText() + '\n' + "Message: " + messageText);
						*/
		String status = "";
		if (subject.getText().equals("") && message.getText().equals("") && email.getText().equals(""))
		{
			statusOf.setText("All Fields Empty");
			email.requestFocus();
		}
		else if (subject.getText().equals("") && message.getText().equals(""))
		{
			statusOf.setText("Empty Subject & Message Fields");
			subject.requestFocus();
		}
		else if (message.getText().equals(""))
		{
			statusOf.setText("Empty Message Field");
			message.requestFocus();
		}
		else if (subject.getText().equals(""))
		{
			statusOf.setText("Empty Subject Field");
			subject.requestFocus();
		}
		else if (email.getText().equals(""))
		{
			statusOf.setText("Empty Email Field");
			email.requestFocus();
		}
		else {
			try
			{
				statusOf.setText("Preparing to send..");
				String smtpServer= "mailhost.pdx.edu";
				String to="laughnan@pdx.edu";
				String from;
				from = email.getText();
				String subject2;
				subject2 = "Comment Card From Website";
				String body;
				body = "Regarding: " + subject.getText() + '\n' + "Message: " + message.getText();
				status = send(smtpServer, to, from, subject2, body);
			}
			catch (Exception ex)
			{
				System.out.println("Usage: java mail for RHA website"
								   +" smtpServer toAddress fromAddress subjectText bodyText");
				ex.printStackTrace();
				setToDefault(status);
			}
			setToDefault(status);
		}
		
	} // action performed

	
	public void setToDefault (String foo) 
	{
		email.setText("name@address.com");
		subject.setText("");
		statusOf.setText(foo);
		email.requestFocus();
	}
	
	public String send(String smtpServer, String to, String from
							, String subject, String body)
	{
		try
		{
			Properties props = System.getProperties();
			// -- Attaching to default Session, or we could start a new one --
			props.put("mail.smtp.host", smtpServer);
			Session session = Session.getDefaultInstance(props, null);
			// -- Create a new message --
			Message msg = new MimeMessage(session);
			// -- Set the FROM and TO fields --
			msg.setFrom(new InternetAddress(from));
			msg.setRecipients(Message.RecipientType.TO,
							  InternetAddress.parse(to, false));
			// -- Set the subject and body text --
			msg.setSubject(subject);
			msg.setText(body);
			// -- Set some other header information --
			msg.setSentDate(new Date());
			// -- Send the message --
			Transport.send(msg);
			message.setText("");
			return ("Message Sent Successfully");
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			message.setText("");
			return ("Comment Failed: Check Email Address");
		}
	}
	
} // public class rha_contact


HTML: (test.html)

<HTML>
  <applet code=ContactForm.class name=RHAContactForm archive=mail.jar width=350 height=500>
  There is an issue with the applet.
  </applet>
</HMTL>

I hope to get this up and running by September 15th, 2010. I have messed around quite a bit, but I can't seem to get it to run on the web, though it still runs on the Eclipse workspace.

laughnan
Newbie Poster
24 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

Correction: to a pdx.edu email NOT gmail

laughnan
Newbie Poster
24 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

What error messages are shown in the browser's java console?

Your class is in a package and the applet tag's attribute: code= doesn't show that. If you add the package name then you will have to move the class file into that folder.
If you want to have all the files in the same folder, remove the package statement.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

I have removed the package, but it still gives me the "RHAContactForm applet notloaded"

The Error Console is:

Java Plug-in 1.6.0_20
Using JRE version 1.6.0_20-b02-279-10M3065 Java HotSpot(TM) Client VM
User home directory = /Users/alexlaughnan
laughnan
Newbie Poster
24 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 
and
"RHAContactForm applet notloaded"

I've never used the name= attribute on an applet tag and don't know why the error message refers to the name vs the class name given in the the code= attribute. Try removing the name= attribute.

When you said "Error console" is that the Browser's Java console? I see you're on linux so my WinXP experience won't be any use.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

I removed the name attribute and I still have the same issue. Notloading.

I am running on a Mac OS X, but I could try and bring the website up.

Right now all the files are uploaded at: http://web.pdx.edu/~laughnan/test/test.html

laughnan
Newbie Poster
24 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 
I removed the name attribute and I still have the same


Is the error message the same without the name= attribute?
I would think it would be different?

You really need to find the browser's java console to be able to debug.
If you think you have found it, verify it by adding a System.out.println("my applet");
to your code and see if it prints on the console.

My mail experience says you need two jar files:

archive="mail.jar,activation.jar" width=350 height=500>

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Here is what the Java console says:

java.lang.ClassFormatError: Incompatible magic value 1886741100 in class file ContactForm
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:209)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:144)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:654)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:785)
at sun.plugin.AppletViewer.createApplet(AppletViewer.java:2354)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
at sun.applet.AppletPanel.run(AppletPanel.java:368)
at java.lang.Thread.run(Thread.java:637)


I have included the mail.jar and the activation.jar

laughnan
Newbie Poster
24 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

That looks like you have a bad .class file.
If you have a hex editor, open the class file and look at the first few bytes. They should look like the following line:
CAFE BABE 0003 002D 0033 0100

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Okay, I will look at it! How can I compile this applet on a Terminal (cmd) prompt? I need to include the jar files too..I think that if I compile it on a cmd prompt it will fix the .class file!

laughnan
Newbie Poster
24 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 
How can I compile this applet


Use the javac command to compile a .java file.

Sorry, I don't know about development on a Mac

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

alexlaughnan@time:RHAapplet $java ContactForm.java -jar mail.jar activation.jar
Exception in thread "main" java.lang.NoClassDefFoundError: ContactForm/java
Caused by: java.lang.ClassNotFoundException: ContactForm.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

laughnan
Newbie Poster
24 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

Class file:

public class ContactForm extends java.applet.Applet
    implements java.awt.event.ActionListener
{
    /* Fields */
    java.awt.TextField email;
    java.awt.TextField statusOf;
    java.awt.TextArea message;
    java.awt.TextField subject;
    java.awt.Button submit;

    /* Constructors */
    public ContactForm() {
    }


    /* Methods */
    public void init() {
    }

    public void actionPerformed(java.awt.event.ActionEvent) {
    }

    public void setToDefault(java.lang.String) {
    }

    public java.lang.String send(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) {
    }

}
laughnan
Newbie Poster
24 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 
alexlaughnan@time:RHAapplet $java ContactForm.java -jar mail.jar activation.jar


Several problems with your command line:
1) use the javac command
2) use the -classpath option (not -jar)
3) put the options before the filename.

javac -classpath mail.jar ContactForm.java

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

alexlaughnan@time:RHAapplet $javac -classpath mail.jar ContactForm.java
alexlaughnan@time:RHAapplet $


Then what do I do to run the program??

laughnan
Newbie Poster
24 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

P.S. the .class file didn't change it still looks the same as above

laughnan
Newbie Poster
24 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

But the applet loaded...now let me see if it works on the server!

laughnan
Newbie Poster
24 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 
the .class file didn't change it still looks the same as above


Sorry, I miss where you posted the contents of the .class fileBut the applet loaded
What does that mean?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

It now shows the applet as opposed to saying "notloaded" but now the mail doesn't send. So I will just debug that for a bit and no doubt I'll be back on here soon.

I have it on the web:

http://rha.pdx.edu/test/test.html

laughnan
Newbie Poster
24 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 
the mail doesn't send.


What does it show in the browser's Java console?

Here's what I see:Java Plug-in 1.6.0_20
Using JRE version 1.6.0_20-b02 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\Owner

----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to
----------------------------------------------------

running init
java.security.AccessControlException: access denied (java.util.PropertyPermission * read,write)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPropertiesAccess(Unknown Source)
at java.lang.System.getProperties(Unknown Source)
at ContactForm.send(ContactForm.java:140)
at ContactForm.actionPerformed(ContactForm.java:112)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Applet's need permission to get data from a user's PC.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: