/**
*
*/
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
/**
* @author liuwen
*
*/
public class SendMail
{
String to = "";//收件人
String from = "";//发件人
String cc = "";//抄送人
String bcc = "";//暗送人
String smtpServer = "";//smtp服务器
String username = "" ;
String password = "" ;
String filename = "";//附件文件名
String subject = "";//邮件主题
String content = "";//邮件正文
Vector file = new Vector();//附件文件集合
private String cfg_file = "cmail.properties";
private Properties p = new Properties();
public SendMail()
{
}
public SendMail(String to,String from,String smtpServer,String username,String password,String subject,String content)
{
this.to = to;
this.from = from;
this.smtpServer = smtpServer;
this.username = username;
this.password = password;
this.subject = subject;
this.content = content;
}
public void setSmtpServer(String smtpServer)
{
this.smtpServer = smtpServer;
}
public void setUserName(String usename)
{
this.username = usename;
}
public void setPassWord(String pwd)
{
this.password = pwd;
}
public void setTo(String to)
{
this.to = to;
}
public void setFrom(String from)
{
this.from = from;
}
public void setSubject(String subject)
{
this.subject = subject;
}
public void setContent(String content)
{
this.content = content;
}
//把邮件主题转换为中文
public String transferChinese(String strText)
{
try
{
strText = MimeUtility.encodeText(new String(strText.getBytes(), "GB2312"), "GB2312", "B");
}
catch(Exception e)
{
e.printStackTrace();
}
return strText;
}
//增加附件
public void attachfile(String fname)
{
file.addElement(fname);
}
public void loadConfig() //throws Exception
{
InputStream in = null;
try {
in = new FileInputStream( cfg_file);
this.getProperties().load( in);
} catch (IOException e) {
System.out.println(cfg_file+" was not found");
} finally{
try {
if( null != in)
in.close();
} catch (IOException e) {
}
}
}
public String getCfg_file() {
return cfg_file;
}
public void setCfg_file(String cfg_file) {
this.cfg_file = cfg_file;
}
public Properties getProperties()
{
return p;
}
public String getParameterValue( String entry)
{
return p.getProperty(entry);
}
public boolean send()
{
//构造mail session
Properties props = System.getProperties();
props.put("mail.smtp.host",smtpServer);
props.put("mail.smtp.auth","true");
Session session=Session.getDefaultInstance(props, new Authenticator()
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username,password);
}
});
try
{
Transport transport = session.getTransport("smtp");
//构造MimeMessage 并设定基本的值
MimeMessage msg = new MimeMessage(session);
//设置发送人地址
msg.setFrom(new InternetAddress(from));
String toaddr[] = to.split(",");
InternetAddress[] address = new InternetAddress[toaddr.length];
for(int i = 0; i < toaddr.length; i++){
address[i] = new InternetAddress(toaddr[i].trim());
}
//设置收件人地址
msg.setRecipients(Message.RecipientType.TO,address);
//添加抄送人
InternetAddress[] ccaddress = null;
if (cc != null && !cc.equals("")){
String ccaddr[] = cc.split(",");
ccaddress = new InternetAddress[ccaddr.length];
for(int i = 0; i < ccaddr.length; i++){
ccaddress[i] = new InternetAddress(ccaddr[i].trim());
}
if (ccaddr != null)
msg.addRecipients(Message.RecipientType.CC,ccaddress);
}
//添加暗送人
InternetAddress[] bccaddress = null;
if (bcc != null && !bcc.equals("")){
String bccaddr[] = bcc.split(",");
bccaddress = new InternetAddress[bccaddr.length];
for(int i = 0; i < bccaddr.length; i++){
bccaddress[i] = new InternetAddress(bccaddr[i].trim());
}
if (bccaddr != null)
msg.addRecipients(Message.RecipientType.BCC,bccaddress);
}
subject = transferChinese(subject);
//设置邮件标题
msg.setSubject(subject);
//构造Multipart
Multipart mp = new MimeMultipart();
//向Multipart添加正文
MimeBodyPart mbpContent = new MimeBodyPart();
mbpContent.setText(content);
//向MimeMessage添加(Multipart代表正文)
mp.addBodyPart(mbpContent);
//向Multipart添加附件
Enumeration efile=file.elements();
while(efile.hasMoreElements())
{
MimeBodyPart mbpFile = new MimeBodyPart();
filename=efile.nextElement().toString();
FileDataSource fds = new FileDataSource(filename);
mbpFile.setDataHandler(new DataHandler(fds));
mbpFile.setFileName(fds.getName());
//向MimeMessage添加(Multipart代表附件)
mp.addBodyPart(mbpFile);
System.out.println(filename);
}
file.removeAllElements();
//向Multipart添加MimeMessage
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();
}
return false;
}
return true;
}
public static void main(String[] args)
{
SendMail sendmail = new SendMail();
if (args != null && args.length > 0 ) {
sendmail.setCfg_file(args[0].trim());
}
try {
sendmail.loadConfig();
//设置发送主机的服务器地址
sendmail.setSmtpServer(sendmail.getParameterValue("smtpServer")); //.setSmtpServer("smtp.m800400.com");
//此处设置登陆的用户名
sendmail.setUserName(sendmail.getParameterValue("usename")); //.setUserName("ticket");
//此处设置登陆的密码
sendmail.setPassWord(sendmail.getParameterValue("pwd")); //.setPassWord("123456");
//发送人
sendmail.setFrom(sendmail.getParameterValue("from")); //.setFrom("ticket");
//发送的地址
sendmail.setTo(sendmail.getParameterValue("to")); //.setTo("huangyuanbin@m800400.com");
//抄送的地址
sendmail.setCc(sendmail.getParameterValue("cc")); //.setTo("huangyuanbin@m800400.com");
//暗送的地址
sendmail.setBcc(sendmail.getParameterValue("bcc")); //.setTo("huangyuanbin@m800400.com");
//设置标题
sendmail.setSubject(sendmail.getParameterValue("subject")); //.setSubject("你好,这是测试!");
//设置内容
sendmail.setContent(sendmail.getParameterValue("content")); //.setContent("你好这是一个带多附件的测试!");
String [] fname = sendmail.getParameterValue("fname").trim().split(",");
for (int i = 0 ; i< fname.length ; i ++){
//粘贴附件
sendmail.attachfile(fname[i].trim());
}
//粘贴附件
//sendmail.attachfile(sendmail.getParameterValue("fname")); //.attachfile("SendMail.java");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Cmail Config file not found!");
}
//发送
if(!sendmail.send()){
System.out.println("Send Email fail !");
System.exit(1);
}
}
public String getCc() {
return cc;
}
public void setCc(String cc) {
this.cc = cc;
}
public String getBcc() {
return bcc;
}
public void setBcc(String bcc) {
this.bcc = bcc;
}
}