package com.foxconn.logic; import java.util.Date; import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; import javax.activation.DataSource; public class MailSender { public String body; public String subject; public String from; public String host; public String to[]; public String cc[]; public String bcc[]; public boolean toflag; public boolean ccflag; public boolean bccflag; public MailSender() { toflag = false; ccflag = false; bccflag = false; } public void setFrom(String s) { from = s; } public void setTo(String s) { to = s.split(","); toflag = true; } public void setCc(String s) { cc = s.split(","); ccflag = true; } public void setBcc(String s) { bcc = s.split(","); bccflag = true; } public void setSubject(String s) { subject = s; } public void setBody(String s) { body = s; } public void setHost(String s) { host = s; } public boolean send() { boolean result = false; try { Properties properties = System.getProperties(); properties.put("mail.smtp.host",host); Session session = Session.getDefaultInstance(properties, null); session.setDebug(false); MimeMessage mimemessage = new MimeMessage(session); mimemessage.setFrom(new InternetAddress(from)); if(toflag) { InternetAddress ainternetaddress[] = new InternetAddress[to.length]; for(int i = 0; i < to.length; i++) { if(to[i].indexOf(" ")!=-1){ to[i]="/""+to[i]+"/""; } ainternetaddress[i] = new InternetAddress(to[i]); } mimemessage.setRecipients(javax.mail.Message.RecipientType.TO, ainternetaddress); } if(ccflag) { InternetAddress ainternetaddress1[] = new InternetAddress[cc.length]; for(int j = 0; j < cc.length; j++) { ainternetaddress1[j] = new InternetAddress(cc[j]); } mimemessage.setRecipients(javax.mail.Message.RecipientType.CC, ainternetaddress1); } if(bccflag) { InternetAddress ainternetaddress2[] = new InternetAddress[bcc.length]; for(int k = 0; k < bcc.length; k++) { ainternetaddress2[k] = new InternetAddress(bcc[k]); } mimemessage.setRecipients(javax.mail.Message.RecipientType.BCC, ainternetaddress2); } mimemessage.setSubject(subject, "big5"); MimeMultipart mimemultipart = new MimeMultipart(); MimeBodyPart mimebodypart = new MimeBodyPart(); mimebodypart.setContent(body, "text/html;charset=big5"); mimemultipart.addBodyPart(mimebodypart); mimemessage.setContent(mimemultipart); Transport.send(mimemessage); result = true; } catch (AddressException e) { e.printStackTrace(); return false; } catch (MessagingException e) { e.printStackTrace(); return false; } return result; } public static void main(String args[]) { String addr="CP-IT4B/CCPBG/FOXCONN"; //String[] copyaddr={"CP-SCM2B/CPBG/FOXCONN"}; //String[] bcopyaddr={"CP-SCM3B/CPBG/FOXCONN"}; //String[] attachfile={"D://aa.txt","D://aa1.txt"};//附件中中文文件名不能識別 String mailhost="10.194.216.121"; Date date = new Date(); MailSender sender=new MailSender(); sender.setHost(mailhost); sender.setFrom("CP-SCM5B/CCPBG/FOXCONN"); sender.setTo(addr); //sender.setCc(copyaddr); //sender.setBcc(bcopyaddr); sender.setBody("This is a test Mail
successful,ok
last testing+date.toString()"); sender.setSubject("成型廠設備咨詢管理系統測試郵件yyyymm"); //sender.setAttachfile(attachfile); boolean aa=sender.send(); if(aa) System.out.println("success"); } public static final String mailEnd = "
***********************************************************************
" + " ??虜???雿輻????????????
" + " ???雿輻???葉???噶???,隢蝟餅??????嚗?" + "
" + " CP蝟餌絞鞈????:
" + " ?蝟颱犖:?銝?曀?/??? TEL:563-79962/79961
" + " Mail: CP-IT4B/CCPBG/FOXCONN" + "
CCPBG???瘚雯蝡???:http://10.194.203.36/indexlist/index.asp" + "
***********************************************************************"; }
successful,ok
last testing+date.toString()"); sender.setSubject("成型廠設備咨詢管理系統測試郵件yyyymm"); //sender.setAttachfile(attachfile); boolean aa=sender.send(); if(aa) System.out.println("success"); } public static final String mailEnd = "
***********************************************************************
" + " ??虜???雿輻????????????
" + " ???雿輻???葉???噶???,隢蝟餅??????嚗?" + "
" + " CP蝟餌絞鞈????:
" + " ?蝟颱犖:?銝?曀?/??? TEL:563-79962/79961
" + " Mail: CP-IT4B/CCPBG/FOXCONN" + "
CCPBG???瘚雯蝡???:http://10.194.203.36/indexlist/index.asp" + "
***********************************************************************"; }