📄 sendmailservlet.java
字号:
import java.io.*;
import java.text.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SendMailServlet extends HttpServlet
{
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException
{
doGet(request,response);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException
{
response.setContentType("text/html;charset=gb2312");
HtmlOut htmlOut = new HtmlOut (response);
// 得到名字和口令
String name="",pass;
Person person = new Person();
person.setName (CommonMethods.GetUserName(request));
pass = CommonMethods.GetUserPass (request);
if( person.getName ()==null || (!person.checkPassword(pass)) )
{
htmlOut.setErrorID (htmlOut.SHOW_SIMPLE_MESSAGE ,"",
"请先登录!","Login?action=reLog");
htmlOut.outHtml();
return;
}
String sReturn = request.getParameter("return");
if(sReturn==null)
sReturn = "SendMailServlet";
MimeSplit allParam = new MimeSplit();
String sInit = request.getParameter("init");
if( sInit==null ){
allParam.dealWithIt(getUploadFile(request));
}
String sSend;
String sAddAttach ;
String sDelAttach ;
String sTo;
String sCc;
String sBcc;
String sFrom;
String sTitle;
String sInfo;
String sFileName="";
String sAttachSel;
String sAttachDir;
Email EM=null;
if( sInit==null ){
sSend = allParam.getMimeKey("sendmail");
sAddAttach = allParam.getMimeKey("addattach");
sDelAttach = allParam.getMimeKey("delattach");
sTo = allParam.getMimeKey("to");
sCc = allParam.getMimeKey("cc");
sBcc = allParam.getMimeKey("bcc");
sFrom = allParam.getMimeKey("from");
sTitle = allParam.getMimeKey("title");
sInfo = allParam.getMimeKey("info");
sAttachSel = allParam.getMimeKey("files");
sAttachDir = allParam.getMimeKey("mailattach");
sFileName = allParam.getMimeKey("file");
}
else{
sSend = request.getParameter("sendmail");
sAddAttach = request.getParameter("addattach");
sDelAttach = request.getParameter("delattach");
sTo = request.getParameter("to");
sCc = request.getParameter("cc");
sBcc = request.getParameter("bcc");
sFrom = request.getParameter("from");
sTitle = request.getParameter("title");
sInfo = request.getParameter("info");
sAttachSel = request.getParameter("files");
sAttachDir = request.getParameter("mailattach");
String sHaveMail= request.getParameter("mailfrom");
if( sHaveMail!=null ){
String sMail = request.getParameter("mail");
String sFolder = request.getParameter("FOLDER");
String sPage = request.getParameter("PAGENO");
if( sMail==null )
{
htmlOut.setErrorID (htmlOut.SHOW_SIMPLE_MESSAGE ,"",
"没有此邮件!",
"");
htmlOut.outHtml();
return;
}
if(sFolder!=null)
sMail +=("&FOLDER="+sFolder);
if(sPage!=null)
sMail +=("&PAGENO="+sPage);
GetEmailInfo GM = new GetEmailInfo ();
EM = GM.getEmailContentFromPerlCGI ( person.getName (),
pass , sMail );
if( EM==null ){
htmlOut.setErrorID (htmlOut.SHOW_SIMPLE_MESSAGE ,"",
"没有此邮件!",
"");
htmlOut.outHtml();
return;
}
}
}
Vector Attachs=new Vector (5,5);
Vector AttachPaths=new Vector (5,5);
String sAttachPath="";
if( sAddAttach!=null ){
//write attaches now;
if( sAttachDir==null ){
sAttachDir = CommonMethods.makeDirForUpload (person.getName ());
}
sAttachPath = CommonMethods.makeDirPathForUpload(person.getName (),sAttachDir);
byte[] sFileContent = allParam.getMimeKeyContent ("file");
CommonMethods.WriteToDisk (sFileContent,sAttachPath+""+File.separatorChar +""+sFileName);
}
if( sDelAttach!=null ){
sAttachPath = CommonMethods.makeDirPathForUpload(person.getName (),sAttachDir);
if( sAttachSel!=null&&sAttachDir!=null ){
File file = new File(sAttachPath+""+File.separatorChar +""+sAttachSel);
file.delete ();
}
}
if(EM!=null){
String sReply = request.getParameter ("REPLY");
String sForword = request.getParameter ("FORWARD");
String sBody=">>原件:---------------------------------\r\n";
if( EM.GetItemNum()>0 ){
MailItem EItem = EM.GetItemAt (0);
if(EItem.GetContentType ()==MailItem.CONTENT_TYPE_PLAIN ){
sBody += ( new String (EItem.GetContentOut () ) );
}
}
sFrom=person.getName () +"@365do.com";
if( sReply!=null ){
sTo = EM.GetMailHeader().GetFrom() ;
sTitle="回复:";
if( EM.GetMailHeader().GetSubject ()!=null )
sTitle+=EM.GetMailHeader().GetSubject ();
}
if( sForword!=null ){
sTitle="转发:";
if( EM.GetMailHeader().GetSubject ()!=null )
sTitle += EM.GetMailHeader().GetSubject ();
sAttachDir = CommonMethods.makeDirForUpload (person.getName ());
sAttachPath = CommonMethods.makeDirPathForUpload(person.getName (),sAttachDir);
int nAttach = EM.GetItemNum();
for( int i=0;i<nAttach;i++){
MailItem MI = EM.GetItemAt (i);
if( MI.GetContentType()==MI.CONTENT_TYPE_ATTCH ){
if(MI.GetContentName ()!=null)
CommonMethods.WriteToDisk (MI.GetContentOut (),
sAttachPath+""+File.separatorChar +""+MI.GetContentName() );
else
CommonMethods.WriteToDisk (MI.GetContentOut (),
sAttachPath+""+File.separatorChar +"noname" );
}
}
}
sInfo = sBody;
}
if( sAttachDir!=null ){
sAttachPath = CommonMethods.makeDirPathForUpload(person.getName (),sAttachDir);
AttachPaths = this.getAllFilesPathNameInDir(sAttachPath);
Attachs = this.getAllFilesNameInDir(sAttachPath);
}
if( sSend!=null ){//send mail;
try{
SendMail sm = new SendMail ();
sm.SetServerInfo (CommonMethods.sDBMailHost,sm.SMTP_PORT);
boolean tag = false;
if( sm.SetEmailInfo (sTo,sFrom,sTitle,
sCc,sBcc)&&sm.addPlainBody("",(byte)0,sInfo) )
{
//add attach;
for(int i=0;i<AttachPaths.size ();i++){
sm.addAttach ((String)Attachs.elementAt (i),
MailItem.ENCODE_TYPE_BASE64 ,
(String)AttachPaths.elementAt (i) );
}
tag = sm.SendEMail ();
Folder.deltree (CommonMethods.makeDirPathForUpload(person.getName (),sAttachDir));
}
if(tag)
htmlOut.setErrorID (htmlOut.SHOW_SIMPLE_MESSAGE ,"",
"给"+sTo+"发信成功!",sReturn);
else
htmlOut.setErrorID (htmlOut.SHOW_SIMPLE_MESSAGE ,"",
"给"+sTo+"发信失败!",sReturn);
htmlOut.outHtml ();
}catch(Exception e){
}
return;
}
//show all...
{
TemplateList tempList = CommonMethods.getSendMailTemplate ();
if(tempList!=null){
SingleTemplate st = tempList.searchTemplate ("ROOT");
for(int i=0;i<st.getKeyNum ();i++){
String sKey = st.getKeyAt (i);
htmlOut.addString (st.getStringAt (i));
if( sKey .compareTo("cgi-path")==0 )
htmlOut.addString (CommonMethods.sHostNameCGI);
if(sKey.compareTo ("to")==0)
htmlOut.addString (sTo);
if(sKey.compareTo ("cc")==0)
htmlOut.addString (sCc);
if(sKey.compareTo ("bcc")==0)
htmlOut.addString (sBcc);
if(sKey.compareTo ("title")==0)
htmlOut.addString (sTitle);
if(sKey.compareTo ("info")==0)
htmlOut.addString (sInfo);
if(sKey.compareTo ("attachID")==0)
htmlOut.addString (sAttachDir);
if(sKey.compareTo ("attaches")==0){
for(int j=0;j<Attachs.size ();j++)
htmlOut.addString ("<br><option value=\""
+(String)Attachs.elementAt (j)
+"\">"
+(String)Attachs.elementAt (j)
+"</option>");
}
if(sKey.compareTo ("from")==0)
htmlOut.addString (person.getName ()+"@365do.com");
if(sKey.compareTo ("return")==0)
htmlOut.addString (sReturn);
}
htmlOut.addString (st.getEndString ());
}
else
htmlOut.setErrorID (htmlOut.SHOW_SIMPLE_MESSAGE ,"","模版错误.","");
}
htmlOut.outHtml ();
}
MailBuff getUploadFile( HttpServletRequest request )
{
int nBufferLength=10240;
MailBuff mb = new MailBuff (nBufferLength,nBufferLength);
try{
byte[] b1=new byte[nBufferLength];
int nSize=0;
ServletInputStream sis = request.getInputStream();
try {
while((nSize=sis.read(b1,0,nBufferLength))>0){
mb.append (b1,0,nSize);
}
}catch(Exception ex) {
}
}catch(Exception e){
return null;
}
return mb;
}
Vector getAllFilesNameInDir(String sDir)
{
Vector files=new Vector (5,5);
File tmpFile1= new File(sDir);
if(tmpFile1.isDirectory()){
String[] sList = tmpFile1.list();
for(int i=0;i<sList.length ;i++ ){
files.addElement (sList[i]);
}
}
return files;
}
Vector getAllFilesPathNameInDir(String sDir)
{
Vector files=new Vector (5,5);
File tmpFile1= new File(sDir);
if(tmpFile1.isDirectory()){
String[] sList = tmpFile1.list();
for(int i=0;i<sList.length ;i++ ){
files.addElement (sDir+""+File.separatorChar +""+sList[i]);
}
}
return files;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -