Link to home
Start Free TrialLog in
Avatar of Frank-22
Frank-22

asked on

MIME-parts and JavaMail

Hello

I'm doing an email client with JavaMail and I got a question regarding storing the email-messages.

I would like to write a modul called 'Store' for my program, that does the following.

1) Then this modul recieves an email it would like to split it up into MIME-parts (From, id, mailbody) etc. I would like these parts to be stored in a MySQLDB.

2) Is it possible to add feature that reassembles these MIME-parts into a readable email ?

I hope that there are someone of You, how can answer these questions for me !

Yours
Frank
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Just look at the Part/MimePart interface. If you decompose the mail into its parts you can write it to a db, possibly with the assistance of serialization and ObjectOutputStream. Each Part should be renderable on the symmetrical read operation with ObjectInputStream.
Avatar of Frank-22
Frank-22

ASKER

Yeah,

I know I can do this to decode the message.

MimePart part = ...
  String rawvalue = null;
  String  value = null;
  try {
    if ((rawvalue = part.getHeader("X-mailer")[0]) != null)
      value = MimeUtility.decodeText(rawvalue);
  } catch (UnsupportedEncodingException e) {
      // Don't care
      value = rawvalue;
  } catch (MessagingException me) { }

  return value;

But how do I save these MIME-parts into a MYSQLDB, and Reassemble them into an readable email ?

The reason for me asking is because I have looked through all the books I have at home for a method on how to do this, but have been unable to find any !!!

And I promissed someone, that I could do this easily and present a working modul tommorow !

So it would "really" appricate if You could show me how to do it !!

Frank  
Hi

As of now my Store-Modul looks the code below.

I'm uanable to get the store-feature to function.

What I'm I doing wrong ? How do I get it to work ?

Frank

package modmail.store;
import javax.mail.*;
import modmail.listener.*;
import java.sql.*;

public class mySQL implements GuiListener, ReceiveListener
{
    private Connection connectionToDB;
    private String password = "";
    private String login = "";
    private String selectDB = "";

    //Start of RecieveListener's methods
    public void saveSuccessful(boolean b, String[] arrayOfMailID, int errorcode)
    {
    }

    public void hasNewMail(String[] arrayOfMailID)
    {
    }
    //End of RecieveListener's methods

    //Start of GuiListener's methods

    /** Is activated when the button "Send" is pressed on the GUI.  */
    public void sendButtonClicked()
    {
      //Do nothing
    }

    public void reciveButtonClicked()
    {
    }
   
    public void loadMail()
    {
    }

    public void saveMail()
    {
    }
   
    public void deleteMail()
    {
    }
   
    public void undeleteMail()
    {
    }

    public void printMail()
    {
      //Do nothing
    }
    //End of GuiListener's methods

    private void connectToDb()
    {
      /** Temporaty code to login, get login info from config file later */
      if(password == "" || login == "" || selectDB == "")
          {
            password = "e3110";
            login = "root";
            selectDB = "//localhost/movieDB";
          }
      try{
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      connectionToDB = DriverManager.getConnection("jdbc:mysql:" + selectDB + "?user=" + login + "&password=" + password);
      }catch(Exception e){}
    }

    /** Adds an email to the mail database. */
    /*
    public void addEntry(Message mail) throws Exception
    {
      Statement stmt = null;
      String query = "";
      String subject = mail.getSubject();
      Date sendDate = mail.getSendDate();
      String fromEmail = (mail.getSender()).toString();
      Address[] replyToAdd = mail.getReplyTo();
      String[] replyTo = new String[replyToAdd.length];
      for(int i = 0; i > replyToAdd.length; i++)
          {
            replyTo[i] = replyToAdd.toString();
          }
      Date recievedDate = mail.getRecievedDate();
      String body = ""; // WRU real body txt?!?
      int ID = mail.getMessageID();
      stmt = connectionToDB.createStatement();
      stmt.executeUpdate(query);
      stmt = null;
    }
    */
}
And what seems to be the problem?
(almost) never have this sort of thing:


>>catch(Exception e){}
I get an unrecognized type every time I try to add !

Is there a better way of doing it than I'm allready doing it now ?

Sincerely

Frank

All I can see of your statement is the following:

>>String query = "";

What is it meant to be - what will your SQL be?
Hi and thank You for Your answer.

As You correctly singled out the String query = ""; is empty, that is because I can not get the sql query, that was suppose to insert the MiME-parts into the mySQL db.

I know how to make an sql - query, but unfornally not in this context.

So I would really appricate if You have a surgestion on how You think this SQL-Query should sound ?

Sincerely
Frank  

The first thing you need to do is have a table with columns in it to take all the pieces of data you want to save. You would then do an INSERT statement
Okay....

I got a table called maildb (movieDB is a typoo) would this be a correct query ?

String query = "insert into maildb (subject, sendDate, fromEmail) values ('" + mail.getSubject() + "','" + mail.getSendDate() + "','" + (mail.getSender()).toString() + "')";


Sincerely

Frank
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial