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.

Correction: to a pdx.edu email NOT gmail

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.

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

<applet code=ContactForm.class name=RHAContactForm

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.

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

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>

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

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

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!

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

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)

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) {
    }

}

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

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


Then what do I do to run the program??

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

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

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

Sorry, I miss where you posted the contents of the .class file

But the applet loaded

What does that mean?

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

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 <n>
----------------------------------------------------

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.

That is what I get too. Can you think of an easy fix?

Or a better way to send an email from java?

I changed the Properties so it doesn't call the get method for the SystemProperties now it just is:

Properties props = new Properties();

And now (if you look at the website: http://rha.pdx.edu/test/test.html) the applet says "Preparing to Send" but then it seems to freeze or at least it did on my browser. Why is this?

I get a timed out error trying to connect to your page.
Only suggestion I can think to make is to add println()s to isolate where the code is hanging.

Here is the error that I get.

java.security.AccessControlException: access denied (java.net.SocketPermission mailhost.pdx.edu resolve)
	at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
	at java.security.AccessController.checkPermission(AccessController.java:546)
	at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
	at java.lang.SecurityManager.checkConnect(SecurityManager.java:1031)
	at sun.plugin.security.ActivatorSecurityManager.checkConnect(ActivatorSecurityManager.java:202)
	at java.net.InetAddress.getAllByName0(InetAddress.java:1146)
	at java.net.InetAddress.getAllByName(InetAddress.java:1084)
	at java.net.InetAddress.getAllByName(InetAddress.java:1020)
	at java.net.InetAddress.getByName(InetAddress.java:970)
	at javax.mail.URLName.getHostAddress(URLName.java:482)
	at javax.mail.URLName.hashCode(URLName.java:458)
	at java.util.Hashtable.get(Hashtable.java:334)
	at javax.mail.Session.getPasswordAuthentication(Session.java:823)
	at javax.mail.Service.connect(Service.java:274)
	at javax.mail.Service.connect(Service.java:172)
	at javax.mail.Service.connect(Service.java:121)
	at javax.mail.Transport.send0(Transport.java:190)
	at javax.mail.Transport.send(Transport.java:120)
	at ContactForm.send(ContactForm.java:165)
	at ContactForm.actionPerformed(ContactForm.java:113)
	at java.awt.Button.processActionEvent(Button.java:392)
	at java.awt.Button.processEvent(Button.java:360)
	at java.awt.Component.dispatchEventImpl(Component.java:4714)
	at java.awt.Component.dispatchEvent(Component.java:4544)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:635)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

This error occurs as it tries to use this line:

Transport.send(simpleMessage); // where simpleMessage is the email being sent out

Applets need permission to connect to a host other than the one they are loaded from.

So is there a way to work around that?

I know of two ways to give an applet permission, sign it jar file
or add entries to the .java.policy file on all user's PCs.

What is a sign it jar file?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.