dcsimg
Attachments not recognised
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Wednesday, April 9, 2003 03:13 AM

I'm new to java so please be gentle I have a simple testcase, but i can't get it to work. On the mailserver (MS exchange 5.5) there is 1 mail with 2 attachments (txt-files). I want to write (standard output) the contents of the attachments. However, my code recognised the mail as multipart, but with one (?) BodyPart (i.e. the text of the orginal body). What am i missing? P.S. every Disposition I checked came out null import java.io.*; import java.lang.reflect.*; import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; import java.sql.SQLException; import java.sql.*; import oracle.sql.*;    More>>

I'm new to java so please be gentle

I have a simple testcase, but i can't get it to work.

On the mailserver (MS exchange 5.5) there is 1 mail with 2 attachments (txt-files). I want to write (standard output) the contents of the attachments.

However, my code recognised the mail as multipart, but with one (?) BodyPart (i.e. the text of the orginal body).
What am i missing?


P.S. every Disposition I checked came out null


			
import java.io.*;
import java.lang.reflect.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import java.sql.SQLException;
import java.sql.*;
import oracle.sql.*;
import oracle.jdbc.*;

public class TestMail
{
static String g_username ;
static String g_password ;
static String g_host = "MYMAILHOST" ;
static String g_folder = "INBOX" ;

private void WriteMyText
( InputStream p_stream
, String p_from
, String p_subject
)
throws Exception
{ int length = -1;
byte[] buffer = new byte[1024];
while ((length = p_stream.read(buffer)) != -1)
System.out.write(buffer, 0, length);
System.out.println("Ready with message");
}

public void getPartMimeType
( Part p_part
, String p_from
, String p_subject
) throws Exception
{
System.out.println(p_part.getContentType());
if (p_part.isMimeType("multipart/*"))
{ Multipart mp = (Multipart)p_part.getContent();
for (int x = 0; x < mp.getCount(); x++)
{ if (mp.getBodyPart(x).isMimeType("multipart/*"))
getPartMimeType(mp.getBodyPart(x),p_from,p_subject);
else
WriteMyText(p_part.getInputStream(),p_from,p_subject);
}
}
else
WriteMyText(p_part.getInputStream(),p_from,p_subject);
}

public void ReadMyList
( String p_username
, String p_password
)
throws Exception
{ Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
session.setDebug(false);
Store store = session.getStore("imap");
store.connect(g_host, p_username, p_password);
Folder folder = store.getFolder(g_folder);
if (folder == null || !folder.exists())
{
System.out.println("Invalid folder: " + folder.getName());
System.exit(1);
}
folder.open(Folder.READ_WRITE);
int count = folder.getMessageCount();
if (count == 0)
{ folder.close(false);
store.close();
return;
}
Message message[] = folder.getMessages();
String from, subject;
//
for (int i=0, n=message.length; i
{ from = message[i].getFrom()[0]+"";
subject = message[i].getSubject();
message[i].writeTo(System.out);
//
if (message[i].isMimeType("multipart/*" ))
{
Multipart multipart = (Multipart) message[i].getContent();
System.out.println("No. attachments: " + multipart.getCount());
for (int l_counter = 0; l_counter < multipart.getCount(); l_counter++)
{ Part part = multipart.getBodyPart(l_counter);
getPartMimeType(part,from,subject);
}
}
else
WriteMyText(message[i].getInputStream(),from,subject);
}
folder.close(true);
store.close();
}

public static void GetMyMail
( String p_username
, String p_paswoord
)
throws Exception
{ TestMail rmail = new TestMail();
rmail.ReadMyList(p_username, p_paswoord);
System.out.println("Ready");
}

public static void main
( String[] args
)
throws Exception
{ // username, password
GetMyMail(args[0],args[1]);
}

   <<Less
About | Sitemap | Contact