总结(QQ邮箱为例):
1.使用第三方邮箱,需在qq邮箱,设置->账户设置授权码,以授权码作为QQ邮箱登录密码。
2.接受服务器类型POP3
接收邮件服务器:pop.qq.com 端口:995 使用SSL连接服务器:true
发送邮件服务器:smtp.qq.com 端口:25 使用SSL连接服务器:true
(pop.qq.com:为个人邮箱;pop.exmail.qq.com:为企业邮箱)
3.properties.setProperty("mail.smtp.starttls.enable", "true");
必须加上,否则会报错(A secure connection is requiered(such as ssl))
=============================================================
//import javax.mail.*;
public void senderEmailForPhone(TblEmailInfo mailInfo){
try{
BASE64Decoder decoder = new BASE64Decoder();
PasswordAuthenticator authenticator = new PasswordAuthenticator(mailInfo.getEmail(),mailInfo.getPassword());
//获得邮件会话属性
Properties properties = new Properties();
properties.setProperty("mail.smtp.host", mailInfo.getSendHost());//smtp.qq.com
properties.setProperty("mail.smtp.port", String.valueOf(mailInfo.getSendHostPort()));//25
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.starttls.enable", "true");
// 创建Session实例对象
Session session = Session.getInstance(properties, authenticator);
// 创建MimeMessage实例对象
Message message = new MimeMessage(session);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getEmailInfoSendEmail());
message.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address[] tos = null;
String[] receivers = mailInfo.getEmailInfoSendTo().split(",");
if (receivers != null){
// 为每个邮件接收者创建一个地址
tos = new InternetAddress[receivers.length + 1];
tos[0] = new InternetAddress(mailInfo.getEmailInfoSendTo());
for (int i=0; i<receivers.length; i++){
tos[i+1] = new InternetAddress(receivers[i]);
}
} else {
tos = new InternetAddress[1];
tos[0] = new InternetAddress(mailInfo.getEmailInfoSendTo());
}
// 将所有接收者地址都添加到邮件接收者属性中
message.setRecipients(Message.RecipientType.TO, tos);
// 获取抄送者信息
String[] ccs = mailInfo.getEmailInfoSendCc()!=null?mailInfo.getEmailInfoSendCc().split(","):null;
if (ccs != null){
// 为每个邮件接收者创建一个地址
Address[] ccAdresses = new InternetAddress[ccs.length];
for (int i=0; i<ccs.length; i++){
ccAdresses[i] = new InternetAddress(ccs[i]);
}
// 将抄送者信息设置到邮件信息中,注意类型为Message.RecipientType.CC
message.setRecipients(Message.RecipientType.CC, ccAdresses);
}
// 获取密送者信息
String[] bccs = mailInfo.getEmailInfoSendBcc()!=null?mailInfo.getEmailInfoSendBcc().split(","):null;
if (bccs != null){
// 为每个邮件接收者创建一个地址
Address[] bccAdresses = new InternetAddress[bccs.length];
for (int i=0; i<bccs.length; i++){
bccAdresses[i] = new InternetAddress(bccs[i]);
}
// 将抄送者信息设置到邮件信息中,注意类型为Message.RecipientType.CC
message.setRecipients(Message.RecipientType.BCC, bccAdresses);
}
//主题
message.setSubject(mailInfo.getEmailInfoSubject());
// 设置发送时间
message.setSentDate(new Date());
// 设置邮件内容
Multipart msgMultipart = new MimeMultipart();
BodyPart htmlBodyPart = new MimeBodyPart();
htmlBodyPart.setContent(mailInfo.getEmailInfoSendContent(), "text/html;charset=GBK");//内容
msgMultipart.addBodyPart(htmlBodyPart);
message.setContent(msgMultipart);
//监控邮件发送过程
session.setDebug(true);
// 发送邮件
Transport.send(message);
}catch(Exception e){
e.printStackTrace();
}
}