<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>1.5</version> </dependency>
// 使用Java Mail API Properties prop = new Properties(); prop.setProperty("mail.host", "smtp.qq.com"); prop.setProperty("mail.transport.protocol", "smtp"); prop.setProperty("mail.smtp.auth", "true"); // 创建session Session session = Session.getInstance(prop); // Session的debug模式,设置为true能够查看到程序发送Email的运行状态 // session.setDebug(true); Transport ts = session.getTransport(); // 以qq邮箱为例,需要开启POP3/SMTP服务,生成授权码供connect时使用 ts.connect("from@qq.com", "xxxxxxxxx"); // 创建邮件对象 MimeMessage message = new MimeMessage(session); message.setFrom("from@qq.com"); message.setRecipient(Message.RecipientType.TO, new InternetAddress("to@163.com")); message.setSubject("javamail简单文本邮件"); message.setContent("这是一封javamail发送的简单邮件", "text/html;charset=UTF-8");
// 发送 ts.sendMessage(message, message.getAllRecipients()); ts.close();
|