1.导包
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
2.外网qq邮箱、新浪邮箱
注意点:连接需要生成授权码
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");//发送邮件协议
properties.setProperty("mail.smtp.auth", "true");//需要验证
//properties.setProperty("mail.debug", "true");//设置debug模式 后台输出邮件发送的过程
Session session = Session.getInstance(properties);
session.setDebug(true);//debug模式(控制台)
//邮件信息
Message messgae = new MimeMessage(session);
messgae.setFrom(new InternetAddress("邮箱名@qq.com"));//设置发送人
messgae.setText("你的验证码为:"+1111+"。请注意,验证码有效时间为2分钟!!!");//设置邮件内容
messgae.setSubject("邮箱验证");//设置邮件主题
//发送邮件
Transport tran = session.getTransport();
//tran.connect("smtp.sina.com", 25, "邮箱账户", "邮箱密码");//连接到新浪邮箱服务器(端口25)y
tran.connect("smtp.qq.com",587, "发送人账号(qq)", "授权登录码");//连接到QQ邮箱服务器(端口587)
tran.sendMessage(messgae, new Address[]{ new InternetAddress("2731738783@qq.com")});//设置邮件接收人
System.out.println("邮件发送成功");
tran.close();
2.内网安宁邮件
注意点:可能需要邮件服务器加入服务器IP
Boolean flag = false;
String host = "服务器地址";
Properties properties = new Properties();
//设置smtp协议(发送邮件协议)
properties.put("mail.transport.protocol", "SMTP");
//设置服务器连接地址,向后兼容
properties.put("mail.smtp.host", host);
//设置身份校验
properties.put("mail.smtp.auth", "true");
//设置时间
properties.put("mail.smtp.timeout",3000);//接收
properties.put("mail.smtp.connectiontimeout",3000);
properties.put("mail.smtp.writetimeout",3000);//发送
//设置默认端口号,向后兼容
properties.put("mail.smtp.port", 465);
//此句很重要,把当前smtp host设为可信任地址
properties.put("mail.smtp.ssl.trust", host);
MailSSLSocketFactory sf;
try{
sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.socketFactory",sf);
}catch(GeneralSecurityExeception el){
el.printStacktrace();
}
//开启SSL
properties.put("mail.smtp.starttls.enable","true");
properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
//使用环境属性和授权信息,创建邮件会话
Session session = Session.getDefaultInstance(properties);
//控制台打印日志
session.setDebug(true);
String msgtest = "发送内容";
//创建邮件消息
Message msg = new MimeMessage(session);
//设置发件人
InternetAddress from = new InternetAddress("发送人邮箱");
msg.setFrom(from);
String [] to= {"发送地址一","发送地址一"};
InternetAddress toAddressString = to[i]+"后缀";
InternetAddress toAddress = InternetAddress(toAddressString);
//设置收件人的邮箱
msg.setRecipients(RecipientType.TO, toAddress);
//设置邮件标题
msg.setSubject("标题");
//设置发送时间
msg.setSentDate(new Date());
//设置邮件的内容体
BodyPart mbp = new MimeBodyPart();
mbp.setContent("发送内容", "text/html;charset=utf-8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mbp);
msg.setContent(multipart);
//保存设置
msg.saveChanges();
flag = true;
System.setProperty("java.net.preferIPV4Stack","true");
try{
//建立邮件传输对象
Transport transport = session.getTransport("smtp");
//与服务端建立连接,此处跟发送人的地址一样
transport.connect("IP地址","admin@cams.net", "123456");
//发送邮件
transport.sendMessage(msg, msg.getAllRecipients());
//关闭
transport.close();
System.out.println("发送邮件成功!!!");
}catch(Exception e){
e.printStackTrace();
System.out.println("发送邮件失败!!!");
}