package JBase.bs.mail;import javax.mail.*;
import java.net.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
import javax.activation.*;
import JBase.bs.file.bsFile;
import JBase.bs.exception.JBaseException;
import JBase.bs.log.bsLog;
import JBase.bs.constant.bsApp;public class bsMail { private String strMailEncode = null;
private String strMailSrvAddr = null;
private int iMailSrvPort = 25;
private Session session = null;
private MimeMessage msg = null;
private Multipart mp = new MimeMultipart();
private Properties propMail = new Properties();
public bsMail()
throws Exception
{
session = Session.getDefaultInstance(propMail,null);
msg = new MimeMessage(session);
propMail.put("mail.transport.protocol", "smtp");
} public void setEncodeName(String MailEncode)
{
strMailEncode = MailEncode;
} public void setMailSrvAddr(String MailSrvAddr)
throws Exception
{
setMailSrvAddrAsIpAddr(MailSrvAddr);
propMail.put("mail.smtp.host", strMailSrvAddr);
} private void setMailSrvAddrAsIpAddr(String MailSrvAddr)
throws Exception
{
InetAddress host[] = null;
try {
host = InetAddress.getAllByName(MailSrvAddr);
} catch (UnknownHostException e) {
bsLog.log.warn(this,"");
} if (host.length > 0) {
strMailSrvAddr = host[0].getHostAddress();
} else {
strMailSrvAddr = MailSrvAddr;
}
} public void setMailPort(int port)
{
iMailSrvPort = port;
} public MimeMessage getMimeMessage()
{
return(msg);
} public void setMailFrom(String strFrom)
throws Exception
{
try {
msg.setFrom(InternetAddress.parse(strFrom, true)[0]);
} catch (AddressException e) {
} catch (MessagingException e) {
}
} public void setMailSubject(String MailSubject)
throws Exception
{
try {
msg.setSubject(MimeUtility.encodeText(MailSubject,strMailEncode,"B"));
} catch (MessagingException e) {
bsLog.log.warn(this,e);
} catch (UnsupportedEncodingException e) {
bsLog.log.warn(this,e);
}
} public void setMailTo(String strMailTo)
throws Exception
{
if(strMailTo == null || strMailTo.equals("")){
return ;
}
try {
String strTempTo = "";
StringTokenizer tokens = new StringTokenizer(strMailTo,",");
while(tokens.hasMoreTokens())
{
strTempTo = (String)tokens.nextToken();
msg.addRecipients(MimeMessage.RecipientType.TO,InternetAddress.parse(strTempTo ,true));
}
} catch (AddressException e) {
bsLog.log.warn(this,e);
} catch (MessagingException e) {
bsLog.log.warn(this,e);
}
} public void setHeader(String strName, String strValue)
throws Exception
{
try {
msg.setHeader(strName, strValue);
} catch (MessagingException e) {
bsLog.log.warn(this,e);
}
} public void addHeader(String strName, String strValue)
throws Exception
{
try {
msg.addHeader(strName, strValue);
} catch (MessagingException e) {
bsLog.log.warn(this,e);
}
} public void setMailText(String MailText)
throws Exception
{
try {
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(MailText,strMailEncode);
mp.addBodyPart(mbp);
} catch (MessagingException e) {
bsLog.log.warn(this,e);
}
} public void setMailFile(String MailFullFilePath)
throws Exception
{
try {
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(MailFullFilePath);
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
} catch (MessagingException e) {
bsLog.log.warn(this,e);
}
} public boolean isNotSendMail()
{
String strValue = propMail.getProperty("mail.notSend"); if ((strValue != null) && strValue.equalsIgnoreCase("true")) {
return(true);
}
return(false);
} public void send()
throws Exception
{
bsLog.log.debug(this,"S---------------------------------------");
bsLog.log.debug(this,"strMailEncode="+strMailEncode);
bsLog.log.debug(this,"strMailSrvAddr="+strMailSrvAddr);
bsLog.log.debug(this,"iMailSrvPort="+iMailSrvPort);
bsLog.log.debug(this,"E---------------------------------------");
try {
msg.setContent(mp);
msg.setSentDate(new Date());
Transport.send(msg);
} catch (MessagingException e) {
e.printStackTrace();
bsLog.log.warn(this,e);
}
} public void sendFileName(String msgText1,String filename){
try {
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);
MimeBodyPart mbp2 = new MimeBodyPart(); FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName()); Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2); msg.setContent(mp); msg.setSentDate(new Date()); Transport.send(msg); } catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
}
}}
import java.net.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
import javax.activation.*;
import JBase.bs.file.bsFile;
import JBase.bs.exception.JBaseException;
import JBase.bs.log.bsLog;
import JBase.bs.constant.bsApp;public class bsMail { private String strMailEncode = null;
private String strMailSrvAddr = null;
private int iMailSrvPort = 25;
private Session session = null;
private MimeMessage msg = null;
private Multipart mp = new MimeMultipart();
private Properties propMail = new Properties();
public bsMail()
throws Exception
{
session = Session.getDefaultInstance(propMail,null);
msg = new MimeMessage(session);
propMail.put("mail.transport.protocol", "smtp");
} public void setEncodeName(String MailEncode)
{
strMailEncode = MailEncode;
} public void setMailSrvAddr(String MailSrvAddr)
throws Exception
{
setMailSrvAddrAsIpAddr(MailSrvAddr);
propMail.put("mail.smtp.host", strMailSrvAddr);
} private void setMailSrvAddrAsIpAddr(String MailSrvAddr)
throws Exception
{
InetAddress host[] = null;
try {
host = InetAddress.getAllByName(MailSrvAddr);
} catch (UnknownHostException e) {
bsLog.log.warn(this,"");
} if (host.length > 0) {
strMailSrvAddr = host[0].getHostAddress();
} else {
strMailSrvAddr = MailSrvAddr;
}
} public void setMailPort(int port)
{
iMailSrvPort = port;
} public MimeMessage getMimeMessage()
{
return(msg);
} public void setMailFrom(String strFrom)
throws Exception
{
try {
msg.setFrom(InternetAddress.parse(strFrom, true)[0]);
} catch (AddressException e) {
} catch (MessagingException e) {
}
} public void setMailSubject(String MailSubject)
throws Exception
{
try {
msg.setSubject(MimeUtility.encodeText(MailSubject,strMailEncode,"B"));
} catch (MessagingException e) {
bsLog.log.warn(this,e);
} catch (UnsupportedEncodingException e) {
bsLog.log.warn(this,e);
}
} public void setMailTo(String strMailTo)
throws Exception
{
if(strMailTo == null || strMailTo.equals("")){
return ;
}
try {
String strTempTo = "";
StringTokenizer tokens = new StringTokenizer(strMailTo,",");
while(tokens.hasMoreTokens())
{
strTempTo = (String)tokens.nextToken();
msg.addRecipients(MimeMessage.RecipientType.TO,InternetAddress.parse(strTempTo ,true));
}
} catch (AddressException e) {
bsLog.log.warn(this,e);
} catch (MessagingException e) {
bsLog.log.warn(this,e);
}
} public void setHeader(String strName, String strValue)
throws Exception
{
try {
msg.setHeader(strName, strValue);
} catch (MessagingException e) {
bsLog.log.warn(this,e);
}
} public void addHeader(String strName, String strValue)
throws Exception
{
try {
msg.addHeader(strName, strValue);
} catch (MessagingException e) {
bsLog.log.warn(this,e);
}
} public void setMailText(String MailText)
throws Exception
{
try {
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(MailText,strMailEncode);
mp.addBodyPart(mbp);
} catch (MessagingException e) {
bsLog.log.warn(this,e);
}
} public void setMailFile(String MailFullFilePath)
throws Exception
{
try {
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(MailFullFilePath);
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
} catch (MessagingException e) {
bsLog.log.warn(this,e);
}
} public boolean isNotSendMail()
{
String strValue = propMail.getProperty("mail.notSend"); if ((strValue != null) && strValue.equalsIgnoreCase("true")) {
return(true);
}
return(false);
} public void send()
throws Exception
{
bsLog.log.debug(this,"S---------------------------------------");
bsLog.log.debug(this,"strMailEncode="+strMailEncode);
bsLog.log.debug(this,"strMailSrvAddr="+strMailSrvAddr);
bsLog.log.debug(this,"iMailSrvPort="+iMailSrvPort);
bsLog.log.debug(this,"E---------------------------------------");
try {
msg.setContent(mp);
msg.setSentDate(new Date());
Transport.send(msg);
} catch (MessagingException e) {
e.printStackTrace();
bsLog.log.warn(this,e);
}
} public void sendFileName(String msgText1,String filename){
try {
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);
MimeBodyPart mbp2 = new MimeBodyPart(); FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName()); Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2); msg.setContent(mp); msg.setSentDate(new Date()); Transport.send(msg); } catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
}
}}
没整理
先删除bsLog.log.debug();
调用时:
bsMail mail = new bsMail();
mail.setEncodeName(“GB2312”);
mail.setMailSrvAddr("172.28.122.1");
mail.setMailFrom("****@ec.necsi-sh.nec.com.cn");
mail.setMailSubject("JBase mail send");
mail.setMailText(strTemp);
mail.setMailFile("D:\\JBase\\"+strPdfName);
mail.setMailTo(strMailTo);
mail.send();
就算一起提交也是要先上传后再处理http://www.csdn.net/Develop/read_article.asp?id=20621
我也覺得先上傳後,再處理較容易些.
163.net和21cn.com就是那樣做的.但是先上傳,為了避免附件文件沖突,不是每個登陸用戶都要給它建立一個目錄?