dcsimg
unable to parse all mime-types/content-types in javamail
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   amit_wadhwa
Posted On:   Thursday, February 6, 2003 08:55 PM

i have made a servlet based email system, integrated with database, parsing 95-99% of emails correctly with attachments, but every once in a while i get a mimemessage@227362dsf32 something as the body of the message. here is my code used for fetching mail for each user based on member passwords from a database, looping through each message, and inserting into the database. where am i going wrong? how do i handle all content types? im not really sure on which email types my application does not handle, cos it deletes all emails from the server after fetching the email and (un)successfully inserting into database.    More>>

i have made a servlet based email system,

integrated with database, parsing 95-99% of emails

correctly with attachments, but every once in a while i get

a mimemessage@227362dsf32 something as the body of the

message. here is my code used for fetching mail for each

user based on member passwords from a database, looping

through each message, and inserting into the database.

where am i going wrong? how do i handle all content types?

im not really sure on which email types my application does

not handle, cos it deletes all emails from the server after

fetching the email and (un)successfully inserting into

database.


here is the complete code:




			
				


import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.sun.mail.util.*;


public class NewMailTimer extends HttpServlet
{
public static void main(String args[]) throws Exception
{
SiLib silicone = new SiLib();
Hashtable SiVar = new Hashtable();
Connection con = silicone.getDBConnection();
SiVar.put("con", con);
new NewMailTimer().getAllIds(SiVar);
}

protected boolean getAllIds(Hashtable SiVar) throws Exception
{
Connection con = (Connection)SiVar.get("con");
String sql = "select memid from simember where isactive=1 order by memid";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
System.out.println("-----------------------------------------");
System.out.println(new java.util.Date().toString());
System.out.println("-----------------------------------------");

while(rs.next())
{
SiVar.put("MemId", rs.getString("memid"));
try
{
fetchMail(SiVar);
System.out.println("checked mail for: " + SiVar.get("MemId"));
}
catch (AuthenticationFailedException ignore)
{
System.out.println(SiVar.get("MemId") + ": Invalid Username/Password");
}
catch (Exception ignore)
{
if (ignore.toString().indexOf("Connect failed")!=-1)
{
ignore.printStackTrace(System.out);
System.out.println(SiVar.get("MemId") + ": ( Invalid User/Pw ) / server conn fail");
}
else
{
System.out.println("error checking mail for: " + SiVar.get("MemId") + " ");
ignore.printStackTrace(System.out);
}
}
}
return true;
}

protected String[] getDetails(Hashtable SiVar) throws Exception
{
SiLib silicone = new SiLib();
PrintWriter client = (PrintWriter)SiVar.get("client");
Connection con = (Connection)SiVar.get("con");
Statement stmt = con.createStatement();
ResultSet rs = null;
ResultSet rs2 = null;
String[] details = new String[4];
rs = stmt.executeQuery("select emaillogin,emailpwd from simember where memid = '" + SiVar.get("MemId") + "'");

if (rs.next())
{
details[0] = ""+rs.getString("emaillogin");
details[1] = ""+rs.getString("emailpwd");
}

rs2 = stmt.executeQuery("select inmailsrvip, inmailsrvport from sisetting");
if (rs2.next())
{
details[2] = ""+rs2.getString("inmailsrvip");
details[3] = ""+rs2.getString("inmailsrvport");
}
return details;
}


protected void fetchMail(Hashtable SiVar) throws SQLException, Exception
{
Connection con = (Connection)SiVar.get("con");
Statement stmt = con.createStatement();
SiLib silicone = new SiLib();
PreparedStatement prstm = con.prepareStatement("insert into siemail2 (mailidxid, memid, maildate, mailisopen, mailto, mailfr, mailcc, mailbcc, mailsub, mailbody, mailtotalsize, mailatts, mailfldr, hasattach) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?) ");
Properties props = new Properties();
Store store = null;
Folder folder = null;
String subject = "";
String date = "";
String mailFrom = "";
String mailTo = "";
String mailCC = "";
String mailBCC = "";
String[] details = getDetails(SiVar);
String newId = "";
String user = details[0];
String password = details[1];
String ip = details[2];
String port = details[3];
String fileDir = new SiConfLib().getEmlFilePath();
File f = new File(fileDir);
f.mkdirs();
int size = 0;
boolean delete = true;


props.put("mail.pop3.port", port);
Session session = Session.getInstance(props, null);
store = session.getStore("pop3");
store.connect(ip, user , password);

folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);

Message message[] = folder.getMessages();
int msgcount = 0;
try
{
msgcount = message.length;
}
catch (Exception e){}

for (int i=0; i < message.length; i++)
{
Address to[] = null;
Address from[] = null;
Address cc[] = null;
Address bcc[] = null;
Object content = null;
newId = getId(SiVar, "lastmailidxid");
// message[i].writeTo(new FileOutputStream(fileDir+"/"+SiVar.get("MemId")+"_"+newId+".eml"));
SiVar.put("newId", newId);
subject = message[i].getSubject();
size = message[i].getSize();
date = ""+message[i].getSentDate();
try
{
to = message[i].getRecipients(Message.RecipientType.TO);
}
catch (Exception ignored){}
try
{
from = message[i].getFrom();
mailFrom = from[0].toString();
}
catch (Exception ignored){}
try
{
cc = message[i].getRecipients(Message.RecipientType.CC);
}
catch (Exception ignored){}
try
{
bcc = message[i].getRecipients(Message.RecipientType.BCC);
}
catch (Exception ignored){}

mailTo = join(to,", ");
mailCC = join(cc,", ");
mailBCC = join(bcc,", ");
try
{
content = message[i].getContent();
}
catch (Exception ignored){}
try
{
prstm.setString(1, newId);
prstm.setString(2, silicone.getvalue(SiVar, "MemId"));
prstm.setString(3, date);
prstm.setInt(4, 1); // mailisopen = 0 if mail is read, 1 if mail is not read
prstm.setString(5, mailTo);
prstm.setString(6, mailFrom);
prstm.setString(7, mailCC);
prstm.setString(8, mailBCC);
prstm.setString(9, "-"+subject);
prstm.setString(10, "");
prstm.setInt(11, size);
prstm.setString(12, "");
prstm.setString(13, "Inbox");
prstm.setString(14, "n");
prstm.executeUpdate();
}
catch (Exception e)
{
e.printStackTrace(System.out);
System.out.println("mail from: " + mailFrom);
System.out.println("Database Message: "+e.getMessage());
delete = false;
}
try
{
if(message[i].isMimeType("multipart/*"))
{
try
{
Multipart mp=(Multipart)message[i].getContent();
partlist=new ArrayList(mp.getCount());
buildPartInfoList(partlist,mp, SiVar);
}
catch (Exception e)
{
try
{
message[i].writeTo(new FileOutputStream(fileDir+"/"+SiVar.get("MemId")+"_"+newId+".eml"));
}
catch (Exception ignored){}
}
}
else
{
try
{
Part p = (Part)(message[i]);
handlePart(p, SiVar);
}
catch (Exception e)
{
try
{
message[i].writeTo(new FileOutputStream(fileDir+"/"+SiVar.get("MemId")+"_"+newId+".eml"));
}
catch (Exception ignored){}
}
}
}
catch (Exception e)
{
delete = false;
stmt.executeUpdate("delete from siemail2 where mailidxid='"+newId+"'");
e.printStackTrace(System.out);
System.out.println("Multipart Message: "+e.getMessage());
}
finally
{
delete=true;
if (delete) message[i].setFlag(Flags.Flag.DELETED, true);
}
}
try
{
folder.close(true);
store.close();
}
catch (Exception ignored){}
}


