import com.sun.mail.imap.IMAPFolder; import com.sun.mail.imap.protocol.IMAPProtocol; import org.apache.tomcat.util.http.fileupload.IOUtils; import org.springframework.util.ObjectUtils; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeUtility; import java.io.*; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class ReceivedIMAPMail { public static String NORM_DATETIME_PATTERN = "yyyy-MM-dd hh:mm:ss"; private MimeMessage mimeMessage; /** * 附件下载后的存放目录 */ private String saveAttachPath = ""; /** * 存放邮件内容的StringBuffer对象 */ private StringBuffer bodyText = new StringBuffer(); /** * 构造函数,初始化一个MimeMessage对象 * * @param mimeMessage */ public ReceivedIMAPMail(MimeMessage mimeMessage) { this.mimeMessage = mimeMessage; } /** * 获得发件人的地址和姓名 * * @return * @throws MessagingException */ public String getFrom() throws MessagingException { InternetAddress address[] = (InternetAddress[]) mimeMessage.getFrom(); String from = address[0].getAddress(); if (from == null) { from = ""; } String personal = address[0].getPersonal(); if (personal == null) { personal = ""; } String fromAddr = null; if (personal != null || from != null) { fromAddr = personal + "<" + from + ">"; } return fromAddr; } /** * 获得邮件的收件人,抄送,和密送的地址和姓名,根据所传递的参数的不同 * * @param type "to"----收件人 "cc"---抄送人地址 "bcc"---密送人地址 * @return * @throws MessagingException * @throws UnsupportedEncodingException */ public String getMailAddress(String type) throws MessagingException, UnsupportedEncodingException { if (ObjectUtils.isEmpty(type)) { return ""; } String addType = type.toUpperCase(); if (!addType.equals("TO") && !addType.equals("CC") && !addType.equals("BCC")) { return ""; } InternetAddress[] address; if (addType.equals("TO")) { address = (InternetAddress[]) mimeMessage.getRecipients(Message.RecipientType.TO); } else if (addType.equals("CC")) { address = (InternetAddress[]) mimeMessage.getRecipients(Message.RecipientType.CC); } else { address = (InternetAddress[]) mimeMessage.getRecipients(Message.RecipientType.BCC); } if (ObjectUtils.isEmpty(address)) { return ""; } StringBuilder mailAddr = new StringBuilder(); String emailAddr; String personal; for (int i = 0; i < address.length; i++) { emailAddr = address[i].getAddress(); if (emailAddr == null) { emailAddr = ""; } else { emailAddr = MimeUtility.decodeText(emailAddr); } personal = address[i].getPersonal(); if (personal == null) { personal = ""; } else { personal = MimeUtility.decodeText(personal); } mailAddr.append(",").append(personal).append("<").append(emailAddr).append(">"); } return mailAddr.toString().substring(1); } /** * 获得邮件主题 * * @return * @throws MessagingException * @throws UnsupportedEncodingException */ public String getSubject() throws MessagingException, UnsupportedEncodingException { String subject = MimeUtility.decodeText(mimeMessage.getSubject()); if (subject == null) { subject = ""; } return subject; } /** * 获得邮件发送日期 * * @return * @throws MessagingException */ public String getSentDate() throws MessagingException { Date sentDate = mimeMessage.getSentDate(); SimpleDateFormat format = new SimpleDateFormat(NORM_DATETIME_PATTERN); return format.format(sentDate); } /** * 获得邮件正文内容 * * @return */ public String getBodyText() { return bodyText.toString(); } /** * 解析邮件,把得到的邮件内容保存到一个StringBuffer对象中,解析邮件 主要是根据MimeType类型的不同执行不同的操作,一步一步的解析 * * @param part * @throws MessagingException * @throws IOException */ public void getMailContent(Part part) throws MessagingException, IOException { String contentType = part.getContentType(); int nameIndex = contentType.indexOf("name"); boolean conName = false; if (nameIndex != -1) { conName = true; } if (part.isMimeType("text/plain") && conName == false) { bodyText.append((String) part.getContent()); } else if (part.isMimeType("text/html") && conName == false) { bodyText.append((String) part.getContent()); } else if (part.isMimeType("multipart/*")) { Multipart multipart = (Multipart) part.getContent(); int counts = multipart.getCount(); for (int i = 0; i < counts; i++) { this.getMailContent(multipart.getBodyPart(i)); } } else if (part.isMimeType("message/rfc822")) { this.getMailContent((Part) part.getContent()); } } /** * 判断此邮件是否需要回执,如果需要回执返回"true",否则返回"false" * * @return * @throws MessagingException */ public boolean getReplySign() throws MessagingException { boolean replySign = false; String needReply[] = mimeMessage.getHeader("Disposition-Notification-To"); if (needReply != null) { replySign = true; } return replySign; } /** * 判断此邮件是否已读,如果未读返回false,反之返回true * * @return * @throws MessagingException */ public boolean isNew() throws MessagingException { boolean isNew = false; Flags flags = mimeMessage.getFlags(); Flags.Flag[] flag = flags.getSystemFlags(); for (int i = 0; i < flag.length; i++) { if (flag[i] == Flags.Flag.SEEN) { isNew = true; } } return isNew; } /** * 判断此邮件是否包含附件 * * @param part * @return * @throws MessagingException * @throws IOException */ public boolean isContainAttach(Part part) throws MessagingException, IOException { boolean attachFlag = false; if (part.isMimeType("multipart/*")) { Multipart mp = (Multipart) part.getContent(); BodyPart mPart; String conType; for (int i = 0; i < mp.getCount(); i++) { mPart = mp.getBodyPart(i); String disposition = mPart.getDisposition(); if (Part.ATTACHMENT.equals(disposition) || Part.INLINE.equals(disposition)) { attachFlag = true; } else if (mPart.isMimeType("multipart/*")) { attachFlag = this.isContainAttach(mPart); } else { conType = mPart.getContentType(); if (conType.toLowerCase().indexOf("application") != -1 || conType.toLowerCase().indexOf("name") != -1) { attachFlag = true; } } } } else if (part.isMimeType("message/rfc822")) { attachFlag = isContainAttach((Part) part.getContent()); } return attachFlag; } /** * 保存附件 * * @param part * @throws MessagingException * @throws IOException */ public void saveAttachMent(Part part) throws MessagingException, IOException { String fileName; if (part.isMimeType("multipart/*")) { Multipart mp = (Multipart) part.getContent(); BodyPart mPart; for (int i = 0; i < mp.getCount(); i++) { mPart = mp.getBodyPart(i); String disposition = mPart.getDisposition(); if (Part.ATTACHMENT.equals(disposition) || Part.INLINE.equals(disposition)) { fileName = mPart.getFileName(); if (null != fileName && fileName.toLowerCase().indexOf("gb2312") != -1) { fileName = MimeUtility.decodeText(fileName); } fileName = fileName.replaceAll("\\?|=", ""); this.saveFile(fileName, mPart.getInputStream()); } else if (mPart.isMimeType("multipart/*")) { this.saveAttachMent(mPart); } else { fileName = mPart.getFileName(); if ((fileName != null) && (fileName.toLowerCase().indexOf("GB2312") != -1)) { fileName = MimeUtility.decodeText(fileName); this.saveFile(fileName, mPart.getInputStream()); } } } } else if (part.isMimeType("message/rfc822")) { this.saveAttachMent((Part) part.getContent()); } } /** * 设置附件存放路径 * * @param attachPath */ public void setAttachPath(String attachPath) { this.saveAttachPath = attachPath; } /** * 获得附件存放路径 * * @return */ public String getAttachPath() { return saveAttachPath; } /** * 真正的保存附件到指定目录里 * * @param fileName * @param in * @throws IOException */ private void saveFile(String fileName, InputStream in) throws IOException { String osName = System.getProperty("os.name"); String storeDir = this.getAttachPath(); if (null == osName) { osName = ""; } if (osName.toLowerCase().indexOf("win") != -1) { if (ObjectUtils.isEmpty(storeDir)) storeDir = "C:\\tmp"; } else { storeDir = "/tmp"; } FileOutputStream fos = new FileOutputStream(new File(storeDir + File.separator + fileName)); IOUtils.copy(in, fos); IOUtils.closeQuietly(fos); IOUtils.closeQuietly(in); } /** * 获取阿里云邮箱信息 * * @param host 邮件服务器 * @param username 邮箱名 * @param password 密码 * @param protocol 协议 * @return * @throws MessagingException */ public static Message[] getALiYunMessage(String host, String username, String password, String protocol) throws MessagingException { Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); Store store = session.getStore(protocol); store.connect(host, username, password); Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_ONLY); return folder.getMessages(); } /** * 获取163邮箱信息 * * @param host * @param username * @param password * @param protocol * @return * @throws MessagingException */ public static Message[] getWEMessage(String host, String username, String password, String protocol) throws MessagingException { Properties props = System.getProperties(); props.setProperty("mail.store.protocol", protocol); Session session = Session.getDefaultInstance(props, null); Store store = session.getStore(protocol); store.connect(host, username, password); Folder folder = store.getFolder("INBOX"); if (folder instanceof IMAPFolder) { IMAPFolder imapFolder = (IMAPFolder) folder; // javamail中使用id命令有校验checkOpened, 所以要去掉id方法中的checkOpened(); imapFolder.doCommand(new IMAPFolder.ProtocolCommand() { public Object doCommand(IMAPProtocol p) throws com.sun.mail.iap.ProtocolException { Mapgmap = new HashMap<>(); gmap.put("GUID", "FUTONG"); // p.id("FUTONG"); p.id(gmap); return null; } }); } if (folder != null) { folder.open(Folder.READ_WRITE); } return folder.getMessages(); } /** * 过滤邮箱信息 * * @param messages * @param fromMail 只读取该邮箱发来的邮件,如果为空则不过滤 * @param startDate 只读取该日期以后的邮件,如果为空则不过滤 * @return * @throws MessagingException */ public static List filterMessage(Message[] messages, String fromMail, String startDate) throws MessagingException, ParseException { List messageList = new ArrayList<>(); if (ObjectUtils.isEmpty(messages)) { return messageList; } boolean isEnptyFromMail = ObjectUtils.isEmpty(fromMail); boolean isEnptyStartDate = ObjectUtils.isEmpty(startDate); if (isEnptyFromMail && isEnptyStartDate) { return Arrays.asList(messages); } String from; for (Message message : messages) { from = (message.getFrom()[0]).toString(); if (isEnptyFromMail) { if (new SimpleDateFormat(NORM_DATETIME_PATTERN).parse(startDate).getTime() > message.getSentDate() .getTime()) { continue; } } else if (null != from && from.contains(fromMail)) { if (!isEnptyStartDate && new SimpleDateFormat(NORM_DATETIME_PATTERN).parse(startDate) .getTime() > message.getSentDate().getTime()) { continue; } } else { continue; } messageList.add(message); } return messageList; } /** * 打印邮件 * * @param messageList * @throws IOException * @throws MessagingException */ public static void printMailMessage(List messageList) throws IOException, MessagingException { System.out.println("邮件数量:" + messageList.size()); ReceivedIMAPMail re; Message message; for (int i = 0, leng = messageList.size(); i < leng; i++) { message = messageList.get(i); re = new ReceivedIMAPMail((MimeMessage) message); System.out.println("邮件【" + i + "】主题:" + re.getSubject()); System.out.println("邮件【" + i + "】发送时间:" + re.getSentDate()); System.out.println("邮件【" + i + "】是否需要回复:" + re.getReplySign()); System.out.println("邮件【" + i + "】是否已读:" + re.isNew()); System.out.println("邮件【" + i + "】是否包含附件:" + re.isContainAttach(message)); System.out.println("邮件【" + i + "】发送人地址:" + re.getFrom()); System.out.println("邮件【" + i + "】收信人地址:" + re.getMailAddress("to")); System.out.println("邮件【" + i + "】抄送:" + re.getMailAddress("cc")); System.out.println("邮件【" + i + "】暗抄:" + re.getMailAddress("bcc")); System.out.println("邮件【" + i + "】发送时间:" + re.getSentDate()); System.out.println("邮件【" + i + "】邮件ID:" + ((MimeMessage) message).getMessageID()); re.getMailContent(message); // System.out.println("邮件【" + i + "】正文内容:\r\n" + re.getBodyText()); if(re.isContainAttach(message)) { System.out.println("邮件包含附件,开始下载到D:\\xl下面"); re.setAttachPath("D:\\xl\\mail\\"); re.saveAttachMent(message); } } } public static void main(String[] args) throws MessagingException, IOException, ParseException { // 阿里云登录信息 // String host = "pop3.mxhichina.com"; // String username = "[email protected]"; // String password = "密码"; // String protocol = "pop3"; // String fromMail = "[email protected]"; // String startDate = "2020-2-24 23:35:40"; // List messageList = filterMessage(getALiYunMessage(host, username, password, protocol), fromMail, startDate); // 163登录信息 String host = "imap.163.com"; String username = "*****@163.com"; String password = "客户端授权码"; String protocol = "imaps"; String fromMail = "";// "****@163.com"; String startDate = "";// "2020-2-24 23:35:40"; ListmessageList = filterMessage(getWEMessage(host, username, password, protocol), fromMail, startDate); printMailMessage(messageList); } }
解决可能的附件文件乱码问题:
/** * 保存附件 * * @param part * @throws MessagingException * @throws IOException */ public void saveAttachMent(Part part) throws MessagingException, IOException { String fileName; if (part.isMimeType("multipart/*")) { Multipart mp = (Multipart) part.getContent(); BodyPart mPart; for (int i = 0; i < mp.getCount(); i++) { mPart = mp.getBodyPart(i); String disposition = mPart.getDisposition(); if (Part.ATTACHMENT.equals(disposition) || Part.INLINE.equals(disposition)) { fileName = mPart.getFileName(); if (null != fileName && fileName.toLowerCase().indexOf("gb2312") != -1) { fileName = MimeUtility.decodeText(fileName); } if(null != fileName && fileName.toLowerCase().indexOf("utf-8") != -1) { fileName = MimeUtility.decodeWord(fileName); } //fileName = fileName.replaceAll("\\?|=", ""); this.saveFile(fileName, mPart.getInputStream()); } else if (mPart.isMimeType("multipart/*")) { this.saveAttachMent(mPart); } else { fileName = mPart.getFileName(); if ((fileName != null) && (fileName.toLowerCase().indexOf("gb2312") != -1)) { fileName = MimeUtility.decodeText(fileName); this.saveFile(fileName, mPart.getInputStream()); } if ((fileName != null) && (fileName.toLowerCase().indexOf("utf-8") != -1)) { fileName = MimeUtility.decodeWord(fileName); this.saveFile(fileName, mPart.getInputStream()); } } } } else if (part.isMimeType("message/rfc822")) { this.saveAttachMent((Part) part.getContent()); } }
发邮件代码:
compile "org.springframework.boot:spring-boot-starter-mail:1.5.8"
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; import java.util.Date; @Component public class SpringbootMailService { @Autowired JavaMailSender javaMailSender; public String sendSimpleMail(String to, String cc, String title, String content) { // 1.构建一个邮件对象 SimpleMailMessage message = new SimpleMailMessage(); // 2.设置邮件主题 message.setSubject(title); // 3.设置邮件发送者 message.setFrom("****@163.com"); // 4. 设置邮件接收者,可以有多个接收者 message.setTo(to); // 5.设置邮件抄送人,可以有多个抄送人 message.setCc(cc); // 6.设置隐秘抄送人,可以有多个 // message.setBcc(); // 7.设置邮件发送日期 message.setSentDate(new Date()); // 8. 设置邮件的正文 message.setText(content); try { // 9. 发送邮件 javaMailSender.send(message); return "success"; } catch (Throwable e) { return e.getMessage(); } } public void sendSimpleMail() { // 1.构建一个邮件对象 SimpleMailMessage message = new SimpleMailMessage(); // 2.设置邮件主题 message.setSubject("这是一封测试邮件"); // 3.设置邮件发送者 message.setFrom("****@163.com"); // 4. 设置邮件接收者,可以有多个接收者 message.setTo("*****"); // 5.设置邮件抄送人,可以有多个抄送人 // message.setCc(); // 6.设置隐秘抄送人,可以有多个 // message.setBcc(); // 7.设置邮件发送日期 message.setSentDate(new Date()); // 8. 设置邮件的正文 message.setText("这是测试邮件的正文"); // 9. 发送邮件 javaMailSender.send(message); } public void sendAttachFileMail() throws MessagingException { // 1. 构建邮件对象,注意,这里要通过 javaMailSender 来获取一个复杂邮件对象 MimeMessage mimeMessage = javaMailSender.createMimeMessage(); // 2. MimeMessageHelper 是一个邮件配置的辅助工具类,true 表示构建一个 multipart message 类型的邮件 MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); // 3. 针对工具类,配置邮件发送的基本信息 helper.setSubject("图片"); helper.setFrom("******@163.com"); helper.setTo("l*****"); helper.setCc("*****@163.com"); // helper.setBcc(); helper.setSentDate(new Date()); helper.setText("这是测试邮件的正文"); // 4. 添加邮件附件 helper.addAttachment("杨幂.jpg", new File("C:\\Users\\Administrator\\Pictures\\0.jpg")); // 5. 发送邮件 javaMailSender.send(mimeMessage); } public void sendImgResMail() throws MessagingException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject("这是一封测试邮件"); helper.setFrom("*****@163.com"); helper.setTo("*****@163.com"); helper.setSentDate(new Date()); helper.setText( "hello 大家好,这是一封测试邮件,这封邮件包含两种图片,分别如下
第一张图片:
第二张图片:
", true); helper.addInline("p01", new FileSystemResource(new File("C:\\Users\\sang\\Downloads\\javaboy.png"))); helper.addInline("p02", new FileSystemResource(new File("C:\\Users\\sang\\Downloads\\javaboy2.png"))); javaMailSender.send(mimeMessage); } public static void main(String[] args) throws Throwable { SpringbootMailService service = new SpringbootMailService(); JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); javaMailSender.setDefaultEncoding("UTF-8"); javaMailSender.setHost("smtp.163.com"); javaMailSender.setPort(25); javaMailSender.setUsername("*****@163.com"); javaMailSender.setPassword("授权码"); service.javaMailSender = javaMailSender; service.sendAttachFileMail(); } }
收邮件几乎搞了一天,就差几行代码,最后网上搜到了。网上人才多啊!
/** * 保存附件 * * @param part * @throws MessagingException * @throws IOException */public void saveAttachMent(Part part) throws MessagingException, IOException {String fileName;if (part.isMimeType("multipart/*")) {Multipart mp = (Multipart) part.getContent();BodyPart mPart;for (int i = 0; i < mp.getCount(); i++) {mPart = mp.getBodyPart(i);String disposition = mPart.getDisposition();if (Part.ATTACHMENT.equals(disposition) || Part.INLINE.equals(disposition)) {fileName = mPart.getFileName();if (null != fileName && fileName.toLowerCase().indexOf("gb2312") != -1) {fileName = MimeUtility.decodeText(fileName);}if(null != fileName && fileName.toLowerCase().indexOf("utf-8") != -1) {fileName = MimeUtility.decodeWord(fileName);}//fileName = fileName.replaceAll("\\?|=", "");this.saveFile(fileName, mPart.getInputStream());} else if (mPart.isMimeType("multipart/*")) {this.saveAttachMent(mPart);} else {fileName = mPart.getFileName();if ((fileName != null) && (fileName.toLowerCase().indexOf("gb2312") != -1)) {fileName = MimeUtility.decodeText(fileName);this.saveFile(fileName, mPart.getInputStream());}if ((fileName != null) && (fileName.toLowerCase().indexOf("utf-8") != -1)) {fileName = MimeUtility.decodeWord(fileName);this.saveFile(fileName, mPart.getInputStream());}}}} else if (part.isMimeType("message/rfc822")) {this.saveAttachMent((Part) part.getContent());}}