<dependency > <groupId >javax.mail </groupId > <artifactId >mail </artifactId > <version >1.4.5 </version > </dependency >
import java.util.Properties; public class MailSenderInfo { private String mailServerHost; private String mailServerPort = "25"; private String fromAddress; private String toAddress; private String userName; private String password; private boolean validate = false; private boolean ssl = false; private String subject; private String content; private String[] attachFileNames; public Properties getProperties() { Properties p = new Properties(); p.put("mail.smtp.host", this.mailServerHost); p.put("mail.smtp.port", this.mailServerPort); p.put("mail.smtp.auth", this.validate ? "true" : "false"); return p; } public Properties getSslProperties() { Properties p = new Properties(); p.put("mail.smtp.host", this.mailServerHost); p.put("mail.smtp.port", this.mailServerPort); p.put("mail.smtp.socketFactory.port", this.mailServerPort); p.put("mail.smtp.ssl.enable", "true"); p.put("mail.smtp.starttls.enable", "true"); p.put("mail.smtp.auth", this.validate ? "true" : "false"); return p; } public String getMailServerHost() { return this.mailServerHost; } public void setMailServerHost(String mailServerHost) { this.mailServerHost = mailServerHost; } public String getMailServerPort() { return this.mailServerPort; } public void setMailServerPort(String mailServerPort) { this.mailServerPort = mailServerPort; } public boolean isValidate() { return this.validate; } public void setValidate(boolean validate) { this.validate = validate; } public String[] getAttachFileNames() { return this.attachFileNames; } public void setAttachFileNames(String[] fileNames) { this.attachFileNames = fileNames; } public String getFromAddress() { return this.fromAddress; } public void setFromAddress(String fromAddress) { this.fromAddress = fromAddress; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } public String getToAddress() { return this.toAddress; } public void setToAddress(String toAddress) { this.toAddress = toAddress; } public String getUserName() { return this.userName; } public void setUserName(String userName) { this.userName = userName; } public String getSubject() { return this.subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return this.content; } public void setContent(String textContent) { this.content = textContent; } public boolean isSsl() { return ssl; } public void setSsl(boolean ssl) { this.ssl = ssl; } }
import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; public class MyAuthenticator extends Authenticator { String userName = null; String password = null; public MyAuthenticator() { } public MyAuthenticator(String username, String password) { this.userName = username; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(this.userName, this.password); } }
import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import java.util.Date; import java.util.Properties; public class SimpleMailSender { public static boolean sendTextMail(MailSenderInfo mailInfo) { MyAuthenticator authenticator = null; Properties pro = null; if (mailInfo.isSsl()) { pro = mailInfo.getSslProperties(); }else { pro = mailInfo.getProperties(); } if (mailInfo.isValidate()) { authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } Session sendMailSession = Session.getDefaultInstance(pro, authenticator); sendMailSession.setDebug(true); try { Message mailMessage = new MimeMessage(sendMailSession); Address from = new InternetAddress(mailInfo.getFromAddress()); mailMessage.setFrom(from); Address to = new InternetAddress(mailInfo.getToAddress()); mailMessage.setRecipient(Message.RecipientType.TO, to); mailMessage.setSubject(mailInfo.getSubject()); mailMessage.setSentDate(new Date()); String mailContent = mailInfo.getContent(); mailMessage.setText(mailContent); Transport.send(mailMessage); return true; } catch (MessagingException ex) { ex.printStackTrace(); } return false; } public static boolean sendHtmlMail(MailSenderInfo mailInfo) { MyAuthenticator authenticator = null; Properties pro = null; if (mailInfo.isSsl()) { pro = mailInfo.getSslProperties(); }else { pro = mailInfo.getProperties(); } if (mailInfo.isValidate()) { authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } Session sendMailSession = Session.getDefaultInstance(pro, authenticator); try { Message mailMessage = new MimeMessage(sendMailSession); Address from = new InternetAddress(mailInfo.getFromAddress()); mailMessage.setFrom(from); Address to = new InternetAddress(mailInfo.getToAddress()); mailMessage.setRecipient(Message.RecipientType.TO, to); mailMessage.setSubject(mailInfo.getSubject()); mailMessage.setSentDate(new Date()); Multipart mainPart = new MimeMultipart(); BodyPart html = new MimeBodyPart(); html.setContent(mailInfo.getContent(), "text/html; charset=utf-8"); mainPart.addBodyPart(html); mailMessage.setContent(mainPart); Transport.send(mailMessage); return true; } catch (MessagingException ex) { ex.printStackTrace(); } return false; } public static void main(String[] args) { MailSenderInfo mailInfo = new MailSenderInfo(); String MailServerHost = "smtp.qq.com"; String MailServerPort = "587"; //465 String MailUsername = "[email protected]"; String MailPassword = "xxxxx"; String toEmail = "[email protected]"; String[] toEmailAddr = toEmail.split(","); mailInfo.setMailServerHost(MailServerHost); mailInfo.setMailServerPort(MailServerPort); mailInfo.setValidate(true); mailInfo.setUserName(MailUsername); mailInfo.setPassword(MailPassword);// 您的邮箱密码 mailInfo.setFromAddress(MailUsername); mailInfo.setSsl(false); for (String entry : toEmailAddr) { mailInfo.setToAddress(entry); mailInfo.setSubject("11"); mailInfo.setContent("11111"); SimpleMailSender.sendHtmlMail(mailInfo);// 发送html格式 } System.out.println("执行结束!"); } } //多个邮箱 val list = new util.ArrayList[String]() list.add("[email protected]") list.add("[email protected]") list.add("[email protected]") list.add("xxxx") for (entry <- list) { mailInfo.setToAddress(entry) SimpleMailSender.sendHtmlMail(mailInfo) }