java压缩文件并加密,发送到邮箱

您所在的位置:网站首页 如何打包文件夹发送到邮箱 java压缩文件并加密,发送到邮箱

java压缩文件并加密,发送到邮箱

2024-01-16 15:03| 来源: 网络整理| 查看: 265

日常记录

目标,我们需要把文件进行压缩 并进行加密设置密码,并发送到指定的邮箱,这是需求

   首先把工具类贴出来

我们需要导入一个jar包 winzipaes-1.0.1.jar

上传了一下,告诉已经存在了所以,有看到的去找下吧, 应该很好找的

用法在下面 import de.idyl.winzipaes.AesZipFileDecrypter; import de.idyl.winzipaes.AesZipFileEncrypter; import de.idyl.winzipaes.impl.AESDecrypterBC; import de.idyl.winzipaes.impl.AESEncrypterBC; import de.idyl.winzipaes.impl.ExtZipEntry; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.Message.RecipientType; import javax.mail.internet.*; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; import java.util.zip.DataFormatException; /** * 发送邮件(适用qq邮箱) * @author miaoch * */ public class MailUtils{ //发送的邮箱 内部代码只适用qq邮箱 private String user ;//= ""; //授权密码 通过QQ邮箱设置->账户->POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务->开启POP3/SMTP服务获取 private String pwd ;//= ""; private String port ; private String host; public MailUtils(String user, String pwd, String port, String host) { this.user = user; this.pwd = pwd; this.port = port; this.host = host; } public MailUtils() { super(); } private String[] to; private String[] cc;//抄送 private String[] bcc;//密送 private String[] fileList;//附件 private String subject;//主题 private String content;//内容,可以用html语言写 public void sendMessage() throws Exception{ // 配置发送邮件的环境属性 final Properties props = new Properties(); //下面两段代码是设置ssl和端口,不设置发送不出去。 props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); //props.setProperty("mail.smtp.port", "465"); //163邮箱的端口是25 props.setProperty("mail.smtp.socketFactory.port", port); // 表示SMTP发送邮件,需要进行身份验证 props.setProperty("mail.transport.protocol", "smtp");// 设置传输协议 props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", host);//QQ邮箱的服务器 如果是企业邮箱或者其他邮箱得更换该服务器地址 // 发件人的账号 //smtp.163.com =163 端口25 //smtp.qq.com =QQ 端口465 props.put("mail.user", user); // 访问SMTP服务时需要提供的密码 props.put("mail.password", pwd); // 构建授权信息,用于进行SMTP进行身份验证 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { // 用户名、密码 String userName = props.getProperty("mail.user"); String password = props.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; // 使用环境属性和授权信息,创建邮件会话 Session mailSession = Session.getInstance(props, authenticator); // 创建邮件消息 //开启debug模式 mailSession.setDebug(true); MimeMessage message = new MimeMessage(mailSession); BodyPart messageBodyPart = new MimeBodyPart(); Multipart multipart = new MimeMultipart(); // 设置发件人 InternetAddress form = new InternetAddress( props.getProperty("mail.user")); message.setFrom(form); //发送 if (to != null) { String toList = getMailList(to); InternetAddress[] iaToList = new InternetAddress().parse(toList); message.setRecipients(RecipientType.TO, iaToList); // 收件人 } //抄送 if (cc != null) { String toListcc = getMailList(cc); InternetAddress[] iaToListcc = new InternetAddress().parse(toListcc); message.setRecipients(RecipientType.CC, iaToListcc); // 抄送人 } //密送 if (bcc != null) { String toListbcc = getMailList(bcc); InternetAddress[] iaToListbcc = new InternetAddress().parse(toListbcc); message.setRecipients(RecipientType.BCC, iaToListbcc); // 密送人 } message.setSentDate(new Date()); // 发送日期 该日期可以随意写,你可以写上昨天的日期(效果很特别,亲测,有兴趣可以试试),或者抽象出来形成一个参数。 message.setSubject(subject); // 主题 message.setText(content); // 内容 //显示以html格式的文本内容 messageBodyPart.setContent(content,"text/html;charset=utf-8"); multipart.addBodyPart(messageBodyPart); //保存多个附件 if(fileList!=null){ addTach(fileList, multipart); } message.setContent(multipart); // 发送邮件 Transport.send(message); } public void setTo(String[] to) { this.to = to; } public void setCc(String[] cc) { this.cc = cc; } public void setBcc(String[] bcc) { this.bcc = bcc; } public void setSubject(String subject) { this.subject = subject; } public void setContent(String content) { this.content = content; } public void setFileList(String[] fileList) { this.fileList = fileList; } private String getMailList(String[] mailArray) { StringBuffer toList = new StringBuffer(); int length = mailArray.length; if (mailArray != null && length < 2) { toList.append(mailArray[0]); } else { for (int i = 0; i < length; i++) { toList.append(mailArray[i]); if (i != (length - 1)) { toList.append(","); } } } return toList.toString(); } //添加多个附件 public void addTach(String fileList[], Multipart multipart) throws MessagingException, UnsupportedEncodingException { for (int index = 0; index < fileList.length; index++) { MimeBodyPart mailArchieve = new MimeBodyPart(); FileDataSource fds = new FileDataSource(fileList[index]); mailArchieve.setDataHandler(new DataHandler(fds)); mailArchieve.setFileName(MimeUtility.encodeText(fds.getName(),"UTF-8","B")); multipart.addBodyPart(mailArchieve); } } public static boolean sendMail(String subject,String content,String[] target,String[] copy,String[] secret,String[] fileList){ boolean sccessOrError=false; MailUtils mail = new MailUtils(); mail.setSubject(subject); mail.setContent(content); //收件人 可以发给其他邮箱(163等) 下同 mail.setTo(target); //抄送 mail.setCc(copy); //密送 mail.setBcc(secret); //发送附件列表 可以写绝对路径 也可以写相对路径(起点是项目根目录) mail.setFileList(fileList); //发送邮件 try { mail.sendMessage(); System.out.println("发送邮件成功!"); sccessOrError=true; } catch (Exception e) { System.out.println("发送邮件失败!"); e.printStackTrace(); return sccessOrError; } return sccessOrError; } public static boolean sendMail(String subject,String content,String[] target,String[] fileList){ boolean sccessOrError=false; MailUtils mail = new MailUtils(); mail.setSubject(subject); mail.setContent(content); //收件人 可以发给其他邮箱(163等) 下同 mail.setTo(target); /*抄送 mail.setCc(copy); //密送 mail.setBcc(secret);*/ //发送附件列表 可以写绝对路径 也可以写相对路径(起点是项目根目录) mail.setFileList(fileList); //发送邮件 try { String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date()); System.out.println("开始发送邮件,开始时间:"+startTime); mail.sendMessage(); String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date()); System.out.println("发送邮件成功!结束时间:"+endTime); sccessOrError=true; } catch (Exception e) { System.out.println("发送邮件失败!"); e.printStackTrace(); return sccessOrError; } return sccessOrError; } public static boolean sendMail(String subject,String content,String[] target){ boolean sccessOrError=false; MailUtils mail = new MailUtils(); mail.setSubject(subject); mail.setContent(content); //收件人 可以发给其他邮箱(163等) 下同 mail.setTo(target); /*抄送 mail.setCc(copy); //密送 mail.setBcc(secret); //发送附件列表 可以写绝对路径 也可以写相对路径(起点是项目根目录) mail.setFileList(fileList); */ //发送邮件 try { String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date()); System.out.println("开始发送邮件,开始时间:"+startTime); mail.sendMessage(); String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date()); System.out.println("发送邮件成功!结束时间:"+endTime); sccessOrError=true; } catch (Exception e) { System.out.println("发送邮件失败!"); e.printStackTrace(); return sccessOrError; } return sccessOrError; } //以下是演示demo /* public static void main(String args[]) { MailUtils mail = new MailUtils(); mail.setSubject("这个是标题"); mail.setContent("这个是内容"); //收件人 可以发给其他邮箱(163等) 下同 mail.setTo(new String[] {"[email protected]"}); //抄送 mail.setCc(new String[] {"[email protected]","[email protected]"}); //密送 mail.setBcc(new String[] {"[email protected]","[email protected]"}); //发送附件列表 可以写绝对路径 也可以写相对路径(起点是项目根目录) mail.setFileList(new String[] {"C:\\Users\\67545\\Desktop\\img\\12312321.out"}); //发送邮件 try { mail.sendMessage(); System.out.println("发送邮件成功!"); } catch (Exception e) { System.out.println("发送邮件失败!"); e.printStackTrace(); } }*/ /** * 压缩单个文件并加密 * @param srcFile 待压缩的文件 * @param desFile 生成的目标文件 * @param passWord 压缩文件加密密码 * @throws IOException */ public static void zipFile(String srcFile,String desFile,String passWord) throws IOException{ AesZipFileEncrypter.zipAndEncrypt(new File(srcFile),new File(desFile), passWord, new AESEncrypterBC()); } /** * 给指定的压缩文件进行加密 * @param srcZipFile 待加密的压缩文件 * @param desFile 加密后的目标压缩文件 * @param passWord 压缩文件加密密码 * @throws IOException */ public static void encryptZipFile(String srcZipFile,String desFile,String passWord) throws IOException{ AesZipFileEncrypter.zipAndEncryptAll(new File(srcZipFile), new File(desFile), passWord, new AESEncrypterBC()); } /** * 解密抽取压缩文件中的某个文件 * @param srcZipFile 加密的压缩文件 * @param extractFileName 抽取压缩文件中的某个文件的名称 * @param desFile 解压后抽取后生成的目标文件 * @param passWord 解压密码 * @throws IOException * @throws DataFormatException */ public static void decrypterZipFile(String srcZipFile,String extractFileName,String desFile,String passWord)throws IOException, DataFormatException{ AesZipFileDecrypter zipFile = new AesZipFileDecrypter( new File(srcZipFile),new AESDecrypterBC()); ExtZipEntry entry = zipFile.getEntry(extractFileName); zipFile.extractEntry( entry, new File(desFile),passWord); } public static void main(String[] args) throws Exception { String srcFile="F:\\WJ\\JiYue\\sunpay\\sp_boss\\src\\java\\com\\core\\common\\util/test.txt"; String desFile="test.zip"; zipFile(srcFile, desFile,"123qwe"); } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getPort() { return port; } public void setPort(String port) { this.port = port; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } }

下面介绍一下用法

直接调用方法即可,第一个参数是你的文件名称,第二个事压缩完后的文件名称,第三个是压缩加密的密码

最好是在项目的根目录 MailUtils.zipFile( fileNameTxt, fileNameZip, password); 如果不在项目根目录的话打开压缩文件会出现你服务器的路径名称 def sendB = sendMail(subject, content, target, fileList)

开始发送邮件

subject  邮箱的标题

content 邮件的内容 ,就跟写页面一样一样一样的

target 是一个数组, 发送的目标邮箱,如:target=['[email protected]','[email protected]','[email protected]']  可以多个一起发送,也可以只发一个 fileList    也是一个数组,发送所附带的附件,也可以是多个,

如:target=['路径','路径','路径'] 

这是发送的方法 可以自行修改成符合自己需求的 public static boolean sendMail(String subject, String content, String[] target, String[] fileList) { boolean sccessOrError = false; MailUtils mail = new MailUtils(user, pwd, port, host); mail.setSubject(subject); mail.setContent(content); //收件人 可以发给其他邮箱(163等) 下同 mail.setTo(target); /*抄送 mail.setCc(copy); //密送 mail.setBcc(secret);*/ //发送附件列表 可以写绝对路径 也可以写相对路径(起点是项目根目录) mail.setFileList(fileList); //发送邮件 try { String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date()); System.out.println("开始发送邮件,开始时间:" + startTime); mail.sendMessage(); String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date()); System.out.println("发送邮件成功!结束时间:" + endTime); sccessOrError = true; } catch (Exception e) { System.out.println("发送邮件失败!"); e.printStackTrace(); return sccessOrError; } return sccessOrError; }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3