Guest User

Untitled

a guest
Nov 5th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. -----------------------------------------------配置文件里mail的配置信息------------------------------------------
  2. mail.host = 192.168.1.1
  3. mail.username = wang
  4. mail.password = 12345
  5. mail.from = 邮箱地址@qq.com
  6.  
  7. ------------------------------------------------java 最外层-----------------------------------------------------
  8.  
  9. @Value("#{'${mail.host:''}'}")
  10. private String MAIL_HOST;
  11. @Value("#{'${mail.username:''}'}")
  12. private String MAIL_USER_NAME;
  13. @Value("#{'${mail.password:''}'}")
  14. private String MAIL_PASSWORD;
  15. @Value("#{'${mail.from:''}'}")
  16. private String MAIL_FROM;
  17.  
  18.  
  19.  
  20. -------------------------------------java调用--------------------------------------------------------------------
  21.  
  22.  
  23.  
  24. sendMailFile(发送给谁的邮件地址@qq.com, cc抄送给谁,title标题 , 邮件内容msg,实体类declstateList);
  25.  
  26.  
  27. -------------------------------------java方法--------------------------------------------------------------------
  28. public void sendMailFile(String to, String cc, String subject, String content,DeclstateList declstateList)
  29. throws AddressException, MessagingException {
  30. Properties properties = new Properties();
  31. properties.setProperty("mail.smtp.auth", "true");// 服务器需要认证
  32. properties.setProperty("mail.transport.protocol", "smtp");// 声明发送邮件使用的端口
  33. properties.setProperty("mail.host", MAIL_HOST);// 发送邮件的服务器地址
  34.  
  35. Session session = Session.getInstance(properties, new Authenticator() {
  36. protected PasswordAuthentication getPasswordAuthentication() {
  37. return new PasswordAuthentication(MAIL_USER_NAME, MAIL_PASSWORD);
  38. }
  39. });
  40. session.setDebug(true);// 在后台打印发送邮件的实时信息
  41.  
  42. Message message = new MimeMessage(session);
  43. message.setFrom(new InternetAddress(MAIL_FROM));
  44. message.setSubject(subject);// 设置主题
  45. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));// 发送
  46.  
  47. // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
  48. Multipart multipart = new MimeMultipart();
  49. // 添加邮件正文
  50. BodyPart contentPart = new MimeBodyPart();
  51. contentPart.setContent(content, "text/html;charset=UTF-8");
  52. multipart.addBodyPart(contentPart);
  53.  
  54. //判断服务器的系统
  55. String osName = System.getProperty("os.name");
  56. String path = "";
  57. if (osName == null)
  58. osName = "";
  59. if (osName.toLowerCase().indexOf("win") != -1){
  60. path = "C:/test1/declstateList/"+declstateList.getId();
  61. }else{
  62. path = "/opt/declstateList/"+declstateList.getId();
  63. }
  64.  
  65. File filePath = new File(path);//File类型可以是文件也可以是文件夹
  66. if(!filePath.exists()){
  67. filePath.mkdirs();
  68. }
  69.  
  70. File[] fileList = filePath.listFiles();//将该目录下的所有文件放置在一个File类型的数组中
  71.  
  72. for (int i = 0; i < fileList.length; i++) {
  73. File file = fileList[i];
  74.  
  75. //添加附件的内容
  76. if (file != null) {
  77. BodyPart attachmentBodyPart = new MimeBodyPart();
  78. DataSource source = new FileDataSource(file);
  79. attachmentBodyPart.setDataHandler(new DataHandler(source));
  80.  
  81. // 网上流传的解决文件名乱码的方法,其实用MimeUtility.encodeWord就可以很方便的搞定
  82. // 这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码
  83. //sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
  84. //messageBodyPart.setFileName("=?GBK?B?" + enc.encode(attachment.getName().getBytes()) + "?=");
  85.  
  86. //MimeUtility.encodeWord可以避免文件名乱码
  87. try {
  88. attachmentBodyPart.setFileName(MimeUtility.encodeWord(file.getName()));
  89. } catch (UnsupportedEncodingException e) {
  90. // TODO Auto-generated catch block
  91. e.printStackTrace();
  92. }
  93. multipart.addBodyPart(attachmentBodyPart);
  94. }
  95. }
  96. // 将multipart对象放到message中
  97. message.setContent(multipart);
  98. // 保存邮件
  99. message.saveChanges();
  100. Transport.send(message);// 发送邮件
  101. }
Add Comment
Please, Sign In to add comment