📄 messageaction.java
字号:
package fengyun.Fastmail.beans;
import javax.mail.*;
import javax.servlet.http.*;
import fengyun.Fastmail.Maildir.*;
import javax.activation.DataHandler;
import javax.mail.internet.*;
import javax.mail.internet.AddressException;
import java.util.Date;
import java.util.Vector;
import java.io.File;
import javax.activation.FileDataSource;
/**
* 消息动作
* @author fengyun & sanware
* @version 1.01
*/
public class MessageAction {
private static BeansConstants CONST = BeansConstants.getInstance();
/**
* 接受消息
* @param request HTTP请求
* @return int 返回结果
*/
public static int POP(HttpServletRequest request) {
int index = 0;
try {
index = Integer.parseInt(request.getParameter(CONST.folderid));
}
catch(Exception e) {
e.printStackTrace();
return CONST.FOLDERID_ERROR;
}
HttpSession httpsession = request.getSession(false);
if (httpsession == null) {
return CONST.HTTPREQUEST_ERROR;
}
Store store = (Store)httpsession.getAttribute(CONST.FastmailStore);
FolderView folderview = (FolderView)httpsession.getAttribute(CONST.FastmailFolderView);
if (store == null || folderview == null) {
return CONST.HTTPSESSION_ERROR;
}
try {
MaildirFolder folder = (MaildirFolder)store.getFolder(folderview.folderlist.getFolderid(index));
if (folder == null ) {
return CONST.FOLDERID_ERROR;
}
folder.popMessages();
return CONST.OK;
}
catch(Exception e) {
e.printStackTrace();
return CONST.FOLDERID_ERROR;
}
}
/**
* 消息移动
* @param request HTTP请求
* @return int 返回结果消息
*/
public static int Moveto(HttpServletRequest request) {
String[] msglist = request.getParameterValues(CONST.messagelist);
if (msglist == null || msglist.length <=0) {
return CONST.NONESELECTED;
}
int fromindex = 0,toindex = 0;
try {
fromindex = Integer.parseInt(request.getParameter(CONST.folderid));
String action = request.getParameter(CONST.action);
if (action != null && CONST.delete.equals(action)) {
toindex = CONST.dsbbox;
}
else {
toindex = Integer.parseInt(request.getParameter(CONST.folderto));
}
if (fromindex == toindex || toindex == -1) return CONST.NONESELECTED;
}
catch(Exception e) {
return CONST.FOLDERID_ERROR;
}
HttpSession httpsession = request.getSession(false);
if (httpsession == null) {
return CONST.HTTPREQUEST_ERROR;
}
Store store = (Store)httpsession.getAttribute(CONST.FastmailStore);
FolderView folderview = (FolderView)httpsession.getAttribute(CONST.FastmailFolderView);
if (store == null || folderview == null) {
return CONST.HTTPSESSION_ERROR;
}
try {
MaildirFolder fromfolder = (MaildirFolder)store.getFolder(folderview.folderlist.getFolderid(fromindex));
MaildirFolder tofolder = (MaildirFolder)store.getFolder(folderview.folderlist.getFolderid(toindex));
if (tofolder == null || fromfolder == null) {
return CONST.NONESELECTED;
}
fromfolder.Moveto(tofolder,msglist);
return CONST.OK;
}
catch(Exception e) {
e.printStackTrace();
return CONST.FOLDERID_ERROR;
}
}
/**
* 消息删除(彻底)
* @param request HTTPREQUEST
* @return int 返回结果消息
*/
public static int RealDeleteMessages(HttpServletRequest request) {
String[] msglist = request.getParameterValues(CONST.messagelist);
if (msglist == null || msglist.length <=0) {
return CONST.NONESELECTED;
}
int index = 0;
try {
index = Integer.parseInt(request.getParameter(CONST.folderid));
}
catch(Exception e) {
return CONST.FOLDERID_ERROR;
}
HttpSession httpsession = request.getSession(false);
if (httpsession == null) {
return CONST.HTTPREQUEST_ERROR;
}
Store store = (Store)httpsession.getAttribute(CONST.FastmailStore);
FolderView folderview = (FolderView)httpsession.getAttribute(CONST.FastmailFolderView);
if (store == null || folderview == null) {
return CONST.HTTPSESSION_ERROR;
}
try {
MaildirFolder folder = (MaildirFolder)store.getFolder(folderview.folderlist.getFolderid(index));
if (folder == null ) {
return CONST.NONESELECTED;
}
folder.deleteMessages(msglist);
return CONST.OK;
}
catch(Exception e) {
e.printStackTrace();
return CONST.MSGID_ERROR;
}
}
/**
* 保存消息
*/
public static int Save(HttpServletRequest request) {
int status = CONST.OK;
String MessageID = request.getParameter(CONST.messageid);
String to = request.getParameter(CONST.to);
if (to == null) to = "";
String cc = request.getParameter(CONST.cc);
String bcc = request.getParameter(CONST.bcc);
String subject = request.getParameter(CONST.subject);
if (subject == null) subject = "";
String body = request.getParameter(CONST.body);
if (body == null) body = "";
HttpSession httpsession = request.getSession(false);
if (httpsession == null) {
return CONST.HTTPSESSION_ERROR;
}
String from = (String)httpsession.getAttribute(CONST.FastmailAddress);
try {
MaildirStore store = (MaildirStore)(httpsession.getAttribute(CONST.FastmailStore));
FolderView folderview = (FolderView)(httpsession.getAttribute(CONST.FastmailFolderView));
MaildirFolder folder = (MaildirFolder)store.getFolder(CONST.src);
Message newmsg = new MimeMessage(store.getSession());
newmsg.setFrom(InternetAddress.parse(from,false)[0]);
newmsg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false));
if (cc != null) newmsg.setRecipients(Message.RecipientType.CC,InternetAddress.parse(cc,false));
if (bcc != null) newmsg.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(bcc,false));
newmsg.setHeader(CONST.mailerkey,CONST.mailer);
newmsg.setSentDate(new Date());
newmsg.setSubject(subject);
//附件处理
Vector attach = (Vector)httpsession.getAttribute(CONST.FastmailAttach);
boolean hasAttachment = false;
for(int i = 0;attach != null && i< attach.size();i++) {
Attachment tmp = (Attachment)attach.get(i);
if (tmp.isAttached()) {
hasAttachment = true;
break;
}
}
try {
if (hasAttachment) {
saveasmulti(newmsg,body,attach);
newmsg.saveChanges();
}
else {
saveastext(newmsg,body);
}
}
catch(MessagingException me) {
me.printStackTrace();
status = CONST.MSGCREATE_ERROR;
}
try {
if (newmsg.getSize() + folderview.getTotalDiskUsed() > folderview.getTotalSize()) return CONST.MAILBOXFULL;
if (MessageID == null || fengyun.Fastmail.util.ToolKit.getFlag(MessageID) != 'S') {
MessageID = folder.save((Message)newmsg,'S');
}
else {
folder.saveas((Message)newmsg,'S',MessageID);
}
httpsession.setAttribute(CONST.messageid,MessageID);
}
catch(MessagingException me) {
me.printStackTrace();
status = CONST.MSGSAVE_ERROR;
}
if (store.getSession().getDebug()) System.out.println("DEBUG: The Message has been saved." + MessageID);
}
catch(AddressException ae) {
ae.printStackTrace();
status = CONST.ADDRESS_ERROR;
}
catch(MessagingException me) {
me.printStackTrace();
status = CONST.FOLDERID_ERROR;
}
return status;
}
protected static void saveastext(Message msg,String body) throws MessagingException {
msg.setContent(body,"text/plain");
}
protected static void saveasmulti(Message msg,String body,Vector attach) throws MessagingException{
Multipart mp = new MimeMultipart();
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setContent(body,"text/plain");
mp.addBodyPart(mbp1);
try {
for(int i=0;attach!=null && i<attach.size();i++) { //张贴
MimeBodyPart mbp2 = new MimeBodyPart();
Attachment ai = (Attachment)attach.elementAt(i);
if (!ai.isAttached()) continue;
File aFile = new File(ai.getTempAbsolute());
if (aFile.exists()) {
// attach the file to the message
FileDataSource fds = new FileDataSource(ai.getTempAbsolute());
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(ai.getFileName());
}
mp.addBodyPart(mbp2);
}
}
catch (Exception e) {
e.printStackTrace();
}
msg.setContent(mp);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -