Solved

MIME-parts and JavaMail

Posted on 2003-11-03
11
473 Views
Last Modified: 2013-11-24
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
0
Comment
Question by:Frank-22
  • 6
  • 4
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9673665
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.
0
 

Author Comment

by:Frank-22
ID: 9673795
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  
0
 

Author Comment

by:Frank-22
ID: 9674106
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;
    }
    */
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9674149
And what seems to be the problem?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9674416
(almost) never have this sort of thing:


>>catch(Exception e){}
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Expert Comment

by:Euroman_21
ID: 9676516
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

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9677927
All I can see of your statement is the following:

>>String query = "";

What is it meant to be - what will your SQL be?
0
 

Author Comment

by:Frank-22
ID: 9678682
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  

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9678777
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
0
 

Author Comment

by:Frank-22
ID: 9678977
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
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 9679048
Yep, looks OK.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now