1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
public void sendEmail(String message,String title,String toAddress){ Properties props = new Properties(); props.setProperty("mail.debug", "false"); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.host", "smtp.qq.com"); props.setProperty("mail.transport.protocol", "smtp"); MailSSLSocketFactory sf; Transport transport =null; try { sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.socketFactory", sf); Session session = Session.getInstance(props); Message msg = new MimeMessage(session); msg.setSubject(title); StringBuilder builder = new StringBuilder(); builder.append(message); builder.append("\n\n 时间: " + DateUtil.getTime());
msg.setFrom(new InternetAddress("你的邮箱地址")); msg.setContent(builder.toString(), "text/html;charset=utf-8"); transport = session.getTransport(); transport.connect("smtp.qq.com", "你的邮箱地址", "你的授权码"); transport.sendMessage(msg, new Address[] { new InternetAddress(toAddress)}); } catch (GeneralSecurityException e) { log.info("ssl 验证错误"); e.printStackTrace(); }catch (Exception e) { log.info("程序异常", e); }finally{ if(transport != null){ try { transport.close(); } catch (MessagingException e) { log.info("transport 关闭异常", e); e.printStackTrace(); } } } log.info("邮件发送成功!"); }
|