import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class GamMail {
private static String contentA;
private static String contentB;
private static String contentC;
private static String Listpath;
private static String ContentPath;
public static String defultData;
public static void main(String[] args)
{
Listpath = args[0];
ContentPath = args[1];
String host = "10.0.0.0.1";
String port = "25";
System.out.println(Listpath+">>"+ContentPath);
Properties props = setProperties(host,port);
Boolean isSuccuss = sendMail(props);
if(isSuccuss){
System.out.println("寄送成功");
}else{
System.out.println("寄送失敗");
}
}
public static boolean sendMail(Properties props)
{
try
{
//construct a mail session
Session session = Session.getDefaultInstance(props,null);
session.setDebug(false);
MimeMessage msg = new MimeMessage(session);
//讀取資料
System.out.println("讀取資料");
Map
Set
System.out.println("準備寄送");
for(String key:mapkey){
System.out.println("寄送信件"+key+"類別");
//[0]寄件者名,[1]寄件者MAIL,[2]收件者MAIL,[3]信件主旨,[4]信件類型,[5]替換代碼
defultData = "inputData";
List
for(String[] addressContent:addresslist){
//讀取信件內容
String content = readContentData(key,addressContent[4],defultData);
//設定主旨
msg.setSubject(addressContent[3]);
//設定寄件人
msg.setFrom(new InternetAddress(addressContent[1],addressContent[0]));
//設定送信時間
msg.setSentDate(new Date());
//設定收件人
System.out.println("寄送信件至 :"+addressContent[2]);
InternetAddress[] address = InternetAddress.parse(addressContent[2],false);
msg.setRecipients(Message.RecipientType.TO,address);
//設定信件內容
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(content,"text/html; charset=utf8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mbp);
msg.setContent(multipart);
Transport.send(msg);
}
}
return true;
}
catch(Exception e){
e.printStackTrace();
return false;
}
}
public static Properties setProperties(String host,String port){
Properties props = new Properties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", port);
props.put("mail.smtp.auth", false);
return props;
}
public static Map
Map
String str = null;
try {
//分類
List
List
List
// Listpath = "D:/廖哥需求/21.社交工程寄mail/範例名單A.csv";
FileInputStream fr = new FileInputStream(Listpath);
BufferedReader br = new BufferedReader(new InputStreamReader(fr,"Big5"));
int i = 0;
while((str = br.readLine()) != null){
if(i == 0){
i++;
continue;
}
String tempLineData[] = str.split("\\,");//逗號分割
if("A".equals(tempLineData[4])){
dataListA.add(tempLineData);
}else if("B".equals(tempLineData[4])){
dataListB.add(tempLineData);
}else if("C".equals(tempLineData[4])){
dataListC.add(tempLineData);
}
}
if(dataListA.size() > 0)
lineMap.put("A",dataListA);
if(dataListB.size() > 0)
lineMap.put("B",dataListB);
if(dataListC.size() > 0)
lineMap.put("C",dataListC);
} catch (Exception e) {
System.out.println(str);
e.printStackTrace();
}
return lineMap;
}
public static String readContentData(String type,String rePlaceData,String beforeData){
//之前有讀取過,就只要置換超連結內碼
if("A".equals(type) && null != getContentA() && !"".equals(contentA)){
setContentA(getContentA().replaceAll(beforeData, rePlaceData));
defultData = rePlaceData;
return getContentA();
}else if("B".equals(type) && null != getContentB() && !"".equals(contentB)){
setContentB(getContentB().replaceAll(beforeData, rePlaceData));
defultData = rePlaceData;
return getContentB();
}else if("C".equals(type) && null != getContentC() && !"".equals(contentC)){
setContentC(getContentC().replaceAll(beforeData, rePlaceData));
defultData = rePlaceData;
return getContentC();
}
String path = ContentPath+type+".txt";
// path = "D:/廖哥需求/21.社交工程寄mail/"+type+".txt";
StringBuffer contentStr = new StringBuffer();
try {
FileInputStream fr = new FileInputStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(fr,"UTF-8"));
String str = null;
while((str = br.readLine()) != null){
contentStr.append(str);
}
} catch (Exception e) {
e.printStackTrace();
}
String returnData = contentStr.toString().replace(beforeData, rePlaceData);
if("A".equals(type)){
setContentA(returnData);
}else if("B".equals(type)){
setContentB(returnData);
}else{
setContentC(returnData);
}
defultData = rePlaceData;
return returnData;
}
public static String getContentA() {
return contentA;
}
public static void setContentA(String contentA) {
GamMail.contentA = contentA;
}
public static String getContentB() {
return contentB;
}
public static void setContentB(String contentB) {
GamMail.contentB = contentB;
}
public static String getContentC() {
return contentC;
}
public static void setContentC(String contentC) {
GamMail.contentC = contentC;
}
}