File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java mail attachmetnt include txt extensions

 
Dorj Galaa
Ranch Hand
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I wrote this code to read mail from address, subject, send date, and mail message. If mail attached file with txt extension this programm display content of file. if attached other file with another extension can't read file content. Why can read content of text file
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;
import java.io.*;
import com.sun.mail.imap.*;
public class Msgread
{
static String host="host";
static String user="username";
static String password="password";
static String protocol="pop3";
static boolean debug=false;
static int port=110;
static String inbox="INBOX";
static boolean deletemessage=true;

public static void extractEnvelope(Message p)
{
try
{
Address a[];
a=p.getFrom();
System.out.print("FROM: ");
for (int j=0;j<a.length;j++)
{
//Display From address
System.out.println(a[j].toString());
}
System.out.print("SUBJECT: ");
//Display Subject
String Subject=p.getSubject();
if (Subject.equals("")) System.out.println("NoSubject"); else System.out.println(Subject);
}
catch(MessagingException e)
{
}
}
public static void extractPart(Part p)
{
try
{
if (p instanceof Message) extractEnvelope((Message)p);

String FileName=p.getFileName();
if (FileName!=null) System.out.println("Attached FileName="+FileName);
if (p.isMimeType("text/plain"))
{
System.out.println(p.getContentType());
System.out.println("MESSAGE IS");
System.out.println((String)p.getContent());
}
else
{
if (p.isMimeType("multipart/*"))
{
Multipart multipart=(Multipart)p.getContent();
//System.out.println(multipart.getContentType());
//System.out.println("END");
int count=multipart.getCount();
for (int j=0;j<count;j++)
{
Part part=multipart.getBodyPart(j);
extractPart(part);
}
}
else
{
if (p.isMimeType("message/rfc822"))
{
System.out.println("Message IS:");
extractPart((Part)p.getContent());
}
}
}
}
catch(MessagingException e)
{
}
catch(IOException e)
{
}
}

public static void main(String args[])
{
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);
//store.connect(host,port,args[0],args[1]);
Folder folder=store.getDefaultFolder();
folder=folder.getFolder(inbox);
folder.open(Folder.READ_ONLY);
int messagecount=0;
messagecount=folder.getMessageCount();
if (messagecount>0)
{
Message[] msgs=folder.getMessages();
//Display message count
//System.out.println(msgs.length);
for (int i=0;i<msgs.length;i++)
{
extractPart(msgs[i]);
}
}
else
{
//FetchProfile fp = new FetchProfile();
//fp.add(FetchProfile.Item.ENVELOPE);
//fp.add(FetchProfile.Item.FLAGS);
//fp.add("X-Mailer");
//folder.fetch(msgs, fp);
//
//for (int i = 0; i < msgs.length; i++)
//{
// System.out.println("--------------------------");
// System.out.println("MESSAGE #" + (i + 1) + ":");
// extractEnvelope(msgs[i]);
//}
System.out.println("No more messages in INBOX");
}
folder.close(deletemessage);
store.close();
//System.out.println(messagecount);
}
catch(AuthenticationFailedException e)
{
System.out.println(e.getMessage());
}
catch(NoSuchProviderException e)
{
System.out.println(e.getMessage());
}
catch(MessagingException e)
{
System.out.println(e.getMessage());
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic