1.新建model
SendMail.java
import java.util.Properties; public class SendMail { // 发送邮件的服务器的IP和端口 private String mailServerHost; //服务器名称,类似163的: smtp.163.com private String mailServerPort = "25"; //服务器端口号 private String fromAddress; //发送邮件的地址 private String addressName; //发送邮件的邮箱名称 private String toAddress; //收件人地址 private String userName; //和邮箱地址保持一致 private String password; //邮件安全密码,网易的是客户端授权密码 private boolean validate = 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", validate ? "true" : "false"); return p; } public String getMailServerHost() { return mailServerHost; } public void setMailServerHost(String mailServerHost) { this.mailServerHost = mailServerHost; } public String getMailServerPort() { return mailServerPort; } public void setMailServerPort(String mailServerPort) { this.mailServerPort = mailServerPort; } public boolean isValidate() { return validate; } public void setValidate(boolean validate) { this.validate = validate; } public String[] getAttachFileNames() { return attachFileNames; } public void setAttachFileNames(String[] fileNames) { this.attachFileNames = fileNames; } public String getFromAddress() { return fromAddress; } public void setFromAddress(String fromAddress) { this.fromAddress = fromAddress; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getToAddress() { return toAddress; } public void setToAddress(String toAddress) { this.toAddress = toAddress; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String textContent) { this.content = textContent; } public String getAddressName() { return addressName; } public void setAddressName(String addressName) { this.addressName = addressName; } }
2.发送邮件实现类
MailSender.java
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.io.UnsupportedEncodingException; import java.util.Date; import java.util.Properties; public class MailSender { /** * 以文本格式发送邮件 */ public boolean sendTextMail(SendMail mailInfo) { // 判断是否需要身份认证 Authentication authenticator = null; Properties pro = mailInfo.getProperties(); if (mailInfo.isValidate()) { // 如果需要身份认证,则创建一个密码验证器 authenticator = new Authentication(mailInfo.getUserName(), mailInfo.getPassword()); } // 根据邮件会话属性和密码验证器构造一个发送邮件的session 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()); String mailContent = mailInfo.getContent(); mailMessage.setText(mailContent); Transport.send(mailMessage); return true; } catch (MessagingException ex) { ex.printStackTrace(); } return false; } /** * 以HTML格式发送邮件 */ public boolean sendHtmlMail(SendMail mailInfo) { // 判断是否需要身份认证 Authentication authenticator = null; Properties pro = mailInfo.getProperties(); if (mailInfo.isValidate()) { authenticator = new Authentication(mailInfo.getUserName(), mailInfo.getPassword()); } // 根据邮件会话属性和密码验证器构造一个发送邮件的session Session sendMailSession = Session .getDefaultInstance(pro, authenticator); try { // 根据session创建一个邮件消息 Message mailMessage = new MimeMessage(sendMailSession); mailMessage.setFrom(new InternetAddress(mailInfo.getFromAddress())); //设置自定义发件人昵称 String nick=""; try { nick=javax.mail.internet.MimeUtility.encodeText(mailInfo.getAddressName()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } mailMessage.setFrom(new InternetAddress(nick+" <"+mailInfo.getFromAddress()+">")); // Address from = new InternetAddress(mailInfo.getFromAddress()); // mailMessage.setFrom(from); Address to = new InternetAddress(mailInfo.getToAddress()); // Message.RecipientType.TO属性表示接收者的类型为TO 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; } }
3.邮件发送工具类
SendMailUtil.java
import java.util.HashMap; import java.util.Map; public class SendMailUtil {private static SendMail mail() { SendMail mail = new SendMail(); mail.setMailServerHost("smtp.yeah.net"); mail.setMailServerPort("25"); mail.setValidate(true); mail.setUserName("发送邮件的邮箱地址"); mail.setPassword("邮件服务器的客户端授权密码"); mail.setFromAddress("发送邮件的邮箱地址"); mail.setAddressName("邮箱名称"); mail.setSubject("标题"); return mail; } public static boolean checkEmail(String email) {// 验证邮箱的正则表达式 String format = "\p{Alpha}\w{2,15}[@][a-z0-9]{3,}[.]\p{Lower}{2,}"; //p{Alpha}:内容是必选的,和字母字符[p{Lower}p{Upper}]等价。如:200896@163.com不是合法的。 //w{2,15}: 2~15个[a-zA-Z_0-9]字符;w{}内容是必选的。 如:dyh@152.com是合法的。 //[a-z0-9]{3,}:至少三个[a-z0-9]字符,[]内的是必选的;如:dyh200896@16.com是不合法的。 //[.]:'.'号时必选的; 如:dyh200896@163com是不合法的。 //p{Lower}{2,}小写字母,两个以上。如:dyh200896@163.c是不合法的。 if (email.matches(format)) { return true;// 邮箱名合法,返回true } else { return false;// 邮箱名不合法,返回false } } public static boolean Send(String con,String email,String subject){ SendMail mail = mail(); mail.setSubject(subject); mail.setContent(con); if (email == null || "".equals(email)) { return false; } mail.setToAddress(email); try { MailSender sms = new MailSender(); boolean flag = sms.sendHtmlMail(mail); if (flag == false) return false; } catch (Exception e) { e.printStackTrace(); return false; } return true; } }