private void buildPartInfoList(List partlist, Multipart mp, Hashtable SiVar) throws Exception
{
for (int i=0; i
{
Part part=mp.getBodyPart(i);
if(part.isMimeType("multipart/*"))
{
buildPartInfoList(partlist,(Multipart)part.getContent(), SiVar);
}
else
{
partlist.add(part);
}
}
for (int i = 0;i
{
handlePart((Part)partlist.get(i), SiVar);
}
}

private void handlePart(Part part, Hashtable SiVar) throws MessagingException, IOException, SQLException
{
try
{
Connection con = (Connection)SiVar.get("con");
Statement stmt = con.createStatement();
SiLib silicone = new SiLib();
String disposition = part.getDisposition();
String contentType = part.getContentType();
PrintWriter client = (PrintWriter)SiVar.get("client");
String name = "";
// System.out.println("Part disposition: " + disposition);
// System.out.println("Part contentType: " + contentType);

if (disposition == null)
{
if (part instanceof InputStream)
{
name = "eml"+ getId(SiVar, "lastmailidxid")+"_"+encodeFileName(part.getFileName());
saveFile(name, part.getInputStream(), SiVar);
stmt.executeUpdate("update siemail2 set mailatts=concat(mailatts, '"+name+"|'), hasattach='y' where mailidxid=\'"+silicone.getvalue(SiVar, "newId")+"\'");
}
/* else if (part.getContent().toString().indexOf(" ")!=-1)
{
if (part.getContent() instanceof String)
{
stmt.executeUpdate("update siemail2 set mailbody= \'"+"
"+silicone.dbFilter(part.getContent()+"")+"
\' where mailidxid=\'"+silicone.getvalue(SiVar, "newId")+"\'");
}
}
*/ else
{
if (part.getContent() instanceof String)
{
stmt.executeUpdate("update siemail2 set mailbody=\'"+""+silicone.dbFilter(part.getContent()+"")+"\' where mailidxid=\'"+silicone.getvalue(SiVar, "newId")+"\'");
}
}
}

else if (disposition.equalsIgnoreCase(Part.ATTACHMENT))
{
try
{
name = "eml"+getId(SiVar, "lastmailidxid")+"_"+encodeFileName(part.getFileName());
saveFile(name, part.getInputStream(), SiVar);
stmt.executeUpdate("update siemail2 set mailatts=concat(mailatts, '"+name+"|'), hasattach='y' where mailidxid=\'"+silicone.getvalue(SiVar, "newId")+"\'");
}
catch (Exception e)
{
System.out.println("Attachment Message: "+e.getMessage());
}
}
else if (disposition.equalsIgnoreCase(Part.INLINE))
{
try
{
stmt.executeUpdate("update siemail2 set mailbody=\'"+""+silicone.dbFilter(part.getContent()+"")+"\' where mailidxid=\'"+silicone.getvalue(SiVar, "newId")+"\'");
}
catch (Exception e)
{
e.printStackTrace(System.out);
System.out.println("Inline Message: "+e.getMessage());
}
}
else{}
}
catch (MessagingException me){System.out.print(" Messaging.." + me.getMessage());}
catch (IOException ioe){System.out.print(" FileWriting.." + ioe.getMessage());}
catch (SQLException sqle){System.out.print(" SQLe.." + sqle.getMessage());}
}


private void saveFile(String filename, InputStream input, Hashtable SiVar) throws IOException
{
HttpServletRequest req = (HttpServletRequest)SiVar.get("req");
PrintWriter client = (PrintWriter)SiVar.get("client");
try
{
if ((filename == null)||((filename+"")).equals("null"))
{
// filename = File.createTempFile("attach", ".out" , new File(new SiConfLib().getEmailUploadPath()+"mail"+"/" + SiVar.get("MemId")+"")+"/"+ViEncoder.encode("inbox")+"/")).getName();
}
String fileDir = new SiConfLib().getInboxPath();
File f = new File(fileDir);
f.mkdirs();
File file = new File(f,filename);
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
BufferedInputStream bis = new BufferedInputStream(input);
int aByte;
while ((aByte = bis.read()) != -1) {
bos.write(aByte);
}
bos.flush();
bos.close();
bis.close();
}
catch (Exception e)
{
e.printStackTrace(System.out);
System.out.println("Save File Message: "+e.getMessage());
}
}

private String encodeFileName(String fileName)
{
char c = 'a';
char content[] = new char[fileName.length()];
fileName.getChars(0, fileName.length(), content, 0);
StringBuffer result = new StringBuffer(content.length + 50);
for (int i = 0; i < content.length; i++)
{
c = content[i];
if (((c <='z')&&(c>='a'))||((c>='A')&&(c <='Z'))||((c>='0')&&(c <='9'))||(c=='.')){}
else c = '_';
result.append(c);
}
return result.toString();
}

private String getId(Hashtable SiVar, String IdOfWhat) throws SQLException
{
SiLib silicone = new SiLib();
String result = "";
Connection con = (Connection)SiVar.get("con");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select max("+IdOfWhat+") from siidxid");
int maxId = rs.getInt("max("+IdOfWhat+")");
int newId = maxId + 1;
stmt.executeQuery("update siidxid set " + IdOfWhat + " = " + newId + "");
return ""+newId;
}

private String join(Address[] addresses, String JoinString)
{
String ReturnStr = "";
int i=0;

JoinString = (JoinString == null)?",":((JoinString.equals(""))?",":JoinString);

if(addresses != null)
{ for(i = 0;i < addresses.length;i++)
{

ReturnStr = (i == (addresses.length-1))?(ReturnStr + addresses[i]):(ReturnStr + addresses[i] + JoinString);
}
}
return (ReturnStr);
}

int level = 0;
List partlist = null;
}
						
							




where am i going wrong? and which content types am i

ignoring?? i cannot ask my clients clients to send mails

only in ascii/unicode format. also, i cannot 'not-delete'

the mails from his server for testing cos this is the only

application they are using for email. and the email table

has some 65000 records in it in the past 2 months most of

which is business critical email. so my only go is to try

and add more content types/dispositions/text formats etc,

to my existing code. someone please help asap.
   <<Less

Re: unable to parse all mime-types/content-types in javamail

Posted By:   Christopher_Koenigsberg  
Posted On:   Monday, February 10, 2003 06:55 AM

...im not really sure on which email types my application does
not handle, cos it deletes all emails from the server after
fetching the email and (un)successfully inserting into
database...i cannot 'not-delete'
the mails from his server for testing cos this is the only
application they are using for email...



Wow, I am sure glad I don't have to use your mail application!

About | Sitemap | Contact