Java coding is definitely more complicated than I first thought.
I have "borrowed" the source code for a mail application with the intention of modifying it to interact with my flash GUI and i have managed to get the mail app to work... to a degree. unfortunately i have come acropper with the code because although it appears to work there are some connection issues between my computer and the mail server.
here is my mailreader
package mailproject;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
import net.sf.classifier4J.*;
import net.sf.classifier4J.bayesian.*;
import net.sf.classifier4J.summariser.*;
public class MailReader
{
public static void main(String[] args)
{
try
{
String popServer="pop.gmail.com";
String popUser="ss.tybs";
String popPassword="xenophobe1";
GetMail(popServer, popUser, popPassword);
}
catch (Exception e)
{
e.printStackTrace();
}
System.exit(0);
}
public static void GetMail(String popServer, String popUser, String popPassword)
{
Store store=null;
Folder folder=null;
String strEmail = "";
double dSpamScore = 0.0;
try
{
Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);
store = session.getStore("pop3");
store.connect(popServer, popUser, popPassword);
folder = store.getDefaultFolder();
if (folder == null) throw new Exception("No default folder");
folder = folder.getFolder("INBOX");
if (folder == null) throw new Exception("No POP3 INBOX");
folder.open(Folder.READ_ONLY);
Message[] msgs = folder.getMessages();
for (int nMsg = 0; nMsg < stremail =" buildMessage(msgs[nMsg]);" strsumm =" getSummary(strEmail,3);">m.out.println(strSumm); dSpamScore = checkSpam(strEmail); //dSpamScore = checkSpamWithBayes(strEmail); if(dSpamScore > 0.7)
{
System.out.println("--------------------------");
System.out.println("SPAM DETECTED:");
System.out.println("--------------------------");
System.out.println(strEmail);
System.out.println("--------------------------");
}}}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (folder!=null) folder.close(false);
if (store!=null) store.close();
}
catch (Exception e2)
{
e2.printStackTrace();
}}}
public static String buildMessage(Message message)
{
String strReturn = "";
try
{
String from=((InternetAddress)message.getFrom()[0]).getPersonal();
if (from==null) from=((InternetAddress)message.getFrom()[0]).getAddress();
strReturn += "FROM: " + from;
String subject=message.getSubject();
strReturn += "SUBJECT: "+subject;
Part messagePart=message;
Object content=messagePart.getContent();
if (content instanceof Multipart)
{
messagePart=((Multipart)content).getBodyPart(0);
strReturn += "[ Multipart Message ]";
}
String contentType=messagePart.getContentType();
strReturn += "CONTENT:"+contentType;
if (contentType.startsWith("text/plain")|| contentType.startsWith("text/html"))
{
InputStream is = messagePart.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(is));
String thisLine=reader.readLine();
while (thisLine!=null)
{
strReturn +=thisLine;
thisLine=reader.readLine();
}}}
catch (Exception ex)
{
ex.printStackTrace();
}
return strReturn;
}
public static double checkSpam(String strEmailBody)
{
double dClassification = 0.0;
try
{
SimpleClassifier classifier = new SimpleClassifier();
classifier.setSearchWord( "belgium" );
dClassification = classifier.classify(strEmailBody);
}
catch(Exception e)
{
e.printStackTrace();
}
return dClassification;
}
public static double checkSpamWithBayes(String strEmailBody)
{
double dReturn = 0.0;
try
{
IWordsDataSource wds = new SimpleWordsDataSource();
wds.addMatch("Belgium");
wds.addMatch("Vogon");
wds.addMatch("Devx");
IClassifier classifier = new BayesianClassifier(wds);
dReturn = classifier.classify(strEmailBody);
}
catch(Exception e)
{
e.printStackTrace();
}
return dReturn;
}
public static String getSummary(String strEmailBody, int nSentences)
{
ISummariser summ = new SimpleSummariser();
String strSumm = summ.summarise(strEmailBody,nSentences);
return strSumm;
}}
and here is what happens when i run it:
annoying to say the least!
Another issue i can't fathom to work around is jdeveloper does not work on my vista laptop! Upon which flash is installed and any hope of me combining the two parts of the organism. :(
Thursday, 6 November 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment