java邮件发送
//邮件发送服务器
private static String HOST = "smtp.163.com";
//默认发送邮件 地址,用户名,密码
private static String FROM = "****@163.com";
private static String USERNAME = "*****";
private static String PASSWORD = "****";
//发送都采用smtp
TRANSPORT = "smtp";
// 邮件发送器
public static boolean sysEmail(String link, String to) throws IOException {
boolean success = false;
try {
Properties props = new Properties();
props.put("mail.smtp.host", HOST);
props.put("mail.smtp.auth", "true");
Session mailSession = Session.getDefaultInstance(props, null);
// MimeMessage message = new MimeMessage(session);
mailSession.setDebug(true);
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
// 将邮件内容以HTML的方式发送
mbp.setContent(link, "text/html;charset=UTF-8");
mp.addBodyPart(mbp);
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(FROM));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
// message.addRecipients(Message.RecipientType.BCC, to);
// System.out.println(props.getProperty(EMAIL_USERNAME));
message.setSubject(EmailList.SUBJECT,"UTF-8");
message.setContent(mp);
message.saveChanges();
Transport transport = mailSession.getTransport(TRANSPORT);
transport.connect(HOST, USERNAME, PASSWORD);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
success = true;
} catch (MessagingException e) {
e.printStackTrace();
success = false;
}
return success;
}
群发的时候
message.addRecipients(Message.RecipientType.BCC, to);
to ="";邮件列表之间用逗号分割
可能发过多造成邮件发送不成功,此时考虑使用线程
有时候邮件显示发送成功,但在邮箱里边收不到,
有可能就是默认发送地址的问题,你把邮件默认发送地址改一下试试。