GeeCON Prague 2014*
The moose likes Other JSE/JEE APIs and the fly likes problem reading the mail Attachment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "problem reading the mail Attachment" Watch "problem reading the mail Attachment" New topic
Author

problem reading the mail Attachment

Sandeep Jain
Ranch Hand

Joined: Oct 25, 2000
Posts: 124
Hey guys ,
I am using javas mail package. and trying to reading and send mails . The problem that I am facing is with attachments.
1) I am unable to read the attachment file name.
it always gives me null however I can read the attachment
2)After reading the attachment when i try to open it , it opens it with all junk charecters in it.

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class msgshow1
{
static String protocol="pop3";
static String host = "bbbbb";
static String user = "hhhhh";
static String password = "kkkk";
static String mbox = "INBOX";
static String url = null;
static int port = -1;
static boolean verbose = false;
static boolean debug = false;
static boolean showStructure = false;
static String indentStr = " ";
static int level = 0;
public static void main(String argv[])
{
int msgnum = 1;
try
{

Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
Store store = null;
store = session.getStore(protocol);
store.connect(host, port, user, password);
Folder folder = store.getDefaultFolder();
if (folder == null)
{
System.out.println("No default folder");
System.exit(1);
}
folder = folder.getFolder(mbox);
if (folder == null)
{
System.out.println("Invalid folder");
System.exit(1);
}
try
{
folder.open(Folder.READ_WRITE);
}
catch (MessagingException ex)
{
folder.open(Folder.READ_ONLY);
}
int totalMessages = folder.getMessageCount();
if (totalMessages == 0)
{
System.out.println("Empty folder");
folder.close(false);
store.close();
System.exit(1);
}
System.out.println("Getting message number: " + msgnum);
Message m = null;
try
{
m = folder.getMessage(msgnum);
dumpPart(m);
}
catch (IndexOutOfBoundsException iex)
{
System.out.println("Message number out of range");
}
folder.close(false);
store.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
System.exit(1);
}
public static void dumpPart(Part p) throws Exception
{

if (p instanceof Message)
dumpEnvelope((Message)p);
/** Dump input stream ..
InputStream is = p.getInputStream();
// If "is" is not already buffered, wrap a BufferedInputStream
// around it.
if (!(is instanceof BufferedInputStream))
is = new BufferedInputStream(is);
int c;
while ((c = is.read()) != -1)
System.out.write(c);
**/
String disp = p.getDisposition();
System.out.println("disp="+disp);
pr("CONTENT-TYPE: " + p.getContentType());
/*
* Using isMimeType to determine the content type avoids
* fetching the actual content data until we need it.
*/
if (p.isMimeType("text/plain"))
{
pr("This is plain text");
pr("---------------------------");
if (!showStructure)
System.out.println((String)p.getContent());
}
else if (p.isMimeType("multipart/*"))
{
pr("This is a Multipart");
pr("---------------------------");
Multipart mp = (Multipart)p.getContent();
level++;
int count = mp.getCount();
for (int i = 0; i < count; i++)
dumpPart(mp.getBodyPart(i));
level--;
}
else if (p.isMimeType("message/rfc822"))
{
pr("This is a Nested Message");
pr("---------------------------");
level++;
dumpPart((Part)p.getContent());
level--;
}
else if (!showStructure)
{
Object o = p.getContent();
if (o instanceof String)
{
pr("This is a string");
pr("---------------------------");
System.out.println((String)o);
}
else if (o instanceof InputStream)
{
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"+p.getFileName());
pr("This is just an input stream");
pr("---------------------------");
InputStream is = (InputStream)o;
//System.out.println("Filename = "+p.getFileName());
FileOutputStream outputStream = new FileOutputStream ("c:/doc1.doc");
/*System.out.println("The Total number Of bytes available ="+is.available());
byte b[]=new byte[is.available()];
is.read(b,0,is.available());
FileOutputStream outputStream = new FileOutputStream ("c:/doc1.doc");
outputStream.write(b);
outputStream.close();*/



int c;
while ((c = is.read()) != -1)
{
outputStream.write(c);
outputStream.flush();
System.out.write(c);
}
outputStream.close();
}
else
{
pr("This is an unknown type");
pr("---------------------------");
pr(o.toString());
}
}
else
{
pr("This is an unknown type");
pr("---------------------------");
}
}
public static void dumpEnvelope(Message m) throws Exception
{
pr("This is the message envelope");
pr("---------------------------");
Address[] a;
// FROM
if ((a = m.getFrom()) != null)
{
for (int j = 0; j < a.length; j++)
pr("FROM: " + a[j].toString());
}
//TO
if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
for (int j = 0; j < a.length; j++)
pr("TO: " + a[j].toString());
}
pr("SUBJECT: " + m.getSubject());
Date d = m.getSentDate();
pr("SendDate: " +(d != null ? d.toString() : "UNKNOWN"));
Flags flags = m.getFlags();
StringBuffer sb = new StringBuffer();
Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags
boolean first = true;
for (int i = 0; i < sf.length; i++)
{
String s;
Flags.Flag f = sf[i];
if (f == Flags.Flag.ANSWERED)
s = "\\Answered";
else if (f == Flags.Flag.DELETED)
s = "\\Deleted";
else if (f == Flags.Flag.DRAFT)
s = "\\Draft";
else if (f == Flags.Flag.FLAGGED)
s = "\\Flagged";
else if (f == Flags.Flag.RECENT)
s = "\\Recent";
else if (f == Flags.Flag.SEEN)
s = "\\Seen";
else
continue; // skip it
if (first)
first = false;
else
sb.append(' ');
sb.append(s);
}
String[] uf = flags.getUserFlags(); // get user-flag strings
for (int i = 0; i < uf.length; i++)
{
if (first)
first = false;
else
sb.append(' ');
sb.append(uf[i]);
}
pr("FLAGS: " + sb.toString());
String[] hdrs = m.getHeader("X-Mailer");
if (hdrs != null)
pr("X-Mailer: " + hdrs[0]);
else
pr("X-Mailer NOT available");
}
public static void pr(String s)
{
if (showStructure)
System.out.print(indentStr.substring(0, level * 2));
System.out.println(s);
}
}


Try and Try Till u succeed<br /> <br />Sandeep Jain
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Sandeep,
I tried your code on my mailbox and it worked fine, i.e. I could read the attachement filename, and the contents was all right.
Concerning the second problem, it might actually not be a problem. If your attachement is a binary file, it is absolutely normal to see weird characters in it.
The file should be stored on disk and read with a editor that knows how to deal with the file format.
Cheers
 
 
subject: problem reading the mail Attachment