This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java mail attachmetnt include txt extensions

 
Dorj Galaa
Ranch Hand
Posts: 113
  • 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());
}
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic