Content einer email in sql-datenbank speichern?

  • Themenstarter Themenstarter dalco
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
D

dalco

Gast
hallo,
ich versuche einen kleinen mailbrowser zu schreiben, dazu will ich die bereits abgerufenen mails in einer sql datenbank speichern, es klappt bei der adresse, subjekt und einen datum.
beim inhalt, den ich mit:

messages.getContent();

bekomme, speichert er es nicht in die datenbank, hab das feld schon als VARCHAR oder TEXT genommen, wie kann ich den inhalt speichern, vorallem wenn der inhalt der mail nicht nur aus einfachem text besteht, denn damit geht es.

und mit:

messages.getSentDate();

wollte ich die zeit der letzten mail festhalten, um danach nur alle danach kommenden erneut in die datenbank zu speichern, alle abfragen auf diese zeit hin scheitern aber leider.

hier der teil des speichern:
-------------------------------------------------------------------------
Code:
/ Connect to the POP3 server
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore(provider);
store.connect(host, username, password);

// Open the folder
Folder inbox = store.getFolder("INBOX");

if (inbox == null) {
System.out.println("No INBOX");
System.exit(1);
}
inbox.open(Folder.READ_ONLY);

// Get the messages from the server
Message[] messages = inbox.getMessages();

for (int i = 0; i < messages.length; i++) {
Message m = messages[i];

System.out.println("-------------------------------------------------- MAIL: "+i);
System.out.println( " Von: " + m.getFrom()[0] );
System.out.println("-- BETREFF -- " + messages[i].getSubject());
System.out.println("-- ZEIT -- " + messages[i].getSentDate());
System.out.println("-- INHALT -- " + messages[i].getContent() );



data[i][0] = messages[i].getSubject();
data[i][2] = messages[i].getSentDate();
data[i][1] = m.getFrom()[0];


db.connectToDatabase();
con = db.getConnection();
update.doEntrie(con , "INSERT INTO tino (id, fromAdr, subject, sendTime, contend) VALUES (\""
+i+"\",\""
+m.getFrom()[0]+"\",\""
+messages[i].getSubject()+"\",\""
+messages[i].getSentDate()+"\",\""
+messages[i].getContent()+"\")");
db.close();
}
-----------------------------------------------------

danke für eure tipps
mfg
Tino
 
du musst herausfinden,was die methode zurück liefert,
versuch das mal:

Code:
public void printParts(Part p)
{
Object o = p.getContent();
if (o instanceof String)
{
System.out.println(“Dies ist ein String“);
System.out.println((String) o);
}
else if (o instanceof Multipart)
{
System.out.println(“Dies ist ein Multipart“);
Multipart mp = (Multipart) o;
int count = mp.getCount();
for (int i=0; i<count; i++)
{
printParts(mp.getBodyPart(i));
}
}
else if (o instanceof InputStream)
{
System.out.println(“Dies ist ein InputStream“);
InputStream is = (InputStream) o;
int c;
while ((c = is.read()) != -1) System.out.write(c);
}
}

wenns nicht klappt schau dir das mal an.

The data type of the "content" is returned by the getContentType() method. The MIME typing system is used to name data types.

The "content" of a Part is available in various formats:

As a DataHandler - using the getDataHandler() method. The "content" of a Part is also available through a javax.activation.DataHandler object. The DataHandler object allows clients to discover the operations available on the content, and to instantiate the appropriate component to perform those operations.
As an input stream - using the getInputStream() method. Any mail-specific encodings are decoded before this stream is returned.
As a Java object - using the getContent() method. This method returns the "content" as a Java object. The returned object is of course dependent on the content itself. In particular, a "multipart" Part's content is always a Multipart or subclass thereof. That is, getContent() on a "multipart" type Part will always return a Multipart (or subclass) object.
Part provides the writeTo() method that streams out its bytestream in mail-safe form suitable for transmission. This bytestream is typically an aggregation of the Part attributes and its content's bytestream.
Message and BodyPart implement the Part interface. Note that in MIME parlance, Part models an Entity (RFC 2045, Section 2.4).

ok, hab damit auch noch nicht gearbeitet, aber du musst dir mal den typ über getContentType() hohlen und dnan kannst du irgendwie mit dem datahandler arbeiten.
die mail api ist hier dokumentiert http://java.sun.com/j2ee/1.4/docs/api/index.html

ps: da hab ich auch was gefunden
 
hallo,

habs versucht und es wird mir auch angezeigt, wenn es ne normale text/plain ist kann es in der sql datenbank gespeichert werden, bei allem anderen kommen fehler, ich weiß nicht weiter???
mfg
Tino
 
Alles was kein "text/plain" ist, ist eben kein Text. Folglich kannst du es nicht als einfachen Text speichern.

Entweder baust du deine DB so um, dass du statt TEXT -> BLOB benutzt und dort dann ganze Java-Objekte reinstecken kannst (welche du dann auch wieder als Java-Objekte rauslesen kannst), ODER du findest einen Weg die anderen, nicht-TEXT/PLAIN Typen in etwas "brauchbares" zu wandeln.

- Alex
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben