int messageID = 0;
try
{
Address from[] = message.getFrom();
// get the message's date and time
long messageTime = (message.getSentDate()).getTime();
java.sql.Timestamp messageDate = (java.sql.Timestamp)new java.sql.Timestamp(messageTime);
cvdl.setSqlQuery("INSERT INTO emailmessage(MessageDate, MailFrom, Subject, Body, accountID , Priority, Headers, owner,Size) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
String mesgFrom = from[0].toString();
mesgFrom = mesgFrom.replaceAll("\""," ");
mesgFrom = mesgFrom.replaceAll("'"," ");
mesgFrom = mesgFrom.trim();
String[] DateHeader = message.getHeader("Date");
Date receiveDate = new Date(DateHeader[0]);
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
String receivedDate = formatter.format(receiveDate);
if (receivedDate != null && !(receivedDate.equals("")))
{
cvdl.setString(1, receivedDate);
}else{
cvdl.setString(1, "CONCAT(CURRENT_DATE, CURRENT_TIME)");
}
cvdl.setString(2, mesgFrom);
cvdl.setString(3, message.getSubject());
String MsgBody = null;
StringBuffer msgBody = new StringBuffer();
Object o = message.getContent();
if (o instanceof String)
{
msgBody.append((String) o);
}else if(o instanceof Multipart){
Multipart mp = (Multipart)o;
int countMultipart = mp.getCount();
for (int j = 0; j < countMultipart; j++)
{
// Part are numbered starting at 0
BodyPart b = mp.getBodyPart(j);
String mimeType2 = b.getContentType();
String disposition2 = b.getDisposition();
Object o2 = b.getContent();
if (o2 instanceof String)
{
if (mimeType2 != null &&
(mimeType2.toLowerCase().indexOf("text/plain") != -1 || mimeType2.toLowerCase().indexOf("text/html") != -1) &&
(disposition2 == null ||(disposition2!=null && !disposition2.equals(Part.ATTACHMENT))))
{
if (mimeType2.toLowerCase().indexOf("text/html")>=0)
{
// HTML message body
msgBody.append(b.getContent().toString());
}else{
// plain text part
msgBody.append(b.getContent().toString());
}
if (! msgBody.toString().equals(""))
{
break;
}
}
}else if(o2 instanceof Multipart){
//"**This BodyPart is a nested Multipart. ");
Multipart mp2 = (Multipart)o2;
int countMultipart1 = mp2.getCount();
for (int k = 0; k < countMultipart1; k++)
{
// Part are numbered starting at 0
BodyPart b2 = mp2.getBodyPart(k);
String mimeType3 = b2.getContentType();
String disposition3 = b2.getDisposition();
Object o3 = b2.getContent();
if (o3 instanceof String)
{
if (mimeType3 != null &&
(mimeType3.toLowerCase().indexOf("text/plain") != -1 || mimeType3.toLowerCase().indexOf("text/html") != -1) &&
(disposition3==null ||( disposition3!=null && !disposition3.equals(Part.ATTACHMENT))))
{
if (mimeType3.toLowerCase().indexOf("text/html") >= 0)
{
// HTML message body
msgBody.append(b2.getContent().toString());
}else{
// plain text part
msgBody.append(b2.getContent().toString());
}
}
if (! msgBody.toString().equals(""))
{
break;
}
} // end if (o3 instanceof String)
} // end for (int k = 0; k < countMultipart1; k++)
} // end if(o2 instanceof Multipart)
} // end for (int j = 0; j < countMultipart; j++)
} // end if (o instanceof String)
MsgBody = msgBody.toString();
if (MsgBody != null)
{
cvdl.setString(4,MsgBody);
}else{
cvdl.setString(4,"");
}
cvdl.setInt(5, accountID);
// set priority field based on message X-Priority header.
// if the X-Priority header is not set, or is unknown, then
// set our database priority field to medium.
if (mimeMessage.getHeader("X-Priority", "") != null)
{
if ((mimeMessage.getHeader("X-Priority", "")).equals("1"))
{
cvdl.setString(6, "HIGH");
}else if ((mimeMessage.getHeader("X-Priority", "")).equals("5")){
cvdl.setString(6, "LOW");
}else{
cvdl.setString(6, "MEDIUM");
}
}else{
cvdl.setString(6, "MEDIUM");
}
// added for taking the header also
Enumeration em = mimeMessage.getAllHeaderLines();
StringBuffer ah = new StringBuffer();
// loop through all the headers
while (em.hasMoreElements())
{
String ch = (String)em.nextElement();
ah.append(ch);
} // end while (em.hasMoreElements())
if (ah.length() > 0)
{
cvdl.setString(7, ah.toString());
}else{
cvdl.setString(7, null);
}
cvdl.setInt(8, userID);
long messageSize = mimeMessage.getSize();
if (messageSize > 1024)
{
messageSize = messageSize / 1024;
}else{
messageSize = 1;
}
cvdl.setString(9, messageSize+"");
cvdl.executeUpdate();
messageID = cvdl.getAutoGeneratedKey();
cvdl.clearParameters();
if (messageID != 0)
{
Address arrayTo[] = message.getRecipients(Message.RecipientType.TO);
Address arrayCc[] = message.getRecipients(Message.RecipientType.CC);
Address arrayBcc[] = message.getRecipients(Message.RecipientType.BCC);
if ((arrayTo != null) && (arrayTo.length != 0))
{
for (int i = 0; i < arrayTo.length; i++)
{