javamail/applet

M

mromarkhan

Peace be unto you.

Q: Can I use JavaMail in applets?
http://java.sun.com/products/javamail/FAQ.html#applets



A jnlp solution:
On server
mailapi.jar
activation.jar
pop3.jar
SignedMail.jar
Support.jnlp
Mail.jnlp
Activation.jnlp

Inside SignedMail.jar
MailSorter.java
TableSorter.java - Sun tutorial file

-- Mail.jnlp
<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+"
codebase="http://members.rogers.com/mromarkhan"
href="Mail.jnlp">
<information>
<title>Read Pop Email</title>
<vendor>Omar</vendor>
<homepage href="pop.html"/>
<description>Read Pop Email</description>
<description kind="short">Read Pop Email</description>
<icon href="pop.jpg"/>
<offline-allowed/>
</information>
<security>
<all-permissions/>
</security>
<resources>
<j2se version="1.4"/>
<jar href="SignedMail.jar"/>
<extension name="JavaMail Api" href="Support.jnlp"/>
<extension name="Activation Api" href="Activation.jnlp"/>
</resources>
<application-desc main-class="MailTableSorter"/>
</jnlp>
-- Mail.jnlp

-- Support.jnlp
<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+" codebase="http://members.rogers.com/mromarkhan" href="Support.jnlp">
<information>
<title>JavaMail</title>
<vendor>Sun Microsystems, Inc.</vendor>
</information>
<resources>
<jar href="pop3.jar" download="eager" />
<jar href="mailapi.jar" download="eager" />
</resources>
<component-desc/>
</jnlp>
-- Support.jnlp

-- Activation.jnlp
<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+" codebase="http://members.rogers.com/mromarkhan" href="Activation.jnlp">
<information>
<title>Activation API</title>
<vendor>Sun Microsystems, Inc.</vendor>
</information>
<resources>
<jar href="activation.jar" download="eager" />
</resources>
<component-desc/>
</jnlp>
-- Activation.jnlp


-- MailTableSorter.java
<code>
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Date;
import java.awt.event.*;
import java.awt.*;
import javax.mail.Flags.*;
import javax.swing.table.*;

public class MailTableSorter extends JPanel implements ActionListener
{
JPopupMenu popup;
JTable table;
static Folder inboxFolder;

String hostName = "";
String userName = "";
String passWord = "";


class PopupListener extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
maybeShowPopup(e);
}

public void mouseReleased(MouseEvent e)
{
maybeShowPopup(e);
}

private void maybeShowPopup(MouseEvent e)
{
int selectedRows [] = table.getSelectedRows();
if (selectedRows.length == 0)
{
return;
}
if (e.isPopupTrigger())
{
popup.show(e.getComponent(),e.getX(), e.getY());
}
}
}

public void actionPerformed(ActionEvent e)
{
int selectedRows [] = table.getSelectedRows();
if (selectedRows.length == 0)
{
return;
}
try
{
for(int i = 0;i< selectedRows.length;i++)
{
Message message = (Message)table.getModel().getValueAt(selectedRows,3);
if (message.isSet(Flags.Flag.DELETED))
{
message.setFlag(Flags.Flag.DELETED,false);
}
else
{
message.setFlag(Flags.Flag.DELETED,true);
}

}
}
catch(Exception ex)
{
System.err.println(ex);
}
}

public MailTableSorter()
{
super(new GridLayout(1,0));

hostName = JOptionPane.showInputDialog("Please enter your pop address ex. pop");
userName = JOptionPane.showInputDialog("Your user name");
passWord = JOptionPane.showInputDialog("Your password");


//...where the GUI is constructed:
//Create the popup menu.
popup = new JPopupMenu();
JMenuItem menuItem = new JMenuItem("Delete selection");
menuItem.addActionListener(this);
popup.add(menuItem);

TableSorter sorter = new TableSorter(new MyTableModel());
table = new JTable(sorter);
MouseListener popupListener = new PopupListener();
table.addMouseListener(popupListener);

sorter.setTableHeader(table.getTableHeader());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
//Set up tool tips for column headers.
table.getTableHeader().setToolTipText(
"Click to specify sorting; Control-Click to specify secondary sorting");
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
}

class MyTableModel extends AbstractTableModel
{
private Object[][] data=null;

public MyTableModel()
{
Message messages[]=null;
try
{
// Get system properties
Properties props = System.getProperties();
// Get session
Session session = Session.getDefaultInstance(props,null); //java.lang.NullPointerException
Store store = session.getStore(new URLName("pop3://"+userName+":"+passWord+"@"+hostName+":110/INBOX"));
// Get folder
store.connect();
inboxFolder = store.getFolder("INBOX");
inboxFolder.open(Folder.READ_WRITE);//Exception in thread "main" java.lang.IllegalStateException: Folder not open
messages= inboxFolder.getMessages();
data = new Object[messages.length][4];
for(int i = 0; i < messages.length;i++)
{
data[0] = messages.getSubject();
Address[] addresses = messages.getFrom();
data[1] = ((InternetAddress)addresses[0]).getPersonal();
data[2] = messages.getSentDate();
data[3] = messages;
}
}
catch(Exception e)
{
System.exit(1);
}


for (int i=0; i < messages.length; i++)
{
System.out.print(" row " + i + ":");
for (int j=0; j < 3; j++)
{
System.out.print(" " + data[j]);
}
System.out.println();
}
}



private String[] columnNames =
{
"Subject",
"From",
"Date"
};


public int getColumnCount()
{
return columnNames.length;
}

public int getRowCount()
{
return data.length;
}

public String getColumnName(int col)
{
return columnNames[col];
}

public Object getValueAt(int row, int col)
{
return data[row][col];
}


}

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI()
{
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("MailTableSorter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
MailTableSorter newContentPane = new MailTableSorter();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent wE)
{
try
{
inboxFolder.close(true);
}
catch(Exception e)
{
System.err.println(e.toString());
}
}
}
);
frame.setVisible(true);
}

public static void main(String[] args)
{
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
</code>


-- Building
javac -classpath ".;C:\downloads\javamail-1_3_1\javamail-1.3.1\lib\pop3.jar;C:\downloads\javamail-1_3_1\javamail-1.3.1\lib\mailapi.jar;C:\downloads\jaf-1_0_2-upd\jaf-1.0.2\activation.jar" MailTableSorter.java

jar -cvf Omar.jar *.class
REM enter your info
keytool -genkey -alias omarFiles -keypass omarkey -keystore omarstore -storepass omarstore
REM pass phrase for keystore:eek:marstore pass for omarFiles:eek:markey
jarsigner -keystore omarstore -signedjar SignedMail.jar Omar.jar omarFiles




I had a problem as defined by
"If Java Web Start prints out a bad installation error message:"
http://java.sun.com/products/javawebstart/1.2/docs/installguide.html#trouble

I fixed it by
Error message stating
c:\Program Files\Java\j2re1.5.0\bin\javaw.exe
is non existant.
Which is true.

I clicked on the shortcut in C:\Java Web Start.

The Java Web Start Application Manager appears.
I went to file menu then preferences sub menu.
I clicked the Java tab.
I noticed the runtime versions pointed to non existing files.
I removed them, then I clicked find and went to my java bin directory.
It found it.
Everything was super afterwards.

-- References
http://java.sun.com/developer/technicalArticles/JavaLP/javawebstart/index.html
By Raghavan N. Srinivas

http://java.sun.com/docs/books/tutorial/security1.2/toolsign/step3.html
By Mary Dageforde

TableSorter.java
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#sorting

//JAR resources in JNLP file are not signed by same certificate
How do I use JAR files that are signed by different certificates?
http://java.sun.com/products/javawebstart/faq.html#72

How to Use Popup menus
http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html

Have a Good Day.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top