首先添加maven依赖包
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>1.4</version> </dependency>
写一下Eamil类
import org.apache.commons.mail.*; public class Email { /** * @describe 发送内容为简单文本的邮件 * @throws EmailException */ public static void sendSimpleTextEmail(String email_text) throws EmailException { SimpleEmail email = new SimpleEmail(); //设置主机名,QQ邮箱是"smtp.qq.com",网易邮箱是"smtp.163.com" email.setHostName("smtp.163.com"); // 用户名和密码为邮箱的账号和授权码(不需要进行base64编码) email.setAuthenticator(new DefaultAuthenticator("xxxxxxxx@163.com", "此处写授权码")); //设置SSL连接,这样写就对了 email.setSSLOnConnect(true); //设置来源,就是发送方的邮箱地址 email.setFrom("xxxxxxxx@163.com"); //设置主题,可以不设置 email.setSubject("java发送邮件"); //设置信息,就是内容,这个必须要有 email.setMsg("这是测试邮件 ... :-)"); //接收人邮箱地址 email.addTo("2074296104@qq.com"); email.send(); } }
测试图