邮件发送
需要的jar包:
- activation-1.1.1.jar
- mail-1.4.7.jar
QQ邮箱设置开启POP3/SMTP服务,并获得授权码
java实现简单邮件发送
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class Mail1 {
public static void main(String[] args) throws Exception {
//要发送邮件,需要获得协议和支持!开启服务POP3/SMTP服务 授权码: fsxqgovorymigfeb
Properties prop=new Properties();
prop.setProperty("mail.host","smtp.qq.com");//设置QQ邮件服务器
prop.setProperty("mail.transport.protocol","smtp");//设置邮箱发送协议
prop.setProperty("mail.smtp.auth","true");//需要验证用户名密码
//QQ邮箱还有设置SSL加密
MailSSLSocketFactory sf=new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable","true");
prop.put("mail.smtp.ssl.socketFactory",sf);
//1.创建定义整个应用程序所需要的环境信息的Session对象
Session session=Session.getDefaultInstance(prop, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("1369410772@qq.com","fsxqgovorymigfeb");
}
});
//开启session的debug模式,这样就可以查看运行状态了
session.setDebug(true);
//2.通过session对象获得transport对象
Transport transport = session.getTransport();