reading an attachment using Javamail

B

barry

Hi all, I wan't to extract the 'attachment' from the following eMail
message using Javamail (this is how the message from one particular
individual shows up in Microsoft Outlook. E-mails I receive from all
other people have the attachments separate and Outlook enables me to
save them using expected behaviour). Surely I don't have to write the
code to search for "begin 666", then an end, and pull the content in
between these two tags, and then unuuencode it.


From: (e-mail address removed)
Sent: 12 December 2003 09:55
To: (e-mail address removed)
Subject: MIMETest.txt

Barry, find attached the test message.
Regards
Joe.

<<MIMETest.txt>>



The attachment is MimeTest.txt. I have attempted to extract the
'attachment' using MultiPart messages but this approach failed because
the message appears to only have one part which contains both the
message and the content. E-mail messages I receive with attachments
from other people have multiple parts, a part for each attachment. Ie
there is something funny about this format of e-mail message I am
receiving. Here is some test/sample code I am using:


import javax.mail.*;
import java.io.InputStream;

public class Class2
{
public static void main(String[] args) throws Exception
{
Store s=Session.getInstance(System.getProperties(),null).getStore("pop3");
s.connect("MyMServer","MyServer/bbulsara23","password");
Folder f=s.getFolder("inbox");
f.open(Folder.READ_ONLY);
Message m=f.getMessages()[0];

if (m.getContent() instanceof Multipart)
System.out.println("instanceof 'Multipart'");
else System.out.println("m.getContent instanceof 'Part'");
Multipart mp=(Multipart)m.getContent();
System.out.println("mp.getCount()="+mp.getCount());
System.out.println("Subject="+m.getSubject());
System.out.println("ContentType="+m.getContentType());
InputStream is=mp.getBodyPart(0).getInputStream();

StringBuffer sb=new StringBuffer();
byte[] b=new byte[100000];
int noChars=is.read(b);
sb.append(new String(b,0,noChars));
System.out.println("MessageContent="+sb.toString());

f.close(false);
s.close();
}
}


And here is the output I see on the screen.

D:\test>java Class2
instanceof 'Multipart'
mp.getCount()=1
Subject=MIMETest.txt
ContentType=multipart/mixed;
boundary="----_=_NextPart_000_01C3C006.5CC35DB0"
MessageContent=
Barry, find attached the test message.
Regards
Joe.

<<MIMETest.txt>>



So is this (the ability to extract attachments encapsulated by begin
666 stuff end) facility part of JavaMail. I don't think it is but want
to be proven wrong. So therefore do I have to manually extract the
content between "begin 666" and the "end" rather than using some
javamail inbuilt function.
Thank you very much
Barry


I am using javamail 1.3.1 and

D:\test>java -version
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top