生活随笔
收集整理的這篇文章主要介紹了
使用javaMail收邮件 支持附件下载
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
http://haohaoxuexi.iteye.com/blog/1162044
使用javaMail收郵件主要有兩種協(xié)議,一種是pop3,一種是imap。這兩種協(xié)議都可以用來收郵件,但是在其中的處理上是有區(qū)別的。pop3是不支持判斷郵件是否為已讀的,也就是說你不能直接從收件箱里面取到未讀郵件,這需要自己進行判斷,然而imap就提供了這樣的功能,使用imap時可以很輕松的判斷該郵件是否為已讀或未讀或其他。
?
此外收件箱中的每一封郵件都對應著一個MessageNumber,所以可以通過一個MessageNumber拿到對應的那封郵件。如:Message message = folder.getMessage(messageNumber);
?
pop3和imap主要區(qū)別就是能否判斷郵件狀態(tài)的問題,其他的操作都差不多.
?
pop3收郵件和解析郵件:
Java代碼 ?
package?com.clxx.test; ????import?java.io.FileOutputStream; ??import?java.io.IOException; ??import?java.io.InputStream; ??import?java.io.OutputStream; ??import?java.util.Enumeration; ??import?java.util.Properties; ????import?javax.mail.BodyPart; ??import?javax.mail.Folder; ??import?javax.mail.Message; ??import?javax.mail.MessagingException; ??import?javax.mail.Multipart; ??import?javax.mail.Session; ??import?javax.mail.Store; ??import?javax.mail.internet.MimeMultipart; ????public?class?Email2?{ ????????public?static?void?main(String?args[])?throws?MessagingException, ??????????????IOException?{ ??????????Properties?props?=?new?Properties(); ??????????props.setProperty("mail.store.protocol",?"pop3"); ??????????props.setProperty("mail.pop3.host",?"pop3.sohu.com"); ??????????Session?session?=?Session.getDefaultInstance(props); ??????????Store?store?=?session.getStore("pop3"); ??????????store.connect("username@sohu.com",?"password"); ??????????Folder?folder?=?store.getFolder("INBOX"); ??????????folder.open(Folder.READ_WRITE); ??????????????????????int?messageCount?=?folder.getMessageCount(); ??????????System.out.println(messageCount); ??????????Message[]?messages?=?folder.getMessages(); ??????????for?(int?i?=?0;?i?<?messages.length;?i++)?{ ??????????????Message?message?=?messages[i]; ??????????????System.out.println(message.getSubject()); ?????????????????????????????????????????????????????????????????????? ????????????????????????????? ? ?? ????????????????System.out.println("發(fā)送時間:"?+?message.getSentDate()); ??????????????System.out.println("主題:"?+?message.getSubject()); ??????????????System.out.println("內(nèi)容:"?+?message.getContent()); ????????????????????????????Enumeration?headers?=?message.getAllHeaders(); ??????????????System.out.println("----------------------allHeaders-----------------------------"); ??????????????????????????? ????????????????????????????Object?content?=?message.getContent(); ??????????????if?(content?instanceof?MimeMultipart)?{ ??????????????????MimeMultipart?multipart?=?(MimeMultipart)?content; ??????????????????parseMultipart(multipart); ??????????????} ??????????????System.out ??????????????????????.println("========================================================"); ??????????????System.out ??????????????????????.println("========================================================"); ????????????} ????????????folder.close(true); ??????????store.close(); ??????} ?????? ?????????????????public?static?void?parseMultipart(Multipart?multipart)?throws?MessagingException,?IOException?{ ??????????int?count?=?multipart.getCount(); ??????????System.out.println("couont?=??"+count); ??????????for?(int?idx=0;idx<count;idx++)?{ ??????????????BodyPart?bodyPart?=?multipart.getBodyPart(idx); ??????????????System.out.println(bodyPart.getContentType()); ??????????????if?(bodyPart.isMimeType("text/plain"))?{ ??????????????????System.out.println("plain................."+bodyPart.getContent()); ??????????????}?else?if(bodyPart.isMimeType("text/html"))?{ ??????????????????System.out.println("html..................."+bodyPart.getContent()); ??????????????}?else?if(bodyPart.isMimeType("multipart/*"))?{ ??????????????????Multipart?mpart?=?(Multipart)bodyPart.getContent(); ??????????????????parseMultipart(mpart); ?????????????????? ??????????????}?else?if?(bodyPart.isMimeType("application/octet-stream"))?{ ??????????????????String?disposition?=?bodyPart.getDisposition(); ??????????????????System.out.println(disposition); ??????????????????if?(disposition.equalsIgnoreCase(BodyPart.ATTACHMENT))?{ ??????????????????????String?fileName?=?bodyPart.getFileName(); ??????????????????????InputStream?is?=?bodyPart.getInputStream(); ??????????????????????copy(is,?new?FileOutputStream("D:\\"+fileName)); ??????????????????} ??????????????} ??????????} ??????} ?????? ??????/** ???????*?文件拷貝,在用戶進行附件下載的時候,可以把附件的InputStream傳給用戶進行下載 ???????*?@param?is ???????*?@param?os ???????*?@throws?IOException ???????*/ ??????public?static?void?copy(InputStream?is,?OutputStream?os)?throws?IOException?{ ??????????byte[]?bytes?=?new?byte[1024]; ??????????int?len?=?0; ??????????while?((len=is.read(bytes))?!=?-1?)?{ ??????????????os.write(bytes,?0,?len); ??????????} ??????????if?(os?!=?null) ??????????????os.close(); ??????????if?(is?!=?null) ??????????????is.close(); ??????} ?????? ????}??
package com.clxx.test;import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeMultipart;public class Email2 {public static void main(String args[]) throws MessagingException,IOException {Properties props = new Properties();props.setProperty("mail.store.protocol", "pop3");props.setProperty("mail.pop3.host", "pop3.sohu.com");Session session = Session.getDefaultInstance(props);Store store = session.getStore("pop3");store.connect("username@sohu.com", "password");Folder folder = store.getFolder("INBOX");folder.open(Folder.READ_WRITE);// 全部郵件數(shù)int messageCount = folder.getMessageCount();System.out.println(messageCount);Message[] messages = folder.getMessages();for (int i = 0; i < messages.length; i++) {Message message = messages[i];System.out.println(message.getSubject());// 刪除郵件// message.setFlag(Flags.Flag.DELETED, true);// 標記為已讀// message.setFlag(Flags.Flag.SEEN, true);//pop3沒有判斷郵件是否為已讀的功能,要使用Imap才可以/*Flags flags = message.getFlags();if (flags.contains(Flags.Flag.SEEN))System.out.println("這是一封已讀郵件");else {System.out.println("未讀郵件");message.setFlag(Flags.Flag.SEEN, true);}*/System.out.println("發(fā)送時間:" + message.getSentDate());System.out.println("主題:" + message.getSubject());System.out.println("內(nèi)容:" + message.getContent());//獲取所有的Header,頭信息Enumeration headers = message.getAllHeaders();System.out.println("----------------------allHeaders-----------------------------");/* while (headers.hasMoreElements()) {Header header = (Header)headers.nextElement();System.out.println(header.getName()+" ======= "+header.getValue());}*///解析郵件內(nèi)容Object content = message.getContent();if (content instanceof MimeMultipart) {MimeMultipart multipart = (MimeMultipart) content;parseMultipart(multipart);}System.out.println("========================================================");System.out.println("========================================================");}folder.close(true);store.close();}/*** 對復雜郵件的解析* @param multipart* @throws MessagingException* @throws IOException*/public static void parseMultipart(Multipart multipart) throws MessagingException, IOException {int count = multipart.getCount();System.out.println("couont = "+count);for (int idx=0;idx<count;idx++) {BodyPart bodyPart = multipart.getBodyPart(idx);System.out.println(bodyPart.getContentType());if (bodyPart.isMimeType("text/plain")) {System.out.println("plain................."+bodyPart.getContent());} else if(bodyPart.isMimeType("text/html")) {System.out.println("html..................."+bodyPart.getContent());} else if(bodyPart.isMimeType("multipart/*")) {Multipart mpart = (Multipart)bodyPart.getContent();parseMultipart(mpart);} else if (bodyPart.isMimeType("application/octet-stream")) {String disposition = bodyPart.getDisposition();System.out.println(disposition);if (disposition.equalsIgnoreCase(BodyPart.ATTACHMENT)) {String fileName = bodyPart.getFileName();InputStream is = bodyPart.getInputStream();copy(is, new FileOutputStream("D:\\"+fileName));}}}}/*** 文件拷貝,在用戶進行附件下載的時候,可以把附件的InputStream傳給用戶進行下載* @param is* @param os* @throws IOException*/public static void copy(InputStream is, OutputStream os) throws IOException {byte[] bytes = new byte[1024];int len = 0;while ((len=is.read(bytes)) != -1 ) {os.write(bytes, 0, len);}if (os != null)os.close();if (is != null)is.close();}}
?
?
使用imap協(xié)議接收郵件:
Java代碼 ?
package?com.clxx.test; ????import?java.util.Properties; ????import?javax.mail.Flags; ??import?javax.mail.Folder; ??import?javax.mail.Message; ??import?javax.mail.Session; ????import?com.sun.mail.imap.IMAPFolder; ??import?com.sun.mail.imap.IMAPStore; ???????????public?class?Email3?{ ????????public?static?void?main(String[]?args)?throws?Exception?{ ??????????String?user?=?"username@sohu.com";??????????String?password?=?"password";?????????????Properties?prop?=?System.getProperties(); ??????????prop.put("mail.store.protocol",?"imap"); ??????????prop.put("mail.imap.host",?"imap.sohu.com"); ????????????Session?session?=?Session.getInstance(prop); ?????????? ??????????int?total?=?0; ??????????IMAPStore?store?=?(IMAPStore)?session.getStore("imap");???????????store.connect(user,?password); ??????????IMAPFolder?folder?=?(IMAPFolder)?store.getFolder("INBOX");???????????folder.open(Folder.READ_WRITE); ????????????????????total?=?folder.getMessageCount(); ??????????System.out.println("-----------------共有郵件:"?+?total ??????????????????+?"?封--------------"); ????????????????????System.out.println("未讀郵件數(shù):"?+?folder.getUnreadMessageCount()); ??????????Message[]?messages?=?folder.getMessages(); ??????????int?messageNumber?=?0; ??????????for?(Message?message?:?messages)?{ ??????????????System.out.println("發(fā)送時間:"?+?message.getSentDate()); ??????????????System.out.println("主題:"?+?message.getSubject()); ??????????????System.out.println("內(nèi)容:"?+?message.getContent()); ??????????????Flags?flags?=?message.getFlags(); ??????????????if?(flags.contains(Flags.Flag.SEEN)) ??????????????????System.out.println("這是一封已讀郵件"); ??????????????else?{ ??????????????????System.out.println("未讀郵件"); ??????????????} ??????????????System.out ??????????????????????.println("========================================================"); ??????????????System.out ??????????????????????.println("========================================================"); ????????????????????????????messageNumber?=?message.getMessageNumber(); ??????????} ??????????Message?message?=?folder.getMessage(messageNumber); ??????????System.out.println(message.getContent()+message.getContentType()); ????????????????????if?(folder?!=?null) ??????????????folder.close(true);? ??????????if?(store?!=?null) ??????????????store.close(); ??????} ????}??
?
總結
以上是生活随笔為你收集整理的使用javaMail收邮件 支持附件下载的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。