package com.lyis.commons.mail; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Message; 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; /** * 邮件服务工具类 * * @author Johnson * @version Wednesday May 18th, 2011 */ public class Email { // 邮件服务器 private String mailServerHost; // 邮件服务器端口 private String mailServerPort; // 邮件发送方地址 private String fromAddr; // 邮件接收方地址 private String[] toAddrs; // 邮件发件方用户名 private String userName; // 邮件发件方密码 private String password; // 邮件主题 private String subject; // 邮件内容 private String content; // 邮件附件 private String[] attachFileNames; /** * 获取基础配置 * * @return */ private Properties getProperties() { Properties prop = new Properties(); String host = getMailServerHost(); prop.put("mail.smtp.host", host); prop.put("mail.smtp.port", getMailServerPort()); prop.put("mail.smtp.auth", "true"); if (host.indexOf("smtp.gmail.com") != -1) { prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); prop.setProperty("mail.smtp.socketFactory.fallback", "false"); prop.setProperty("mail.smtp.port", "465"); prop.setProperty("mail.smtp.socketFactory.port", "465"); } return prop; } /** * 获取邮件会话 * * @return */ private Session getSession() { // 身份验证 Authenticator auth = new MailAuthenticator(userName, password); return Session.getDefaultInstance(getProperties(), auth); } /** * 获取邮件消息 * * @return * @throws Exception */ private Message getMessage() throws Exception { Message message = new MimeMessage(getSession()); // 发件方邮箱地址 Address addr = new InternetAddress(fromAddr); message.setFrom(addr); // 收件方邮箱地址 Address[] addrs = new Address[toAddrs.length]; for (int i = 0; i < addrs.length; i++) { addrs[i] = new InternetAddress(toAddrs[i]); } message.setRecipients(Message.RecipientType.TO, addrs); // 邮件主题 message.setSubject(subject); // 发送日期 message.setSentDate(new Date()); return message; } /** * 添加附件 * * @param message * 邮件消息 * @param content * 邮件内容 * @throws Exception */ private void addAttaches(Message message, String content) throws Exception { Multipart multipart = new MimeMultipart(); BodyPart bodyPart = new MimeBodyPart(); bodyPart.setContent(content, "text/html; charset=utf-8"); multipart.addBodyPart(bodyPart); if (attachFileNames != null && attachFileNames.length > 0) { DataHandler handler = null; FileDataSource fds = null; for (String path : attachFileNames) { fds = new FileDataSource(path); handler = new DataHandler(fds); bodyPart = new MimeBodyPart(); bodyPart.setFileName(fds.getName()); bodyPart.setDataHandler(handler); multipart.addBodyPart(bodyPart); } } message.setContent(multipart); } /** * 发送邮件 * * @param type * 邮件类型枚举 * @throws Exception */ public void send(EmailType type) throws Exception { Message message = getMessage(); switch (type) { case WITH_TEXT: // 纯文本邮件 message.setText(content); break; case WITH_HTML: // HTML格式邮件 message.setContent(content, "text/html; charset=utf-8"); break; case WITH_ATTACH: // 含附件邮件 addAttaches(message, content); break; } message.saveChanges(); Transport.send(message); } /** * 发送邮件(默认按纯文本发送) * * @throws Exception */ public void send() throws Exception { send(EmailType.WITH_TEXT); } public String getMailServerHost() { // 邮件默认服务器 mailServerHost = "smtp.163.com"; if (!"".equals(fromAddr)) { int start = fromAddr.indexOf("@") + 1; int end = fromAddr.length(); mailServerHost = "smtp." + fromAddr.substring(start, end); } return mailServerHost; } public void setMailServerHost(String mailServerHost) { this.mailServerHost = mailServerHost; } public String getMailServerPort() { if (mailServerPort == null || "".equals(mailServerPort)) { return "25"; } return mailServerPort; } public void setMailServerPort(String mailServerPort) { this.mailServerPort = mailServerPort; } public String getFromAddr() { return fromAddr; } public void setFromAddr(String fromAddr) { this.fromAddr = fromAddr; } public String[] getToAddrs() { return toAddrs; } public void setToAddrs(String... toAddrs) { this.toAddrs = toAddrs; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String[] getAttachFileNames() { return attachFileNames; } public void setAttachFileNames(String... attachFileNames) { this.attachFileNames = attachFileNames; } } package com.lyis.commons.mail; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; /** * 邮件身份验证 * * @author Johnson * @version Wednesday May 17th, 2011 */ public class MailAuthenticator extends Authenticator { private String userName; private String password; public MailAuthenticator(String userName, String password) { this.userName = userName; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } } package com.lyis.commons.mail; /** * 邮件类型枚举 * * @author Johnson * @version Wednesday May 17th, 2011 */ public enum EmailType { /** * 纯文本邮件(默认) */ WITH_TEXT, /** * 含附件邮件 */ WITH_ATTACH, /** * HTML格式邮件 */ WITH_HTML; } package com.lyis.commons.mail; public class Test { public static void main(String[] args) { try { Email email = new Email(); email.setFromAddr("johnsonvily@gmail.com"); email.setToAddrs("543076259@qq.com", "839253575@qq.com"); email.setUserName("johnsonvily"); email.setPassword("***********"); email.setSubject("Test java email"); email.setContent("Test java email...."); // 发送附件 email.setAttachFileNames("F:/images/12.jpg", "F:/images/Chuntian03.jpg"); email.send(EmailType.WITH_ATTACH); } catch (Exception e) { e.printStackTrace(); } } } 说明:邮件发送必须通过身份验证,才能发送。