• Post Reply Bookmark Topic Watch Topic
  • New Topic

Disposition value is null in reading attachment  RSS feed

 
Sk naaz
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to read attachment using javamail but when I am using getDisposition function ,it's value is always null evenif for attachments.
This is the code I am using.
import java.io.*;
import javax.swing.*;
import java.util.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import java.sql.*;

public class try_attachment{

public static void main(String args[])throws Exception
{
String host = "host";
String username = "username";
String password = "password";

Properties Prop = new Properties();
Session session = Session.getInstance(Prop,null);
Store store = session.getStore("imap");
store.connect(host,username,password);
// open Inbox
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
// get Directory
Message msg[] = folder.getMessages();
for (int i=0;i<msg.length;i++)
{
Object content = msg[i].getContent();
if(content instanceof Multipart)
{

handleMultipart(( Multipart)content);
}
else
{
handlePart(msg[i]);
}
}
folder.close(false);
store.close();
}


public static void handleMultipart( Multipart multipart)
throws MessagingException, IOException
{
for(int i=0;i<multipart.getCount();i++)
{
handlePart(multipart.getBodyPart(i));
}
}
public static void handlePart(Part part)throws MessagingException,IOException
{

String disposition = part.getDisposition();
String contentType = part.getContentType();
// trying to read disposition value
System.out.println("Hello "+ disposition);
try{if (disposition == null)
{
System.out.println("Null: " +contentType);
//checkif plain
//if ((contentType.length()>=10)&& (contentType.toLowerCase().substring(0,10).equals("text/plain")))
//{
//part.writeTo(System.out);
//}
}

else if(disposition.equalsIgnoreCase(Part.ATTACHMENT))
{

System.out.println("Attachment:" + part.getFileName() + ":" + contentType);

saveFile(part.getFileName(),part.getInputStream());
}
else if(disposition.equalsIgnoreCase(Part.INLINE))
{
System.out.println("Inline :"+ part.getFileName()+":" + contentType);
saveFile(part.getFileName(),part.getInputStream());
}
else
{
System.out.println("Other");
}
}
catch(Exception e)
{ System.out.println(e);}
}
public static void saveFile(String filename, InputStream input) throws IOException
{
if (filename == null)
{
filename = File.createTempFile("testmail",".out").getName();
}
//do not overwrite
File file = new File(filename);
for(int i=0;file.exists();i++)
{
file = new File(filename+i);
}
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
BufferedInputStream bis = new BufferedInputStream(input);
int aByte;
while ((aByte = bis.read())!= -1)
{
bos.write(aByte);
}
bos.flush();
bos.close();
bis.close();
}
}
 
Dragan Jovanovic
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I'm having the same problem - disposition for my attachments (not all, just for one that I need) is null. So I wander, is attachment everything that looks like an attachment ? I mean, if I see it as attachment in outlook, is it realy an attachment in terms of javamail API ? Or I should look for some other data structure inside MimeMultiPart ?
You can see how my test message looks like in outlook here. I see two attachments. My application can read delivery report.txt, but in the next iteration (when it should reach this elektronski račun... attachment), disposition of the Part instance is null.
And, I forgot to tell you, that elektronski računi... attachment is infact the original mail message for which this report message is generated. And it also contains attachments.
[ May 29, 2008: Message edited by: Dragan Jovanovic ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!