pom.xml添加
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.5.0-b01</version> </dependency>测试类
package com.ss.test; import java.util.Properties; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import com.sun.mail.util.MailSSLSocketFactory; public class EmailUtil { public static boolean send(String to,String subject,String msg){ Properties props = new Properties(); //邮件传输的协议 props.put("mail.transport.protocol", "smtp"); //连接的邮件服务器 props.put("mail.host","smtp.qq.com"); //发送人 props.put("mail.from","2172738702@qq.com"); //第一步:创建session Session session = Session.getDefaultInstance(props); session.setDebug(true); try { MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.socketFactory", "true"); //第二步:获取邮件传输对象 Transport ts= session.getTransport(); //连接邮件服务器 ts.connect("2172738702@qq.com", "sqgvewwicqdbecea"); //第三步:创建邮件消息体 MimeMessage message = new MimeMessage(session); //设置邮件的内容 message.setSubject(subject); //设置邮件的内容 message.setContent(msg,"text/html;charset=utf-8"); //第四步:设置发送昵称 String nick=""; nick = javax.mail.internet.MimeUtility.encodeText("Miss.ss"); message.setFrom(new InternetAddress(nick+"'<2172738702@qq.com>'")); //第五步:设置接收人信息 ts.sendMessage(message, InternetAddress.parse(to)); } catch (Exception ex) { // TODO Auto-generated catch block ex.printStackTrace(); } return false; } public static void main(String[] args) { EmailUtil.send("1484901172@qq.com", "一封信","恭喜测试成功!哈哈哈"); } }测试成功,(^__^) 嘻嘻…… 期间也遇到错误,参考链接:http://blog.csdn.net/qq_29011851/article/details/68065288