Created
October 25, 2014 05:39
-
-
Save 0532/afe0d316774644bb2ee9 to your computer and use it in GitHub Desktop.
群发邮件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sentMail; | |
import org.apache.poi.xssf.usermodel.XSSFCell; | |
import org.apache.poi.xssf.usermodel.XSSFSheet; | |
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |
import org.springframework.mail.SimpleMailMessage; | |
import org.springframework.mail.javamail.JavaMailSenderImpl; | |
import java.io.*; | |
/** | |
* User: Lichao.W | |
* Date: 2014/10/24 | |
* Time: 16:28 | |
* activation.jar;commons-logging.jar;dom4j-1.6.1.jar; | |
* log4j-1.2.14.jar;mail.jar;poi-3.7-20101029.jar;poi-ooxml-3.7.20101029.jar; | |
* poi-ooxml-schemas-3.7.20101029.jar;quartz-api.jar;servlet-api.jar; | |
* spring.jar;\xmlbeans-2.3.0.jar | |
*/ | |
public class SendMailGroup { | |
private static final int DEFAULT_PAUSE_TIME_SECONDS = 30; | |
private static final String toemail = "factoryrubber@163.com"; | |
private static final String passwd = "heliqin3771*"; | |
private String title = "factoryrubber";//默认主题 | |
public void readXLS() { | |
try { | |
XSSFWorkbook xwb = new XSSFWorkbook("d:/email.xlsx"); | |
//String ename = ""; //收件人姓名 | |
String email = ""; //收件人地址 | |
String tmp = ""; | |
// 读取第一章表格内容 | |
XSSFSheet sheet = xwb.getSheetAt(0); | |
int rowLen = sheet.getLastRowNum(); | |
//XSSFRow row; | |
XSSFCell cell; | |
// 循环输出表格中的内容 | |
for (int i = 0; i <= rowLen; i++) { | |
cell = sheet.getRow(i).getCell(0); | |
email = cell.getStringCellValue().trim(); | |
//System.out.println("email:" + email); | |
sendEmail(email); | |
Thread.sleep(1000 * DEFAULT_PAUSE_TIME_SECONDS); | |
} | |
} catch (InterruptedException e) { | |
System.out.println("Thread Error"); | |
e.printStackTrace(); | |
} catch (IOException e) { | |
System.out.println("read excel error"); | |
e.printStackTrace(); | |
} | |
} | |
public String readTxt() { | |
File file = new File("d:/email.txt"); | |
BufferedReader reader = null; | |
String tempString = null; | |
StringBuilder sb = new StringBuilder(); | |
int line = 1; | |
try { | |
reader = new BufferedReader(new FileReader(file)); | |
while ((tempString = reader.readLine()) != null) { | |
if (line == 1) { | |
title = tempString; | |
} else { | |
sb.append(tempString + "\n"); | |
} | |
line++; | |
} | |
} catch (FileNotFoundException e) { | |
System.out.println("文件不存在...."); | |
e.printStackTrace(); | |
} catch (IOException e) { | |
System.out.println("文件读取错误...."); | |
e.printStackTrace(); | |
} finally { | |
try { | |
if (reader != null) { | |
reader.close(); | |
} | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
return sb.toString(); | |
} | |
public void sendEmail(String email) { | |
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); | |
// 设定mail server | |
senderImpl.setHost("smtp.163.com"); | |
senderImpl.setUsername(toemail); | |
senderImpl.setPassword(passwd); | |
String messg = readTxt(); | |
// 建立邮件消息 | |
SimpleMailMessage mailMessage = new SimpleMailMessage(); | |
// 设置收件人,寄件人 | |
//String[] array = new String[] {"wanglichao@163.com","937397606@qq.com"}; | |
//mailMessage.setTo(array); //相当群发 | |
mailMessage.setTo(email); | |
mailMessage.setFrom(toemail); | |
mailMessage.setSubject(title); | |
mailMessage.setText(messg); | |
// 发送邮件 | |
senderImpl.send(mailMessage); | |
System.out.println("邮件发送成功...to..." + email); | |
} | |
public static void main(String[] args) { | |
SendMailGroup send = new SendMailGroup(); | |
send.readXLS(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment