springboot工程发送腾讯企业邮箱

抄的,参考http://www.cnblogs.com/LUA123/p/5575134.html 

public class SendSpEmailUtil {
	//用户名密码验证,需要实现抽象类Authenticator的抽象方法PasswordAuthentication
	  static class MyAuthenricator extends Authenticator {
	      String u = null;
	      String p = null;

	      public MyAuthenricator(String u, String p) {
	          this.u = u;
	          this.p = p;
	      }

	      @Override
	      public PasswordAuthentication getPasswordAuthentication() {
	          return new PasswordAuthentication(u, p);
	      }
	  }
	  
	  /**
	   * 
	   * @param subject主题
	   * @param text内容
	   * @param receive接收人
	   */
	  public static void send(String from,String to,String subject,String text,String filePath){
	  	Properties prop = new Properties();
	      //协议
	      prop.setProperty("mail.transport.protocol", "smtp");
	      //服务器
	      prop.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
	      //端口
	      prop.setProperty("mail.smtp.port", "465");
	      //使用smtp身份验证
	      prop.setProperty("mail.smtp.auth", "true");
	      //使用SSL,企业邮箱必需!
	      //开启安全协议
	      MailSSLSocketFactory sf = null;
	      try {
	          sf = new MailSSLSocketFactory();
	          sf.setTrustAllHosts(true);
	      } catch (GeneralSecurityException e) {
	          e.printStackTrace();
	      }
	      prop.put("mail.smtp.ssl.enable", "true");
	      prop.put("mail.smtp.ssl.socketFactory", sf);
	      Session session = Session.getInstance(prop, new MyAuthenricator("[email protected]", "xxxxxxx"));
	      session.setDebug(false);
	      MimeMessage mimeMessage = new MimeMessage(session);
	      try {
	          Address add = new InternetAddress(to);
	          mimeMessage.setFrom(new InternetAddress("[email protected]", "小菜菜"));
	          mimeMessage.addRecipient(Message.RecipientType.TO, add);
	          mimeMessage.setSubject(subject);
	          mimeMessage.setSentDate(new Date());
	          Multipart multipart = new MimeMultipart();
	          MimeBodyPart part = new MimeBodyPart();
	          BodyPart part1 = new MimeBodyPart();
              part1.setContent(text, "text/html;charset=utf-8");//设置邮件文本内容  
              multipart.addBodyPart(part1);  
              part = new MimeBodyPart(); 
              part.attachFile(filePath);
              multipart.addBodyPart(part);
              mimeMessage.setContent(multipart); 
	          mimeMessage.saveChanges();
	          Transport.send(mimeMessage);
	      } catch (Exception e) {
	          e.printStackTrace();
	      }
	  }
}

 

你可能感兴趣的:(springboot工程发送腾讯企业邮箱)