邮件工具
apache-commons-email
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>1.3.2</version> </dependency>
以scala代码例:
import org.apache.commons.mail.Email import org.apache.commons.mail.EmailException import org.apache.commons.mail.SimpleEmail def sendTextMail(toList: List[String], subject: String, msg: String) { logger.debug( "\n\tSending mail: {}://{}:{}\n\t" + "username: {}, password: {}, auth: {}\n\t" + "to: {}\n\t" + "subject: {}" + "\n\tcontent: {}", protocolString.asInstanceOf[AnyRef], host.asInstanceOf[AnyRef], port.asInstanceOf[AnyRef], username.asInstanceOf[AnyRef], password.asInstanceOf[AnyRef], auth.asInstanceOf[AnyRef], toList.asInstanceOf[AnyRef], subject.asInstanceOf[AnyRef], msg.asInstanceOf[AnyRef]) try { val email: Email = new SimpleEmail() email.setSSLOnConnect("ssl" == auth) email.setHostName(host) email.setSmtpPort(Integer.parseInt(port)) email.setAuthentication(username, password) email.setFrom(username, nickname) toList.foreach((to: String) => email.addTo(to)) // email.addTo(to) email.setSubject(subject) email.setMsg(msg) email.send() logger.debug("mail send success! ") } catch { case e: EmailException => e.printStackTrace case e: Exception => e.printStackTrace } }
用GreenMail模拟邮件服务器
<dependency> <groupId>com.icegreen</groupId> <artifactId>greenmail</artifactId> <version>1.3</version> <scope>test</scope> </dependency>
启动邮件服务
服务器的启动与停止工作可以在单元测试的before和after方法中执行。
为了简单这里用Scala的代码。
启动服务,可以选择是否用SSL
import com.icegreen.greenmail.util.GreenMail; import com.icegreen.greenmail.util.GreenMailUtil; import com.icegreen.greenmail.util.ServerSetup; var greenMail: GreenMail = null before { greenMail = new GreenMail( // com.icegreen.greenmail.util.ServerSetupTest.SMTP // no ssl com.icegreen.greenmail.util.ServerSetupTest.SMTPS ); // ssl greenMail.setUser("user@testmail.com", "password"); greenMail.start(); } after { greenMail.stop(); }
检查收到的邮件内容
在单元测试发出邮件以后,可以等待收到邮件,并检查邮件内容:
import javax.mail.Message test("Send-mail-ad") { AdService.sentMail(); greenMail.waitForIncomingEmail(2000, 1); Message[] msgs = greenMail.getReceiveMessages(); assertEquals(1, msgs.length) assertEquals("Mail Subject", msgs[0].getSubject()) assertEquals("mail html text", GreenMailUtil.getBody(msgs[0]).trim()) }
复杂格式邮件
接收复杂格式邮件附件
package mail; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Properties; import javax.mail.Address; import javax.mail.BodyPart; import javax.mail.Flags; import javax.mail.Flags.Flag; import javax.mail.Folder; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Part; import javax.mail.Session; import javax.mail.Store; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; import com.sun.mail.imap.IMAPMessage; /** * 使用IMAP协议接收邮件 * POP3和IMAP协议的区别: * POP3协议允许电子邮件客户端下载服务器上的邮件,但是在客户端的操作(如移动邮件、标记已读等),不会反馈到服务器上, * 比如通过客户端收取了邮箱中的3封邮件并移动到其它文件夹,邮箱服务器上的这些邮件是没有同时被移动的。 * IMAP协议提供webmail与电子邮件客户端之间的双向通信,客户端的操作都会同步反应到服务器上,对邮件进行的操作,服务 * 上的邮件也会做相应的动作。比如在客户端收取了邮箱中的3封邮件,并将其中一封标记为已读,将另外两封标记为删除,这些操作会 * 即时反馈到服务器上。 * 两种协议相比,IMAP 整体上为用户带来更为便捷和可靠的体验。POP3更易丢失邮件或多次下载相同的邮件,但IMAP通过邮件客户端 * 与webmail之间的双向同步功能很好地避免了这些问题。 */ public class MailUtil { public static final String TEXT_PAIN = "text/*"; public static final String TEXT_HTML = "text/html"; public static final String MSG_RFC822 = "message/rfc822"; /* * * 网易邮箱开通了IMAP后,再心蓝中使用IMAP收件无法正常, <br/> * 提示“NO Select Unsafe Login. Please contact kefu@188.com for help”。 <br/> * 第三方客户端收信他们就会认为是不安全的,实则是为了推广他们自己的邮件客户端,实属霸王条款。 <br/> * <br/> * 网民的力量是强大的,据说从2014.12.20开始,网易提供了偷偷提供了一个入口设置解决这个问题,链接地址: <br/> * http://config.mail.163.com/settings/imap/index.jsp?uid=xxxxxx@163.com <br/> * * 首先需要先登录网页邮箱,如果是126则自己替换链接中的163。按页面提示,通过手机短信验证之后,出现如下提示: <br/> * <br/> * 您可以继续使用当前客户端收发邮件了,请特别注意个人的电子信息安全哦。感谢您对网易邮箱的支持!就可以正常使用第三方邮件客户端的IMAP功能来收件了。 <br/> * */ private static String imapServer = "imap.163.com"; private static String imapPort = "143"; private static String email = "dummymail2018@163.com"; private static String password = "1qaz2wsx"; public static void main(String[] args) throws Exception { // moveMail("lab-booking-unread", "lab-booking-success"); loopAllFolder(); } public static void moveMail(String fromStr, String toStr) throws Exception { Properties props = new Properties(); props.setProperty("mail.store.protocol", "imap"); props.setProperty("mail.imap.host", imapServer); props.setProperty("mail.imap.port", imapPort); // 创建Session实例对象 Session session = Session.getInstance(props); // 创建IMAP协议的Store对象 Store store = session.getStore("imap"); // 连接邮件服务器 store.connect(email, password); // 获得收件箱 Folder from = store.getFolder(fromStr); from.open(Folder.READ_WRITE); Folder to = store.getFolder(toStr); to.open(Folder.READ_WRITE); Message[] mails = from.getMessages(); if (null != mails && mails.length > 0) { Message[] mr = {mails[0]}; from.copyMessages(mr, to); mails[0].setFlag(Flags.Flag.DELETED, true);//设置已删除状态为true } from.close(true); // ture表示对标记了删除记录的邮件实施删除操作 to.close(true); store.close(); } public static void loopAllFolder() throws Exception { // BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); // System.out.println("请输入邮箱地址:"); // String email = reader.readLine(); // System.out.println("请输入邮箱密码:"); // String password = reader.readLine(); // 准备连接服务器的会话信息 Properties props = new Properties(); props.setProperty("mail.store.protocol", "imap"); props.setProperty("mail.imap.host", imapServer); props.setProperty("mail.imap.port", imapPort); // 创建Session实例对象 Session session = Session.getInstance(props); // 创建IMAP协议的Store对象 Store store = session.getStore("imap"); // 连接邮件服务器 store.connect(email, password); // 获得收件箱 // Folder folder = store.getFolder("INBOX"); Folder[] folders = store.getDefaultFolder().list(); for (Folder folder : folders) { // Folder folder = store.getDefaultFolder(); // 以读写模式打开收件箱 folder.open(Folder.READ_WRITE); // 获得收件箱的邮件列表 Message[] messages = folder.getMessages(); // 打印不同状态的邮件数量 System.out.println("<" + folder.getFullName() + "> 中有" + messages.length + "封邮件!"); System.out.println("其中有" + folder.getUnreadMessageCount() + "封未读邮件!"); System.out.println("其中有" + folder.getNewMessageCount() + "封新邮件!"); System.out.println("其中有" + folder.getDeletedMessageCount() + "封已删除邮件!"); System.out.println("------------------------开始解析邮件----------------------------------"); // 解析邮件 for (Message message : messages) { IMAPMessage msg = (IMAPMessage) message; String subject = MimeUtility.decodeText(msg.getSubject()); System.out.println("[" + subject + "]未读,是否需要阅读此邮件(yes/no)?"); // reader = new BufferedReader(new InputStreamReader(System.in)); // String answer = reader.readLine(); String answer = "yes"; if ("yes".equalsIgnoreCase(answer)) { MailUtil.parseMessage("C:\\mailtmp", msg); // 解析邮件 // 第二个参数如果设置为true,则将修改反馈给服务器。false则不反馈给服务器 msg.setFlag(Flag.SEEN, true); //设置已读标志 } } // 关闭资源 folder.close(false); } store.close(); } /** * 解析邮件 * @param messages 要解析的邮件列表 */ public static void parseMessage(String downAttachmentDir, Message... messages) throws MessagingException, IOException // { if (messages == null || messages.length < 1) throw new MessagingException("未找到要解析的邮件!"); // 解析所有邮件 for (int i = 0, count = messages.length; i < count; i++) { MimeMessage msg = (MimeMessage) messages[i]; System.out.println( "------------------解析第" + msg.getMessageNumber() + "封邮件-------------------- "); System.out.println("主题: " + getSubject(msg)); System.out.println("发件人: " + getFrom(msg)); System.out.println("收件人:" + getReceiveAddress(msg, null)); System.out.println("发送时间:" + getSentDate(msg, null)); System.out.println("是否已读:" + isSeen(msg)); System.out.println("邮件优先级:" + getPriority(msg)); System.out.println("是否需要回执:" + isReplySign(msg)); System.out.println("邮件大小:" + msg.getSize() * 1024 + "kb"); boolean isContainerAttachment = isContainAttachment(msg); System.out.println("是否包含附件:" + isContainerAttachment); if (isContainerAttachment && (null != downAttachmentDir)) { saveAttachment(msg, downAttachmentDir + msg.getSubject() + "_"); //保存附件 } Map<String, StringBuffer> content = new HashMap<>(); // String contentType = msg.getContentType(); getMailTextContent(msg, content); System.out.println("邮件正文:" + content.get(TEXT_HTML)); // System.out.println("邮件正文:" + content.get("text/pain")); System.out.println("------------------第" + msg.getMessageNumber() + "封邮件解析结束-------------------- "); System.out.println(); } } /** * 获得邮件主题 * @param msg 邮件内容 * @return 解码后的邮件主题 */ public static String getSubject(MimeMessage msg) throws UnsupportedEncodingException, MessagingException { return MimeUtility.decodeText(msg.getSubject()); } /** * 取得发送者信息 * @param msg 邮件内容 * @return 发送者相关信息 * @throws MessagingException * @throws UnsupportedEncodingException */ public static String getSender(MimeMessage msg) throws MessagingException, UnsupportedEncodingException // { Address sender = msg.getSender(); msg.getSender(); if (null == sender) { return null; } return ""; } /** * 获得邮件发件人 * @param msg 邮件内容 * @return 姓名 <Email地址> * @throws MessagingException * @throws UnsupportedEncodingException */ public static String getFrom(MimeMessage msg) throws MessagingException, UnsupportedEncodingException { String from = ""; Address[] froms = msg.getFrom(); if (froms.length < 1) throw new MessagingException("没有发件人!"); InternetAddress address = (InternetAddress) froms[0]; String person = address.getPersonal(); if (person != null) { person = MimeUtility.decodeText(person) + " "; } else { person = ""; } from = person + "<" + address.getAddress() + ">"; return from; } /** * 根据收件人类型,获取邮件收件人、抄送和密送地址。如果收件人类型为空,则获得所有的收件人 * <p>Message.RecipientType.TO 收件人</p> * <p>Message.RecipientType.CC 抄送</p> * <p>Message.RecipientType.BCC 密送</p> * @param msg 邮件内容 * @param type 收件人类型 * @return 收件人1 <邮件地址1>, 收件人2 <邮件地址2>, ... * @throws MessagingException */ public static String getReceiveAddress(MimeMessage msg, Message.RecipientType type) throws MessagingException { StringBuffer receiveAddress = new StringBuffer(); Address[] addresss = null; if (type == null) { addresss = msg.getAllRecipients(); } else { addresss = msg.getRecipients(type); } if (addresss == null || addresss.length < 1) throw new MessagingException("没有收件人!"); for (Address address : addresss) { InternetAddress internetAddress = (InternetAddress) address; receiveAddress.append(internetAddress.toUnicodeString()).append(","); } receiveAddress.deleteCharAt(receiveAddress.length() - 1); //删除最后一个逗号 return receiveAddress.toString(); } /** * 获得邮件发送时间 * @param msg 邮件内容 * @return yyyy年mm月dd日 星期X HH:mm * @throws MessagingException */ public static String getSentDate(MimeMessage msg, String pattern) throws MessagingException { Date receivedDate = msg.getSentDate(); if (receivedDate == null) return ""; if (pattern == null || "".equals(pattern)) pattern = "yyyy年MM月dd日 E HH:mm "; return new SimpleDateFormat(pattern).format(receivedDate); } /** * 判断邮件中是否包含附件 * @param msg 邮件内容 * @return 邮件中存在附件返回true,不存在返回false * @throws MessagingException * @throws IOException */ public static boolean isContainAttachment(Part part) throws MessagingException, IOException { boolean flag = false; if (part.isMimeType("multipart/*")) { MimeMultipart multipart = (MimeMultipart) part.getContent(); int partCount = multipart.getCount(); for (int i = 0; i < partCount; i++) { BodyPart bodyPart = multipart.getBodyPart(i); String disp = bodyPart.getDisposition(); if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) { flag = true; } else if (bodyPart.isMimeType("multipart/*")) { flag = isContainAttachment(bodyPart); } else { String contentType = bodyPart.getContentType(); if (contentType.indexOf("application") != -1) { flag = true; } if (contentType.indexOf("name") != -1) { flag = true; } } if (flag) break; } } else if (part.isMimeType(MSG_RFC822)) { flag = isContainAttachment((Part) part.getContent()); } return flag; } /** * 判断邮件是否已读 * @param msg 邮件内容 * @return 如果邮件已读返回true,否则返回false * @throws MessagingException */ public static boolean isSeen(MimeMessage msg) throws MessagingException { return msg.getFlags().contains(Flags.Flag.SEEN); } /** * 判断邮件是否需要阅读回执 * @param msg 邮件内容 * @return 需要回执返回true,否则返回false * @throws MessagingException */ public static boolean isReplySign(MimeMessage msg) throws MessagingException { boolean replySign = false; String[] headers = msg.getHeader("Disposition-Notification-To"); if (headers != null) replySign = true; return replySign; } /** * 获得邮件的优先级 * @param msg 邮件内容 * @return 1(High):紧急 3:普通(Normal) 5:低(Low) * @throws MessagingException */ public static String getPriority(MimeMessage msg) throws MessagingException { String priority = "普通"; String[] headers = msg.getHeader("X-Priority"); if (headers != null) { String headerPriority = headers[0]; if (headerPriority.indexOf("1") != -1 || headerPriority.indexOf("High") != -1) priority = "紧急"; else if (headerPriority.indexOf("5") != -1 || headerPriority.indexOf("Low") != -1) priority = "低"; else priority = "普通"; } return priority; } /** * 获得邮件文本内容 * @param part 邮件体 * @param content 存储邮件文本内容的字符串 * @param showType 显示的格式 * @throws MessagingException * @throws IOException */ public static void getMailTextContent(Part part, Map<String, StringBuffer> content) throws MessagingException, IOException // { //如果是文本类型的附件,通过getContent方法可以取到文本内容,但这不是我们需要的结果,所以在这里要做判断 boolean isContainTextAttach = part.getContentType().indexOf("name") > 0; if (part.isMimeType(TEXT_HTML) && !isContainTextAttach) { StringBuffer sb = content.get(TEXT_HTML); if (null == sb) { sb = new StringBuffer(500); content.put(TEXT_HTML, sb); } sb.append(MimeUtility.decodeText(part.getContent().toString())); } else if (part.isMimeType(TEXT_PAIN) && !isContainTextAttach) { StringBuffer sb = content.get(TEXT_PAIN); if (null == sb) { sb = new StringBuffer(500); content.put(TEXT_PAIN, sb); } sb.append(part.getContent().toString()); } else if (part.isMimeType(MSG_RFC822)) { getMailTextContent((Part) part.getContent(), content); } else if (part.isMimeType("multipart/*")) { Multipart multipart = (Multipart) part.getContent(); int partCount = multipart.getCount(); for (int i = 0; i < partCount; i++) { BodyPart bodyPart = multipart.getBodyPart(i); getMailTextContent(bodyPart, content); } } } /** * 保存附件 * @param part 邮件中多个组合体中的其中一个组合体 * @param destDir 附件保存目录 * @throws UnsupportedEncodingException * @throws MessagingException * @throws FileNotFoundException * @throws IOException */ public static void saveAttachment(Part part, String destDir) throws UnsupportedEncodingException, MessagingException, FileNotFoundException, IOException { if (part.isMimeType("multipart/*")) { Multipart multipart = (Multipart) part.getContent(); //复杂体邮件 //复杂体邮件包含多个邮件体 int partCount = multipart.getCount(); for (int i = 0; i < partCount; i++) { //获得复杂体邮件中其中一个邮件体 BodyPart bodyPart = multipart.getBodyPart(i); //某一个邮件体也有可能是由多个邮件体组成的复杂体 String disp = bodyPart.getDisposition(); if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) { InputStream is = bodyPart.getInputStream(); saveFile(is, destDir, decodeText(bodyPart.getFileName())); } else if (bodyPart.isMimeType("multipart/*")) { saveAttachment(bodyPart, destDir); } else { String contentType = bodyPart.getContentType(); if (contentType.indexOf("name") != -1 || contentType.indexOf("application") != -1) { saveFile(bodyPart.getInputStream(), destDir, decodeText(bodyPart.getFileName())); } } } } else if (part.isMimeType(MSG_RFC822)) { saveAttachment((Part) part.getContent(), destDir); } } /** * 读取输入流中的数据保存至指定目录 * @param is 输入流 * @param fileName 文件名 * @param destDir 文件存储目录 * @throws FileNotFoundException * @throws IOException */ private static void saveFile(InputStream is, String destDir, String fileName) throws FileNotFoundException, IOException { BufferedInputStream bis = new BufferedInputStream(is); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(destDir + fileName))); int len = -1; while ((len = bis.read()) != -1) { bos.write(len); bos.flush(); } bos.close(); bis.close(); } /** * 文本解码 * @param encodeText 解码MimeUtility.encodeText(String text)方法编码后的文本 * @return 解码后的文本 * @throws UnsupportedEncodingException */ public static String decodeText(String encodeText) throws UnsupportedEncodingException { if (encodeText == null || "".equals(encodeText)) { return ""; } else { return MimeUtility.decodeText(encodeText); } } }
发送复杂格式邮件
package mail; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class EmailHelper { private String host; private String username; private String password; private String from; private String to; private String subject; private String htmlContent; private String imagePath; public EmailHelper(String host, String username, String password, String from) throws AddressException, MessagingException{ this.host = host; this.username = username; this.password = password; this.from = from; } public void sendWithImage() throws Exception { Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", host); final String username1 = username; final String password1 = password; Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username1, password1); } }); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject(subject); Multipart multipart = new MimeMultipart("related"); System.out.println(" html "); BodyPart htmlPart = new MimeBodyPart(); htmlContent = "<img src=\"cid:image\">" + htmlContent; htmlPart.setContent(htmlContent, "text/html"); multipart.addBodyPart(htmlPart); System.out.println(" image "); System.out.println("image path : " + imagePath); BodyPart imgPart = new MimeBodyPart(); DataSource fds = new FileDataSource(this.imagePath); imgPart.setDataHandler(new DataHandler(fds)); imgPart.setHeader("Content-ID", "<image>"); multipart.addBodyPart(imgPart); message.setContent(multipart); Transport.send(message); System.out.println(" Sent -| "); } public void setTo(String to) { this.to = to; } public void setSubject(String subject) { this.subject = subject; } public void setHtmlContent(String htmlContent) { this.htmlContent = htmlContent; } public String getImagePath() { return imagePath; } public void setImagePath(String imagePath) { this.imagePath = imagePath; } }
public class SendEmailDemo { public static void main(){ String host = "smtp.163.com"; // use your smtp server host final String username = "sender@163.com"; // use your username final String password = "password"; // use your password String from = "sender@163.com"; // use your sender email address String to = "reciever@foxmail.com"; // use your reciever email address try { EmailHelper emailHelper = new EmailHelper(host, username, password, from); emailHelper.setTo(to); emailHelper.setSubject("subject ttt test"); emailHelper.setHtmlContent("<h1> This is html </h1>"); emailHelper.setImagePath("/Users/grs/Documents/Java/mavenEmail/test/src/main/resource/promises.png"); emailHelper.send(); } catch (Exception e) { e.printStackTrace(); } } }