网上有许多关于发送邮件的文章,但是也有许多文章没有提到身份验证,完整的发送邮件的代码如下:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class mail {
public static void main(String[] args) {
String[] recipients = { "*****@163.com",
"*****@qq.com",
"******@163.com",
"******@servyou.com.cn"};
String subject = "邮件群发";
String m_body = "<html><body>测试群发,请点击<a href='http://www.baidu.com/'>百度</a></body></html>";
sendMail(subject,m_body,recipients);
}
public static void sendMail(String subject,String m_body,String[] recipients) {
String host = "smtp.163.com";
String port = "25";
String username = "*****@163.com";
String password = "*****";
String to = "*****@gmail.com";
try {
// 创建一个属性对象
Properties props = System.getProperties(); // 创建系统属性对象
props.setProperty("mail.transport.protocol", "smtp"); // 设置使用smtp协议
props.setProperty("mail.smtp.host", host); // 设置SMTP服务器地址
props.setProperty("mail.smtp.port", port); // 设置SMTP端口号
props.setProperty("mail.smtp.auth", "true"); // SMTP服务用户认证
props.put("mail.smtp.starttls.enable","true");
// 创建一个过程对象
PopupAuthenticator authenticator = new PopupAuthenticator();
authenticator.performCheck(username, password);
javax.mail.Session sess = javax.mail.Session.getDefaultInstance(
props, authenticator);
MimeMessage msg = new MimeMessage(sess); // 创建邮件对象
msg.setSubject(subject); // 设置邮件主题
msg.setSentDate(new Date()); // 设置发信日期
msg.setFrom(new InternetAddress(username)); // 设置发信人
// 设置主收件人
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
// 设置暗抄送人
InternetAddress[] BCCAddress = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
System.out.println(recipients[i]);
BCCAddress[i] = new InternetAddress(recipients[i]);
}
msg.addRecipients(Message.RecipientType.BCC,BCCAddress);
MimeMultipart content = new MimeMultipart(); // 创建邮件体对象
// if(content==null) return false;
MimeBodyPart part = new MimeBodyPart(); // 创建文本部分对象
String type = "text/html;charset=UTF-8"; // 文本类型默认为text/plain
part.setHeader("Content-Type", type);
part.setText(m_body);
content.addBodyPart(part);
msg.setContent(content); // 添加文本至邮件中
msg.saveChanges(); // 保存修改
Transport trans = sess.getTransport(); // 创建发送对象
trans.send(msg); // 发送
// trans.connect(host, 25, username, password);
// trans.sendMessage(msg, BCCAddress);
trans.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
class PopupAuthenticator extends Authenticator{
String username=null;
String password=null;
public PopupAuthenticator(){}
public PasswordAuthentication performCheck(String user,String pass){
username = user;
password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
上面的代码中包含了两种身份验证方式,一种是用了PopupAuthenticator类的验证,
PopupAuthenticator authenticator = new PopupAuthenticator();
authenticator.performCheck(username, password);
javax.mail.Session sess = javax.mail.Session.getDefaultInstance(
props, authenticator);
另一种是
javax.mail.Session sess = javax.mail.Session.getDefaultInstance(
props, null);
trans.connect(host, 25, username, password);
trans.sendMessage(msg, BCCAddress);
这个邮件发送程序有不足之处就是发送邮件时,部分邮箱收不到邮件,可能与邮件的规范有关系,在设置邮件标题的时候要注意,不要乱写