email.java
来自「jetspeed源代码」· Java 代码 · 共 1,221 行 · 第 1/3 页
JAVA
1,221 行
}
public String getAttachmentname(Message message) throws Exception
{
Object obj = message.getContent();
Multipart mpart = (Multipart) obj;
String name = null;
for (int i = 0, n = mpart.getCount(); i < n; i++)
{
Part part = mpart.getBodyPart(i);
if (part.getFileName() != null) name = part.getFileName();
}
return name;
}
public Vector getAttachments(Message message) throws Exception
{
Object obj = message.getContent();
Multipart mpart = (Multipart) obj;
Vector vAtt = new Vector();
String name = null;
String id = null;
for (int i = 0, n = mpart.getCount(); i < n; i++)
{
Part part = mpart.getBodyPart(i);
name = part.getFileName();
if (part.getFileName() != null) vAtt.add(part.getFileName());
//attachmentIDs(message);
}
return vAtt;
}
public boolean checkAttachment(Message message) throws Exception
{
boolean hasattachment = false;
try
{
if ((message.isMimeType("text/plain"))
|| ((message.isMimeType("text/html"))))
{
hasattachment = false;
}
else
{ //multipart
Object obj = message.getContent();
Multipart mpart = (Multipart) obj;
for (int i = 0, n = mpart.getCount(); i < n; i++)
{
Part part = mpart.getBodyPart(i);
//get attachment
String disposition = part.getDisposition();
if ((disposition != null)
&& ((disposition.equalsIgnoreCase(Part.ATTACHMENT)) || (disposition
.equals(Part.INLINE))))
{
hasattachment = true;
} else if (disposition == null)
{
if (part.getContent() instanceof MimeMultipart)
{
hasattachment = true;
}
} else
hasattachment = false;
}
return hasattachment;
}
} catch (Exception e)
{
log.error("Error in checkAttachment",e);
}
return false;
}
//check if it's a new message
public int checkNewmessage(Message message) throws Exception
{
log.info("### check new message in Email");
//get message Id to know if it's a new message
String msgeId = getMessageId(message);
Criteria cr = new Criteria();
cr.add(EmailInboxPeer.MESSAGE_ID, msgeId);
// EmailInbox email =
// (EmailInbox)EmailInboxPeer.doSelect(cr).elementAt(0);
//Vector vMsge = EmailInboxPeer.doSelect(cr);
List vMsge = EmailInboxPeer.doSelect(cr);//tdk2.2 version
if (vMsge.isEmpty())
{ // message id not found in db
return 1; // new message
} else
{ // message found in db, flag is set to 0
//EmailInbox email =
// (EmailInbox)EmailInboxPeer.doSelect(cr).elementAt(0);
EmailInbox email = (EmailInbox) EmailInboxPeer.doSelect(cr).get(0);//tdk2.2
// version
if (email.getReadflag() == 0)
{
return 1; // unread message
} else
return 0;
}
}
/*-----------------------------------------------------------------------------------------*
* Retrieves message id from message : RJPY
*-----------------------------------------------------------------------------------------*/
public String getMessageId(Message message)
{
String messageid = "";
try
{
// Retrieve message id from headers
Enumeration e = message.getAllHeaders();
while (messageid.equals(""))
{
Header header = (Header) e.nextElement();
if (header.getName().equals("Message-ID"))
{
messageid = header.getValue();
}
}
} catch (Exception e)
{
log.error("Error in getMessageId()",e);
}
return messageid;
// end Retrieve
}
public String convertMessage(String msg) throws Exception
{
log.info("convert message");
StringBuffer sb = new StringBuffer();
for (int i = 0; i < msg.length(); i++)
{
char c = msg.charAt(i);
if (c == '\n')
{
log.info("new line");
sb.append("<br>");
} else
{
sb.append(c);
}
}
String returnString = sb.toString();
return returnString;
}
public void doCreatefolder(String folder_name) throws Exception
{
Folder dfolder = store.getDefaultFolder();
//Folder dfolder = store.getFolder("myfolder");
IMAPFolder newfolder = (IMAPFolder) dfolder.getFolder(folder_name);
if (newfolder == null)
{
//if the target folder not exist just create it
//note:when you create a subfolder you can assign its
// attribute.Details,please read the javamail document.
}
if (!newfolder.exists())
{
//newfolder.create(Folder.HOLDS_FOLDERS);
newfolder.create(Folder.HOLDS_MESSAGES);
}
//allFolders();
}
public Vector allFolders()
{
Vector vFolders = new Vector();
try
{
Folder allfolders = store.getDefaultFolder();
//Folder allfolders = store.getFolder("myfolder");
Folder[] f = allfolders.list();
for (int i = 0; i < f.length; i++)
{
//String fn = f[i].getFullName();
String fn = f[i].getName();
vFolders.add(fn);
}
//data.getUser().setTemp("vFolders",vFolders);
// data.getSession().setAttribute("vFolders",vFolders);
return vFolders;
} catch (Exception e)
{
log.error("Error in allFolders()",e);
return null;
}
}
public void moveMessage(String fromFolder, String toFolder,
String[] checkboxes)
{
try
{
//get folder destination
//Folder to_folder = store.getFolder("myfolder/"+toFolder);
Folder to_folder = store.getFolder(toFolder);
Folder from_folder = store.getFolder(fromFolder);
from_folder.open(Folder.READ_WRITE);
int tempStart = checkboxes.length;
int startvalue = Integer.parseInt(checkboxes[tempStart - 1]);
int start = startvalue + 1;
int tempEnd = Integer.parseInt(checkboxes[0]);
int end = tempEnd + 1;
to_folder.open(Folder.READ_WRITE);
// get the message
Message msge[] = from_folder.getMessages(start, end);
from_folder.copyMessages(msge, to_folder);
from_folder.close(true);
//delete the message from inbox folder
String protocol = (String) parameters.get("protocol");
checkboxDelete(fromFolder, checkboxes, protocol);
} catch (Exception e)
{
log.error("Error in moveMessage()",e);
}
}
public void moveMessage(String fromFolder, String toFolder,
int current_index)
{
try
{
//get folder destination
//Folder dfolder = store.getFolder("myfolder/"+toFolder);
Folder dfolder = store.getFolder(toFolder);
Folder from_folder = store.getFolder(fromFolder);
from_folder.open(Folder.READ_WRITE);
dfolder.open(Folder.READ_WRITE);
// get the message
Message msge[] = from_folder.getMessages(current_index + 1,
current_index + 1);
from_folder.copyMessages(msge, dfolder);
//from_folder.close(true);
//delete the message from inbox folder
String protocol = (String) parameters.get("protocol");
contentDelete(current_index, fromFolder, protocol);
} catch (Exception e)
{
log.error("Error in Movemessage()",e);
}
}
public Vector openMyfolder(String foldername, String protocol)
throws Exception
{
Vector message_folder = new Vector();
try
{
Folder fname = store.getFolder(foldername);
fname.open(Folder.READ_ONLY);
Message message[] = fname.getMessages();
for (int i = 0; i < message.length; i++)
{
Message msge = message[i];
boolean withAttachment = checkAttachment(msge);
Hashtable ht = new Hashtable();
ht.put("From", msge.getFrom()[0]);
if (msge.getSubject() == null)
{
ht.put("Subject", "none"); // set subject to "none" if
// subject is null
} else
{
ht.put("Subject", msge.getSubject());
}
ht.put("index", String.valueOf(i));
// POP3 does not provide a "received date", so the
// getReceivedDate method will
// return null. It may be possible to examine other message
// headers
// (e.g., the "Received" headers) to estimate the received
// date,
// but these techniques are error-prone at best.
//check protocol... if protocol is pop3, received date is
// empty
if (protocol.equals("imap"))
{
ht.put("ReceivedDate", msge.getReceivedDate());
} else
{
log.info("empty date!!!!!!!");
ht.put("ReceivedDate", "");
}
ht.put("size", String.valueOf(msge.getSize()));
ht.put("message", msge);
if (withAttachment == true)
{
ht.put("hasAttachment", "Attachment");
} else
{
ht.put("hasAttachment", "");
}
// check if it's a new message,
int status = checkNewmessage(msge);
log.info("status " + status);
if (status == 1)
{//new
ht.put("status", "new");
} else
{
ht.put("status", "");
}
message_folder.add(ht);
}
//folder.close(true); //1-28
//store.close(); //1-28
return message_folder;
} catch (Exception e)
{
log.error("Error in openMyFolder()",e);
return message_folder;
}
}
public void folderDelete(String folder_name)
{
try
{
//Folder current_folder =
// store.getFolder("myfolder/"+folder_name);
Folder current_folder = store.getFolder(folder_name);
//current_folder.open(Folder.READ_WRITE);
current_folder.delete(true);
} catch (Exception e)
{
log.error("Error in folderDelete()",e);
}
}
public Folder getFolder(String folder_name)
{
try
{
Folder current_folder = store.getFolder(folder_name);
return current_folder;
} catch (Exception e)
{
log.error("Error in getFolder()",e);
return null;
}
}
public String getAttachmentsFolder()
{
String path = TurbineServlet.getRealPath(File.separator)
+ "attachments";
File aFolder = new File(path);
if (!aFolder.exists())
{
aFolder.mkdir();
}
return path;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?