添加依赖
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
发送邮件工具类
public class EmailUtil {
public static void send(String recipient, String subject, String content) {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.qq.com");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.debug", "true");
Session session = Session.getInstance(props);
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress("369950806@qq.com"));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
msg.setSubject(subject);
msg.setText(content);
Transport tran = session.getTransport();
tran.connect("369950806", "ajhyxkqvapmzbggi");
tran.sendMessage(msg, msg.getAllRecipients());
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String subject = "来自微风商城的激活邮箱";
String content = "点击如下连接激活账户,如果不能点击请复制到浏览器地址栏访问:http://www.wego.com/userServlet?op=active&code=" + WeGoUtil.genCheckCode("asdf123XXX");
EmailUtil.send("3433698914@qq.com", subject, content);
}
}