?? messagesend.java
字號:
package fengyun.Fastmail.beans;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import fengyun.Fastmail.Maildir.MaildirStore;
import fengyun.Fastmail.Maildir.MaildirFolder;
import fengyun.Fastmail.Maildir.MaildirMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Vector;
import java.io.File;
import javax.activation.FileDataSource;
/**
* 發送消息
* @author sanware @ fengyun
* @version 1.01
*
*/
public class MessageSend {
/**
* 發送消息
*/
public static int Send(HttpServletRequest request) {
BeansConstants CONST = BeansConstants.getInstance();
int status = CONST.OK;
String to = request.getParameter(CONST.to);
if (to == null) {
return CONST.MAILTO_ERROR;
}
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.snt);
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);
String strSave = request.getParameter(CONST.save);
boolean isSave = true;
if (strSave == null || !strSave.equals("true")) isSave = false;
//附件處理
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) {
MessageAction.saveasmulti(newmsg,body,attach);
}
else {
MessageAction.saveastext(newmsg,body);
}
}
catch(MessagingException me) {
me.printStackTrace();
return CONST.MSGCREATE_ERROR;
}
try {
Transport.send(newmsg);
for(int i=0;attach!=null && i<attach.size();) {
Attachment ai = (Attachment)attach.elementAt(i);
if (!ai.isUpload()) attach.remove(ai);
else {
ai.setAttached(false);
i++;
}
}
}
catch(MessagingException me) {
me.printStackTrace();
return CONST.MSGSEND_ERROR;
}
if (isSave) {
if (newmsg.getSize() + folderview.getTotalDiskUsed() > folderview.getTotalSize()) return CONST.MAILBOXFULL;
if (hasAttachment) {
newmsg.saveChanges();
}
try {
String MessageID = folder.save((Message)newmsg,'T');
if (store.getSession().getDebug()) {
if (MessageID != null)
System.out.println("DEBUG: The Message" + MessageID + " has been saved.");
}
}
catch(MessagingException me) {
me.printStackTrace();
System.out.println("DEBUG: SAVE ERROR");
return CONST.MSGSAVE_ERROR;
}
}
}
catch(AddressException ae) {
ae.printStackTrace();
return CONST.ADDRESS_ERROR;
}
catch(MessagingException me) {
me.printStackTrace();
return CONST.FOLDERID_ERROR;
}
return status;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -