所需架包:mail-1.4.7.jar
public class EmailUtil {
public static void main(String[] args) {
// TODO Auto-generated method stub
final Properties propt = new Properties();//得到配置对象
propt.put("mail.smtp.auth", "true"); //确定邮件发送的身份验证
propt.put("mail.smtp.host", "smtp.qq.com");//设置发送服务器
propt.put("mail.user", "1474276172@qq.com");//设置qq邮箱发送的账号
propt.put("mail.pssword", "rvjprvwbpkawiejf");//设置qq邮箱发送的授权码
//对信息进行验证
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
//用户名密码验证
String userName = propt.getProperty("mail.user");
String password = propt.getProperty("mail.pssword");
//验证
return new PasswordAuthentication(userName, password);
}
};
Session session = Session.getInstance(propt, authenticator);//设置授权信息
MimeMessage message = new MimeMessage(session);//写邮件发送的内容
//设置发送
try {
InternetAddress from = new InternetAddress(propt.getProperty("mail.user"));
message.setFrom(from);
//设置标题内容
message.setSubject("粥粥");
message.setContent("第五次使用Java技术发送邮件测试————(小凳子收)","text/html;charset=utf-8");
//设置接收人
InternetAddress to = new InternetAddress("2295713129@qq.com");
message.setRecipient(RecipientType.TO, to);
//开始发送
Transport.send(message);
System.out.println("发送成功!!!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("发送失败!!!");
}
}
}