Java Mail

public class MailSenderUtil { public static void send(String title,String []to,String text,List<File> files)throws Exception { JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
      mailSender.setHost("smpt.qq.com");
      mailSender.setPort(25);
      mailSender.setUsername("username");//个人邮箱
      mailSender.setPassword("password");//邮箱密码
      mailSender.setDefaultEncoding("UTF-8");
      
      Properties javaMailProperties = new Properties();
      javaMailProperties.setProperty("mail.smtp.sendpartial", "true");
      javaMailProperties.setProperty("mail.smtp.auth", "true");
      javaMailProperties.setProperty("mail.smtp.socketFactory.port", "465");
      javaMailProperties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      
      mailSender.setJavaMailProperties(javaMailProperties);
      
       MimeMessage msg = mailSender.createMimeMessage();
       MimeMessageHelper helper = new MimeMessageHelper(msg, true);
       helper.setFrom(mailSender.getUsername());
       helper.setTo(to);
       helper.setSubject(title);
       helper.setText(text);
       
       if (!CollectionUtil.isEmpty(files)) { for (File file : files) { helper.addAttachment(file.getName(), file);
         } } mailSender.send(msg);
   } public static void main(String[] args) throws Exception{ String []MAIL_CC_LIST={"[email protected]","[email protected]"};
      File file1 = new File("D:\\test.csv");
      File file2 = new File("D:\\test.xls");
      List<File> files = new ArrayList<File>();
      files.add(file1);
      files.add(file2);
      
      
      send("test", MAIL_CC_LIST, "my test",files);
   }

你可能感兴趣的:(Java Mail)