邮件处理:
public class SendingEmail {
private String host = "";
private String user = "";
private String password = "";
private Properties props = null;
private Session mailSession = null;// 是否在控制台显示debug信息
private Transport transport = null;
public boolean isValidConfig = false;
public SendingEmail() {
setHost(getEmailConfig("severHostOfSenderEmail"));// 指定要使用的邮件服务器
setAccount(getEmailConfig("emailAddressOfSender"),// 设置发送者email帐号
getEmailConfig("emailPasswordOfSender")); // 设置发送者email密码
checkConfigValid();
if (isValidConfig)
conectServer();
}
private void checkConfigValid() {
if (host != null && !"".equals(host))
isValidConfig = true;
}
private void conectServer() {
props = new Properties();
props.put("mail.smtp.host", host);// 指定SMTP服务器
thisAuthenticator auth = null;
if(user != null && !"".equals(user) && password != null && !"".equals(password)){
props.put("mail.smtp.auth", "true");
auth = new thisAuthenticator(user, password);
}
mailSession = Session.getDefaultInstance(props, auth != null ? auth : null);
mailSession.setDebug(false);// 是否在控制台显示debug信息
try {
transport = mailSession.getTransport("smtp");
transport.connect();
logger.info("transport.connect successfully-->("+host+","+user+")");
} catch (MessagingException e) {
e.printStackTrace();
}
}
private void setHost(String host) {
this.host = host;
}
private void setAccount(String user, String password) {
this.user = user;
this.password = password;
}
public void send(List to, List CC, String subject, String content,
List attachment) {
send(this.user, to, CC, subject, content, attachment);
}
public void send(String virtualSender, List to, List CC, String subject, String content,
List attachment) {
try {
MimeMessage message = new MimeMessage(mailSession);
logger.info("the virtual sender is-->("+virtualSender+")");
message.setFrom(new InternetAddress(virtualSender));// 发件人
for(int i=0;i<to.size();i++){
if(to.get(i)!=null && !"".equals(to.get(i)))
message.addRecipient(Message.RecipientType.TO,
new InternetAddress((to.get(i)).toString()));// 收件人
}
for(int i=0;i<CC.size();i++){
if(CC.get(i)!=null && !"".equals(CC.get(i)))
message.addRecipient(Message.RecipientType.CC,
new InternetAddress((CC.get(i)).toString()));// 收件人
}
message.setSubject(subject);// 邮件主题
if (attachment == null || attachment.size() == 0)
message.setText(content);// 邮件内容
else {
// *********************************************
message.setSentDate(new Date());
MimeBodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setText(content);
Multipart container = new MimeMultipart();
container.addBodyPart(textBodyPart);
for(int i=0;i<attachment.size();i++){
MimeBodyPart fileBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(attachment.get(i).toString());// 要发送的附件
fileBodyPart.setDataHandler(new DataHandler(fds));
fileBodyPart.setFileName(MimeUtility.encodeText(fds.getName(),"gb2312","B"));
container.addBodyPart(fileBodyPart);
}
message.setContent(container);
}
message.saveChanges();
transport.sendMessage(message, message.getAllRecipients());
} catch (Exception e) {
}
}
public void send(List to, List CC, String subject, String content) {
send(to, CC, subject, content, null);
}
public void stopSending() {
try {
if (isValidConfig)
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public String getEmailConfig(String code) {
return getPropertyResourceBundle().getString(code);
}
}
class thisAuthenticator extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public thisAuthenticator(String user, String password) {
this.strUser = user;
this.strPwd = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
上传处理:
<%
Split st=new Split();
ChangeCharSet ch=new ChangeCharSet();
String resieve=null;
String sende=null;
String titles=null;
String content=null;
List resultlist=(List)request.getSession().getAttribute("showlist");
resieve=(String)resultlist.get(0);
sende=(String)resultlist.get(1);
titles=(String)resultlist.get(2);
content=(String)resultlist.get(3);
String exlspath=(String)request.getSession().getAttribute("reportpath");
String exlsname=(String)request.getSession().getAttribute("xlsname");
StringBuffer showreportname = new StringBuffer("");
for(int i=exlspath.length()-1; i>=0;i--)
{
if(exlspath.charAt(i)!='/')
{
showreportname.append(exlspath.charAt(i));
}
else
{
break;
}
}
StringBuffer resultshowreportname = new StringBuffer("");
for(int i=showreportname.toString().length()-1; i>=0;i--)
{
if(showreportname.toString().charAt(i)!='/')
{
resultshowreportname.append(showreportname.toString().charAt(i));
}
else
{
break;
}
}
%>
<script type="text/javascript">
<!--
function Attachment(maxatts){
var attHTML = '<!--Attachment--><input type="hidden" name="attachprem" value="0" />'
+ '<table cellspacing="0" cellpadding="0"><tbody>' +
'<tr><td style="vertical-align:top;"></td>'
+
'<td id="att" style="font-size:10pt;"><div id="ia"><span class="l" οnclick="javascript:initattachment(this);">上传附件</span></div>'
+
'<div id="aa" style="display:none;"><span class="l" οnclick="javascript:addattachment(this);">增加附件</span></div></td>'
+
'</tr></tbody></table>' + '<!--/end Attachment-->' ;
document.write(attHTML);
if(maxatts)
MAX_attachments = maxatts;
}
//Attachments Operations!
var VT_attachments = 0;
var MAX_attachments = 5;
var attach=null;
function initattachment(me)
{
var ia = me.parentNode;
var att = ia.parentNode;
if(att.id != 'att')
return;
Ohidden(ia);
var aa = gObId('aa');
Oshow(aa);
addattachment(aa);
}
function addattachment(me)
{
attach = document.createElement('div');
var aa = gObId('aa');
var att = aa.parentNode;
if(att.id != 'att')
return;
switch(VT_attachments)
{
case 0:
fileid0();
break;
case 1:
fileid1();
break;
case 2:
fileid2();
break;
case 3:
fileid3();
break;
case 4:
fileid4();
break;
case 5:
fileid5();
break;
case 6:
fileid6();
break;
case 7:
fileid7();
break;
case 8:
fileid8(); break;
case 9:
fileid9();
break;
}
att.insertBefore(attach,aa);
VT_attachments ++ ;
if(VT_attachments >= MAX_attachments)
{
Ohidden(aa);
}
}
function fileid0()
{
attach.innerHTML = '<span style="font-size:10pt;"><input id="fileid0" type="file" name="fileid0" width="550" /> </span><span class="l" οnclick="javascript:delattachment(this);" style="font-size:10pt;">删除</span>';
}
function fileid1()
{
attach.innerHTML = '<span style="font-size:10pt;"><input id="fileid1" type="file" name="fileid1" /> </span><span class="l" οnclick="javascript:delattachment(this);" style="font-size:10pt;">删除</span>';
}
function fileid2()
{
attach.innerHTML = '<span style="font-size:10pt;"><input id="fileid2" type="file" name="fileid2" /> </span><span class="l" οnclick="javascript:delattachment(this);" style="font-size:10pt;">删除</span>';
}
function fileid3()
{
attach.innerHTML = '<span style="font-size:10pt;"><input id="fileid3" type="file" name="fileid3" /> </span><span class="l" οnclick="javascript:delattachment(this);" style="font-size:10pt;">删除</span>';
}
function fileid4()
{
attach.innerHTML = '<span style="font-size:10pt;"><input id="fileid4" type="file" name="fileid4" /> </span><span class="l" οnclick="javascript:delattachment(this);" style="font-size:10pt;">删除</span>';
}
function fileid5()
{
attach.innerHTML = '<span style="font-size:10pt;"><input id="fileid5" type="file" name="fileid5" /> </span><span class="l" οnclick="javascript:delattachment(this);" style="font-size:10pt;">删除</span>';
}
function fileid6()
{
attach.innerHTML = '<span style="font-size:10pt;"><input id="fileid6" type="file" name="fileid6" /></span><span class="l" οnclick="javascript:delattachment(this);" style="font-size:10pt;">删除</span>';
}
function fileid7()
{
attach.innerHTML = '<span style="font-size:10pt;"><input id="fileid7" type="file" name="fileid7" /> </span><span class="l" οnclick="javascript:delattachment(this);" style="font-size:10pt;">删除</span>';
}
function fileid8()
{
attach.innerHTML = '<span style="font-size:10pt;"><input id="fileid8" type="file" name="fileid8" /> </span><span class="l" οnclick="javascript:delattachment(this);" style="font-size:10pt;">删除</span>';
}
function fileid9()
{
attach.innerHTML = '<span style="font-size:10pt;"><input id="fileid9" type="file" name="fileid9" /> </span><span class="l" οnclick="javascript:delattachment(this);" style="font-size:10pt;">删除</span>';
}
function delattachment(me)
{
var attach = me.parentNode;
var att = attach.parentNode;
if(att.id != 'att')
return;
att.removeChild(attach);
VT_attachments -- ;
if(VT_attachments == 0)
{
Ohidden(gObId('aa'));
Oshow(gObId('ia'));
}else if(VT_attachments < MAX_attachments)
Oshow(gObId('aa'));
}
//Elements Operations
function Oshow(e)
{
e.style.display = "";
}
function Ohidden(e)
{
e.style.display = "none";
}
function gObId(id)
{
try{
return document.getElementById(id);
}
catch(e){
return null;
}
}
function isOshow(e)
{
return !(e.style.display == 'none');
}
//-->
</script>
<% if(request.getSession().getAttribute("state")!=null) {%>
<%
SendingEmail sm=new SendingEmail();
String getEmailTofrompage=(String)request.getParameter("ReceiveEmail");
String getccEmail=(String)request.getParameter("SendEmailMan");
System.out.println(getEmailTofrompage);
System.out.println(getccEmail);
String tomail=null;
String ccmail=null;
String title=null;
String contentcc=null;
List resultlist2=(List)request.getSession().getAttribute("showlist");
tomail=(String)resultlist2.get(0);
ccmail=(String)resultlist2.get(1);
title=(String)resultlist2.get(2);
contentcc=(String)resultlist2.get(3);
List tomaillist=new ArrayList();
List cclist=new ArrayList();
List pathlist=new ArrayList();
request.getSession().removeAttribute("state");
String uploadPath =sm.getEmailConfig("uploadPath");
File testdir = new File(uploadPath); //为了保证目录存在,如果没有则新建该目录
if (!testdir.exists()) {
testdir.mkdirs();
}
DiskFileUpload fu = new DiskFileUpload();
// 设置最大文件尺寸,这里是4MB
fu.setSizeMax(1024*1024*20);
// 设置缓冲区大小,这里是50kb
fu.setSizeThreshold(14096);
// 得到所有的文件:
List fileItems = fu.parseRequest(request);
FileItem file_item;
for(int i=0;i<fileItems.size();i++)
{
file_item=(FileItem)fileItems.get(i);
// System.out.println("file_item="+file_item);
}
Iterator i = fileItems.iterator();
// 依次处理每一个文件:
String EmailTitle=null;
String Emailcctem=null;
List ToEmaillist=new ArrayList();
List ToCCEmail=new ArrayList();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
// 检查当前的项目是普通的表单元素,还是一个上载的文件
if (fi.isFormField()) {
// 获得表单域的名字
if (fi.getFieldName().equals("ReceiveEmail"))
{
String ToEmail =fi.getString();
String[] tt = st.split(ToEmail,";");
for(int j=0;j<tt.length;j++)
{
ToEmaillist.add(tt[j]);
}
// System.out.println("ToEmail is ["+ToEmail+"]");
}
if (fi.getFieldName().equals("SendEmailMan"))
{
String CCEmail =fi.getString();
String[] cc = st.split(CCEmail,";");
for(int h=0;h<cc.length;h++)
{
ToCCEmail.add(cc[h]);
}
// System.out.println("CCEmail is ["+CCEmail+"]");
}
if (fi.getFieldName().equals("txtEmail_title"))
{
EmailTitle =fi.getString("UTF-8");
}
if (fi.getFieldName().equals("txtEmail_content"))
{
Emailcctem =fi.getString("UTF-8");
}
String fieldName = fi.getFieldName();
}
// 获得文件名,这个文件名包括路径:
String fileName = fi.getName();
if (!(fileName == null)) {
// 写入文件,暂定文件名为a.txt,可以从fileName中提取文件名:
String uploadFileName = fileName.substring(fileName.trim().lastIndexOf("//") + 1);
fi.write(new File(uploadPath + uploadFileName));
pathlist.add(uploadPath+uploadFileName);
System.out.println("ok");
}
}
System.out.println("ToEmaillist is ["+ToEmaillist+"]");
System.out.println("ToCCEmail is ["+ToCCEmail+"]");
String senderEmail=(String)request.getSession().getAttribute("LoginEmail");
pathlist.add(exlspath);
request.getSession().removeAttribute("state");
sm.send(senderEmail,ToEmaillist,ToCCEmail,EmailTitle,Emailcctem,pathlist);
%>
<script language="javascript">
alert("QU_BJDSDEM.SendSuccess");
</script>
<%}else{%>
<script language="javascript">
function checkonsubmit()
{
var CkTomail=document.getElementById("ReceiveEmail").value;
if(CkTomail=="")
{
alert(ReceiveEmail ");
return false;
}
else
{
return true;
}
}
</script>
<H1 align=center><%=getResourceString("conactCustomer")%></H1>
<FORM action= enctype="multipart/form-data" method="post" name="form1" id="form1" onSubmit="return checkonsubmit()" >
<td>
<script type="text/javascript">Attachment(10);</script>
</td>
</FORM>
<%request.getSession().setAttribute("state","1");%>
<%}%>