public classsendMail {/*** 创建邮件信息
*@paramsession
*@paramfromAccount
*@paramtoAccount
*@paramsourcePath xml文件目录 e.g. xml
*@paramzipPath zip文件目录 e.g. zip/person.zip*/
public static void CreateMessage(final Session session, final String fromAccount, final String toAccount,final String sourcePath,finalString zipPath){try{final String subjectStr="圣诞节快乐";//主题
final StringBuffer contentStr=new StringBuffer();//内容
contentStr.append("
Dear Friends,
");
contentStr.append("Christmas is coming up soon.
Wish you lots of love, joy &happiness. happy christmas.");
contentStr.append("
Regards,
").append("ZHBIT College
");//创建默认的 MimeMessage 对象final MimeMessage message = newMimeMessage(session);//Set From: 头部头字段
message.setFrom(newInternetAddress(fromAccount));//Set To: 头部头字段
message.addRecipient(Message.RecipientType.TO,newInternetAddress(toAccount));//Set Subject: 头部头字段
message.setSubject(subjectStr);//创建消息部分
final BodyPart messageBodyPart = newMimeBodyPart();//消息
messageBodyPart.setContent(contentStr.toString(),"text/html;charset=UTF-8");//创建多重消息
final Multipart multipart = newMimeMultipart();//设置文本消息部分
multipart.addBodyPart(messageBodyPart);//为邮件添加多个附件
MimeBodyPart attachment = null;final File source = newFile(sourcePath);if (!source.exists()) {
System.out.println(sourcePath+ " not exists");return;
}final File[] files =source.listFiles();for (finalFile f : files) {
attachment= newMimeBodyPart();final String filePath =f.getPath();//根据附件文件创建文件数据源
final DataSource ds = newFileDataSource(filePath);
attachment.setDataHandler(newDataHandler(ds));//为附件设置文件名
attachment.setFileName(ds.getName());
multipart.addBodyPart(attachment);
}//添加zip附件
attachment = newMimeBodyPart();//根据附件文件创建文件数据源
final DataSource ds = newFileDataSource(zipPath);
attachment.setDataHandler(newDataHandler(ds));//为附件设置文件名
attachment.setFileName(ds.getName());
multipart.addBodyPart(attachment);//发送完整消息
message.setContent(multipart);//发送消息
Transport.send(message);
}catch (finalMessagingException mex) {
mex.printStackTrace();
}
}/*** 将源文件目录下的所有文件打包成zip文件
*@paramsourceFilePath e.g. xml
*@paramzipFilePath e.g. zip
*@paramfileName e.g. person
*@return返回生成的zip文件目录 e.g. zip/person.zip*/
public static String tozip(final String sourceFilePath, finalString zipFilePath,finalString fileName) {final File sourceFile = newFile(sourceFilePath);
FileInputStream fis= null;
BufferedInputStream bis= null;
FileOutputStream fos= null;
ZipOutputStream zos= null;final String createZipPath=zipFilePath+ "/" + fileName+ ".zip";if(!sourceFile.exists()){
System.out.println("待压缩的文件目录:" + sourceFilePath + "不存在");
}else{try{final File zipFile = newFile(createZipPath);final File[] sourceFiles =sourceFile.listFiles();if(null == sourceFiles || sourceFiles.length < 1) {
System.out.println("待压缩的文件目录:" + sourceFilePath + " 里面不存在文件,无需压缩");
}else{
fos= newFileOutputStream(zipFile);
zos= new ZipOutputStream(newBufferedOutputStream(fos));final byte[] bufs = new byte[1024*10];for(int i=0;i
final ZipEntry zipEntry = newZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);//读取待压缩的文件并写进压缩包里
fis = newFileInputStream(sourceFiles[i]);
bis= new BufferedInputStream(fis,1024*10);int read = 0;while((read=bis.read(bufs, 0, 1024*10)) != -1) {
zos.write(bufs,0, read);
}
}
}
}catch (finalFileNotFoundException e) {
e.printStackTrace();throw newRuntimeException(e);
}catch (finalIOException e) {
e.printStackTrace();throw newRuntimeException(e);
}finally{try{if (null !=bis) {
bis.close();
}if (null !=zos) {
zos.close();
}
}catch (finalIOException e) {
e.printStackTrace();throw newRuntimeException(e);
}
}
}returncreateZipPath;
}public static void main(finalString[] args) {//收件人电子邮箱
final String toAccount = "********@qq.com";//发件人的 邮箱 和 密码
final String fromAccount = "**********@163.com";final String fromPassword = "**********";//指定发送邮件的主机
final String host = "smtp.163.com";//创建参数配置, 获取系统属性
final Properties properties =System.getProperties();
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");//根据配置创建会话对象,获取默认session对象
final Session session = Session.getDefaultInstance(properties,newAuthenticator(){
@OverridepublicPasswordAuthentication getPasswordAuthentication()
{return new PasswordAuthentication(fromAccount, fromPassword); //发件人邮件用户名、密码
}
});
session.setDebug(true);final String xmlPath="xml";final String zipPath=tozip(xmlPath,"zip","person");
CreateMessage(session,fromAccount,toAccount,xmlPath,zipPath);
}
}