javamail/applet

Discussion in 'Java' started by anonymous@coolgroups.com, May 26, 2004.

  1. Guest

    can i use javamail stuff from an applet
     
    , May 26, 2004
    #1
    1. Advertising

  2. On Wed, 26 May 2004 19:15:26 GMT, wrote:

    > can i use javamail stuff from an applet


    depends
     
    Andrew Thompson, May 26, 2004
    #2
    1. Advertising

  3. Roedy Green Guest

    On Wed, 26 May 2004 19:15:26 GMT, wrote or
    quoted :

    >can i use javamail stuff from an applet


    Yes but.

    You can only talk to the mailserver if it lives at the same IP that
    served the Applet.

    You are willing to wait for the fat Javamail jar to download. Best to
    do this with Java Web Start.


    see http://mindprod.com/jgloss/javawebstart.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, May 26, 2004
    #3
  4. Guest

    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.
     
    , Jun 6, 2004
    #4
  5. Roedy Green Guest

    On Sun, 06 Jun 2004 01:09:48 GMT, wrote or
    quoted :

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


    yes but. The problem is the entire Javamail jar would be downloaded
    each time, and you could only talk to the mailserver on the same
    machine from which the applet was loaded. You are better off with JWS
    so that the Javamail jar gets downloaded only once.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jun 6, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Love

    applet vs. canvas in applet

    Rob Love, Jul 7, 2003, in forum: Java
    Replies:
    1
    Views:
    640
    Liu, Chunyen
    Jul 7, 2003
  2. webster
    Replies:
    0
    Views:
    877
    webster
    Jul 20, 2003
  3. Andrew Thompson
    Replies:
    0
    Views:
    429
    Andrew Thompson
    Aug 5, 2003
  4. Blah Blah
    Replies:
    0
    Views:
    1,446
    Blah Blah
    Aug 13, 2003
  5. John Taylor

    Need help with JavaMail Applet

    John Taylor, Sep 10, 2003, in forum: Java
    Replies:
    1
    Views:
    446
    Roedy Green
    Sep 10, 2003
Loading...

Share This Page