邮件处理:
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
message.addRecipient(Message.RecipientType.TO,
new InternetAddress((to.get(i)).toString()));// 收件人
}
for(int i=0;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
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;
}
}
%>
<% 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
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
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
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);
%>
<%}else{%>
<%=getResourceString("conactCustomer")%>
<%request.getSession().setAttribute("state","1");%>
<%}%>