Email attachments: the disposition is null
843830May 21 2003 — edited May 26 2003Hi,
I have written an email program that is supposed to strip off attachments from their emails and write them to a file. In my testing, I have created an email with an attachment. However, the disposition of the attachment is null. I would appreciate any assistance in determining why the disposition is null and how to rectify this. I am including my code below and have indicated with /**comments*/ where the problem occurs.
TIA,
Jeff
package mil.mii.sources;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import mil.mii.mail.IDataHandler;
import mil.mii.mail.ISubjectLine;
import mil.mii.mail.MessageWrapper;
public class CamsAloneEmailHandler implements IDataHandler
{
private final String cSupports = "cams";
public CamsAloneEmailHandler()
{
}
public void handleMessage( MessageWrapper mMsg, ISubjectLine.Action mAction )
{
Multipart multipart = ( Multipart )mMsg.cContent;
Part part = null;
String disposition = null;
if (multipart == null)
{
System.out.println( "Content is null." );
}
/* Loop through the message determining its parts. If there is an
attachment, save it to a directory. */
try
{
for ( int index = 0, n = multipart.getCount(); index < n; index++ )
{
System.out.println( "index = " + index );
part = multipart.getBodyPart( index );
disposition = part.getDisposition();
if (disposition == null)
System.out.println( "disposition is null" );
/** The line above is printed and none of the remaining code is
executed. */
if ( disposition.equals( part.ATTACHMENT ) )
{
writeToDir( part, part.getInputStream() );
}
}
}
catch( MessagingException me )
{
System.out.println( me );
}
catch( IOException ioe )
{
System.out.println( ioe );
}
}
// private method that writes the attachments to the CAMS directory
private void writeToDir( Part mAttach, InputStream mInput )
{
File camsFile = new File( "C:/temp_test/cams.txt" );
try
{
if ( !camsFile.exists() )
{
camsFile.createNewFile();
}
FileOutputStream fos = new FileOutputStream( camsFile );
BufferedOutputStream bos = new BufferedOutputStream( fos );
BufferedInputStream bis = new BufferedInputStream( mInput );
int aByte;
while ( ( aByte = bis.read() ) != -1 )
{
bos.write( aByte );
bos.flush();
bos.close();
bis.close();
}
}
catch( IOException ioe )
{
System.out.println( ioe );
}
}
public String supportsDataType()
{
return cSupports;
}
}