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.