1.导包
<dependency><groupId>com.sun.mail</groupId><artifactId>javax.mail</artifactId><version>1.6.2</version></dependency>
2.开启POP3/SMTP服务,然后获取16位授权码
3.拷贝修改代码
@Testvoid testSendMail() throws Exception {//创建一个配置文件并保存Properties properties = new Properties();properties.setProperty("mail.host","");properties.setProperty("mail.transport.protocol","smtp");properties.setProperty("mail.smtp.auth","true");//QQ存在一个特性设置SSL加密MailSSLSocketFactory sf = new MailSSLSocketFactory();sf.setTrustAllHosts(true);properties.put("mail.smtp.ssl.enable", "true");properties.put("mail.smtp.ssl.socketFactory", sf);//创建一个session对象Session session = Session.getDefaultInstance(properties, new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication("2223503679@","16位授权码");}});//开启debug模式session.setDebug(true);//获取连接对象Transport transport = session.getTransport();//连接服务器transport.connect("","2223503679@","16位授权码");//创建邮件对象MimeMessage mimeMessage = new MimeMessage(session);//邮件发送人mimeMessage.setFrom(new InternetAddress("2223503679@"));//邮件接收人mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("ntxz123@"));//邮件标题mimeMessage.setSubject("液位警告!!");//邮件内容mimeMessage.setContent("当前系统液位值2.0cm低于设定的警戒值2.1cm,请注意及时补充灌溉用水!!!","text/html;charset=UTF-8");//发送邮件transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());//关闭连接transport.close();}
4.想要发送其他格式请参照下方博客
/baolingye/article/details/96598222