Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------------------------配置文件里mail的配置信息------------------------------------------
- mail.host = 192.168.1.1
- mail.username = wang
- mail.password = 12345
- mail.from = 邮箱地址@qq.com
- ------------------------------------------------java 最外层-----------------------------------------------------
- @Value("#{'${mail.host:''}'}")
- private String MAIL_HOST;
- @Value("#{'${mail.username:''}'}")
- private String MAIL_USER_NAME;
- @Value("#{'${mail.password:''}'}")
- private String MAIL_PASSWORD;
- @Value("#{'${mail.from:''}'}")
- private String MAIL_FROM;
- -------------------------------------java调用--------------------------------------------------------------------
- sendMailFile(发送给谁的邮件地址@qq.com, cc抄送给谁,title标题 , 邮件内容msg,实体类declstateList);
- -------------------------------------java方法--------------------------------------------------------------------
- public void sendMailFile(String to, String cc, String subject, String content,DeclstateList declstateList)
- throws AddressException, MessagingException {
- Properties properties = new Properties();
- properties.setProperty("mail.smtp.auth", "true");// 服务器需要认证
- properties.setProperty("mail.transport.protocol", "smtp");// 声明发送邮件使用的端口
- properties.setProperty("mail.host", MAIL_HOST);// 发送邮件的服务器地址
- Session session = Session.getInstance(properties, new Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(MAIL_USER_NAME, MAIL_PASSWORD);
- }
- });
- session.setDebug(true);// 在后台打印发送邮件的实时信息
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress(MAIL_FROM));
- message.setSubject(subject);// 设置主题
- message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));// 发送
- // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
- Multipart multipart = new MimeMultipart();
- // 添加邮件正文
- BodyPart contentPart = new MimeBodyPart();
- contentPart.setContent(content, "text/html;charset=UTF-8");
- multipart.addBodyPart(contentPart);
- //判断服务器的系统
- String osName = System.getProperty("os.name");
- String path = "";
- if (osName == null)
- osName = "";
- if (osName.toLowerCase().indexOf("win") != -1){
- path = "C:/test1/declstateList/"+declstateList.getId();
- }else{
- path = "/opt/declstateList/"+declstateList.getId();
- }
- File filePath = new File(path);//File类型可以是文件也可以是文件夹
- if(!filePath.exists()){
- filePath.mkdirs();
- }
- File[] fileList = filePath.listFiles();//将该目录下的所有文件放置在一个File类型的数组中
- for (int i = 0; i < fileList.length; i++) {
- File file = fileList[i];
- //添加附件的内容
- if (file != null) {
- BodyPart attachmentBodyPart = new MimeBodyPart();
- DataSource source = new FileDataSource(file);
- attachmentBodyPart.setDataHandler(new DataHandler(source));
- // 网上流传的解决文件名乱码的方法,其实用MimeUtility.encodeWord就可以很方便的搞定
- // 这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码
- //sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
- //messageBodyPart.setFileName("=?GBK?B?" + enc.encode(attachment.getName().getBytes()) + "?=");
- //MimeUtility.encodeWord可以避免文件名乱码
- try {
- attachmentBodyPart.setFileName(MimeUtility.encodeWord(file.getName()));
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- multipart.addBodyPart(attachmentBodyPart);
- }
- }
- // 将multipart对象放到message中
- message.setContent(multipart);
- // 保存邮件
- message.saveChanges();
- Transport.send(message);// 发送邮件
- }
Add Comment
Please, Sign In to add comment