aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes Java mail attachmetnt include txt extensions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Java mail attachmetnt include txt extensions" Watch "Java mail attachmetnt include txt extensions" New topic
Author

Java mail attachmetnt include txt extensions

Dorj Galaa
Ranch Hand

Joined: May 29, 2001
Posts: 113
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());
}
}
}


Senior software engineer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java mail attachmetnt include txt extensions
 
Similar Threads
problem reading the mail Attachment
How to properly read the custom Header value in the E-mail envolope
JavaMail: Help with sending e-mails
Display Word attachments
javax.mail.MessagingException: Connection dropped by server?; Error