JavaMail Pop获取邮件

JavaMail Pop获取邮件

 

主要步骤如下:

设置服务器,设置登录名,设置密码

获取连接

获取所有的信息Message

根据自己需要信息获取Email

 

工具类如下:

 

 

import com.sun.mail.pop3.POP3Folder;

import org.apache.commons.logging.Log;

 

import javax.mail.*;

import javax.mail.internet.*;

import java.io.*;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

 

public class ReceiveMail {

 

    final Log LOGGER = LoggerFactory.getLogger(getClass());

 

    private int mailCounter; //邮件计数

 

    private int mailIndex; //邮件编号,即邮件在messages数组中的位置

 

    private int mailDownErrorCounter; //正在下载邮件时,出错的计数器

 

    private boolean[] recordFailure; //记录下载出错的邮件的序号

 

    private int totalRetryTimes; //总共重试次数

 

    private int retryTimeCounter; //记下重试的次数

 

    private boolean otherError; //若是在邮件正式下载之前出错,则置该值为true

 

    private String extension = ".eml"; //文件扩展名

 

    private Store store;

 

    private Folder folder;

 

    private Message[] messages;

 

    private Message message;

 

    private Part part;

 

    private String emlName;

 

    private String attachName;

 

    private int allMessageCount;

 

    private int messageCount;

 

    private String dateformat; //默认的日前显示格式

 

    //  private String propFile = MailConstants.PROPS_FILE_NAME;//用这个接口类的好处是更改配置文件路径的时候不需要更改每个类

 

    private String protocol = "pop3"; //服务协议

 

    private String mailHost; //服务器地址

 

    private String userName; //用户名

 

    private String password; //密码

 

    private String saveAttachPath; //附件下载后的存放目录

 

    private String saveEmlPath = "D://tempEML//"; //保存eml文件的路径

 

    public ReceiveMail() throws IOException {

        /*   FileProperties fp = new FileProperties(propFile);

fp.load();

protocol = fp.getProperty(MailConstants.RECV_PROTO);

mailHost = fp.getProperty(MailConstants.RECV_HOST);

userName = fp.getProperty(MailConstants.RECV_USER);

password = fp.getProperty(MailConstants.RECV_PASS);

saveAttachPath = fp.getProperty(MailConstants.RECV_ATTACH);

saveEmlPath = fp.getProperty(MailConstants.RECV_ROOT);

dateformat = fp.getProperty("mail.receive.dtfmat");

extension = fp.getProperty("mail.receive.extension");

totalRetryTimes = Integer

        .parseInt(fp.getProperty("mail.receive.retry"));*/

    }

 

 

    /**

     * 获得发件人的地址和姓名

     *

     * @throws Exception

     */

    public String getFrom() throws Exception {

        return getFrom(this.message);

    }

 

    public String getFrom(Message mimeMessage) throws Exception {

        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 = personal + "<" + from + ">";

        return fromaddr;

    }

 

    /**

     * 获得邮件的收件人,抄送,和密送的地址和姓名,根据所传递的参数的不同 * "to"----收件人 "cc"---抄送人地址

     * "bcc"---密送人地址

     */

    public String getTOAddress() throws Exception {

        return getMailAddress("TO", this.message);

    }

 

    public String getCCAddress() throws Exception {

        return getMailAddress("CC", this.message);

    }

 

    public String getBCCAddress() throws Exception {

        return getMailAddress("BCC", this.message);

    }

 

    public String getTOAddress(Message mimeMessage) throws Exception {

        return getMailAddress("TO", mimeMessage);

    }

 

    public String getCCAddress(Message mimeMessage) throws Exception {

        return getMailAddress("CC", mimeMessage);

    }

 

    public String getBCCAddress(Message mimeMessage) throws Exception {

        return getMailAddress("BCC", mimeMessage);

    }

 

    public String getMailAddress(String type) throws Exception {

        return getMailAddress(type, this.message);

    }

 

    public String getMailAddress(String type, Message mimeMessage) throws Exception {

        String mailaddr = "";

        String addtype = type.toUpperCase();

        InternetAddress[] address = null;

        if (addtype.equals("TO") || addtype.equals("CC")

                || addtype.equals("BCC")) {

            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 (address != null) {

                for (int i = 0; i < address.length; i++) {

                    String email = address[i].getAddress();

                    if (email == null)

                        email = "";

                    else {

                        email = MimeUtility.decodeText(email);

                    }

                    String personal = address[i].getPersonal();

                    if (personal == null)

                        personal = "";

                    else {

                        personal = MimeUtility.decodeText(personal);

                    }

                    String compositeto = personal + "<" + email + ">";

                    mailaddr += "," + compositeto;

                }

                mailaddr = mailaddr.substring(1);

            }

        } else {

            throw new Exception("Error emailaddr type!");

        }

        return mailaddr;

    }

 

    /**

     * 获得邮件主题

     */

    public String getSubject() throws MessagingException {

        return getSubject(this.message);

    }

 

    public String getSubject(Message mimeMessage) throws MessagingException {

        String subject = "";

        try {

            subject = MimeUtility.decodeText(mimeMessage.getSubject());

            if (subject == null)

                subject = "";

        } catch (Exception exce) {

        }

        return subject;

    }

 

    /**

     * 获得邮件发送日期

     */

    public String getSentDate() throws Exception {

        return getSentDate(this.message);

    }

 

    public String getSentDate(Message mimeMessage) throws Exception {

        Date sentdate = mimeMessage.getSentDate();

        SimpleDateFormat format = new SimpleDateFormat(dateformat);

        return format.format(sentdate);

    }

 

    /**

     * 判断此邮件是否需要回执,如果需要回执返回"true",否则返回"false"

     */

    public boolean getReplySign() throws MessagingException {

        return getReplySign(this.message);

    }

 

    public boolean getReplySign(Message mimeMessage) throws MessagingException {

        boolean replysign = false;

        String needreply[] = mimeMessage

                .getHeader("Disposition-Notification-To");

        if (needreply != null) {

            replysign = true;

        }

        return replysign;

    }

 

    /**

     * 获得此邮件的Message-ID

     */

    public String getMessageId() throws MessagingException {

        return getMessageId(this.message);

    }

 

    public String getMessageId(Message mimeMessage) throws MessagingException {

        return ((MimeMessage) mimeMessage).getMessageID();

    }

 

    /**

     * 初始化出错邮件数组

     */

    private void setRecordFailure() {

        this.recordFailure = new boolean[getMessageCount()];

    }

 

    /**

     * 返回出错数组

     *

     * @return

     */

    public boolean[] getRecordFailure() {

        return this.recordFailure;

    }

 

    /**

     * 判断此邮件是否已读,如果未读返回返回false,反之返回true

     */

    public boolean isNew() throws MessagingException {

        return isNew(this.message);

    }

 

    /**

     * 判断此邮件是否已读,如果未读返回返回false,反之返回true

     */

    public boolean isNew(Message mimeMessage) 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;

                break;

            }

        }

        return isnew;

    }

 

    /**

     * 判断此邮件是否包含附件

     */

    public boolean isContainAttach() throws Exception {

        return isContainAttach(this.part);

    }

 

    /**

     * 判断此邮件是否包含附件

     */

    public boolean isContainAttach(Part part) throws Exception {

        boolean attachflag = false;

        String contentType = part.getContentType();

        if (part.isMimeType("multipart/*")) {

            Multipart mp = (Multipart) part.getContent();

            for (int i = 0; i < mp.getCount(); i++) {

                BodyPart mpart = mp.getBodyPart(i);

                String disposition = mpart.getDisposition();

                if ((disposition != null)

                        && ((disposition.equals(Part.ATTACHMENT)) || (disposition

                        .equals(Part.INLINE))))

                    attachflag = true;

                else if (mpart.isMimeType("multipart/*")) {

                    attachflag = isContainAttach((Part) mpart);

                } else {

                    String contype = mpart.getContentType();

                    if (contype.toLowerCase().indexOf("application") != -1)

                        attachflag = true;

                    if (contype.toLowerCase().indexOf("name") != -1)

                        attachflag = true;

                }

            }

        } else if (part.isMimeType("message/rfc822")) {

            attachflag = isContainAttach((Part) part.getContent());

        }

        return attachflag;

    }

 

    /**

     * 连到server,创建folder对象,创建message对象

     */

    public void getConn() {

        try {

            this.getStoreFromServer();

            this.getFolderFromStore();

        } catch (Exception e) {

            otherError = true;

            mailDownErrorCounter++;

            LOGGER.debug(e.getLocalizedMessage());

        }

    }

 

    /**

     * 建立Store连接

     */

    private Store getStoreFromServer() throws Exception {

        //创建session

        Session session = Session.getDefaultInstance(System.getProperties(),

                null);

        //session.setDebug(true);

 

        //创建store,建立连接

        Store store = session.getStore(protocol);

        LOGGER.debug("connecting");

        store.connect(mailHost, userName, password);

        LOGGER.debug("connected successfully");

        setStore(store);

        return store;

    }

 

    /**

     * 打开INBox文件夹

     */

    private Folder getFolderFromStore() {

        //打开邮件相应文件夹

        Folder getFolder;

        try {

            getFolder = store.getFolder("INBOX");

            getFolder.open(Folder.READ_ONLY);

            setFolder(getFolder);

            return getFolder;

        } catch (MessagingException e) {

            // TODO Auto-generated catch block

            System.err.println("获取Folder失败!");

            e.printStackTrace();

        }

        return null;

    }

 

    /**

     * 从folder中提取所有的messages

     *

     * @throws MessagingException

     */

    public void getAllMessages() throws MessagingException {

        //从邮件文件夹获取邮件信息

        Message[] messages = folder.getMessages();

        setMessages(messages);

        setRecordFailure(); //初始化出错数组

        //        setMessageCount();

    }

 

    /**

     * 获得messageNums数组指定的message

     *

     * @param messageNums

     * @throws MessagingException

     */

    public void getMessages(int[] messageNums) throws MessagingException {

        Message[] messages = folder.getMessages(messageNums);

        setMessages(messages);

        setRecordFailure(); //初始化出错数组

        //        setMessageCount();

    }

 

    /**

     * 获得start和end之间的message

     *

     * @param start

     * @param end

     * @throws MessagingException

     */

    public void getMessages(int start, int end) throws MessagingException {

        Message[] messages = folder.getMessages(start, end);

        setMessages(messages);

        setRecordFailure(); //初始化出错数组

        //        setMessageCount();

    }

 

    /**

     * 关闭连接

     */

    public void closeConnection() {

        try {

            messages = null;

            message = null;

            if (folder.isOpen())

                folder.close(true);

            store.close();

            LOGGER.debug("close");

        } catch (Exception e) {

            LOGGER.debug("关闭和邮件服务器之间连接时出错!");

            e.printStackTrace();

        }

    }

 

    /**

     * 获得当前邮件的基本方法 Pop3Bean内部应该调用这个方法 以便在调用函数中加入重试机制

     *

     * @throws MessagingException

     * @throws MessagingException

     */

    public void getMail() throws Throwable { //抛出异常,用以重掷

        try {

            saveMessageAs(message);         //保存消息

            parseMessage(message);

        } catch (IOException e) {

            // TODO Auto-generated catch block

            System.err.println("保存邮件出错,检查保存路径");

            throw new IOException("保存邮件出错,检查保存路径");

        } catch (MessagingException e) {

            // TODO Auto-generated catch block

            System.err.println("邮件转换出错");

            throw new MessagingException("邮件转换出错");

        } catch (Exception e) {

            System.err.println("未知错误");

            otherError = true;

            e.printStackTrace();

            throw new Exception("未知错误");

        }

    }

 

    /**

     * 获得指定的邮件

     *

     * @param index

     */

    public void getMail(int index) {

        mailDownErrorCounter = 0; //邮件下载出错计数器置零

        try { //获取邮件下载之前的错误

            setMessage(messages[index]); //设置当前message

            LOGGER.debug("正在获取第" + index + "封邮件. . .");

            getMail(); //获取当前message

            LOGGER.debug("成功获取第" + index + "封邮件");

        } catch (Throwable e) { //获得重掷异常

            recordFailure[index] = true;

            mailDownErrorCounter++;

            System.err.println("下载第" + index + "封邮件时出错");

            retry();

        }

    }

 

    /**

     * 获取messages中的所有邮件

     */

    public void getAllMail() {

        int mailArrayLength; //将要下载的邮件的数量。若是重试时,则为还未下载的邮件数量

 

        mailArrayLength = getMessageCount();

 

        LOGGER.debug("一共有邮件" + mailArrayLength + "封");

 

        mailDownErrorCounter = 0; //邮件下载出错计数器置零

        mailCounter = 0;

        for (int index = 0; index < mailArrayLength; index++) {

            try {

                setMessage(messages[index]); //设置当前message

                LOGGER.debug("正在获取第" + index + "封邮件. . ." +

                        "/nsubject:" + getSubject() + "from:" + getFrom() + "toaddress:" + getTOAddress());

                getMail(); //获取当前message

                LOGGER.debug("成功获取第" + index + "封邮件");

                mailCounter++;

            } catch (Throwable e) {

                otherError = false;

                recordFailure[index] = true;

                mailDownErrorCounter++;

                System.err.println("下载第" + index + "封邮件时出错");

            }

        }

        LOGGER.debug("成功下载" + mailCounter + "封邮件");

        mailCounter = 0;

        if (mailDownErrorCounter != 0)

            retry();

    }

 

    /**

     * 获取messages中的系统退信

     */

    public void getReturnMessage(String[] Eamils, String[] Titles) {

        int mailArrayLength; //将要下载的邮件的数量。若是重试时,则为还未下载的邮件数量

        mailArrayLength = getMessageCount();

        if (mailArrayLength == 0) {//防止意外调用

            try {

                mailArrayLength = folder.getMessageCount();

                setMessages(folder.getMessages());

            } catch (MessagingException e) {

                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.

            }

        }

        ArrayList<Message> _messages = new ArrayList<Message>();

        for (int index = 0; index < mailArrayLength; index++) {

            try {

                setMessage(messages[index]); //设置当前message

                LOGGER.debug("/nsubject:" + getSubject() + "from:" + getFrom() + "toaddress:" + getTOAddress() + "/nUid:" + ((POP3Folder) folder).getUID(messages[index]));

 

                //先判断 邮箱是不是,如果是就不再判断,否则,在判断标题是不是

                boolean isReturn = false;

                for (String email : Eamils) {

                    if (("<" + email + ">").equals(getFrom())) {

                        isReturn = true;

                    }

                }

                if (!isReturn) {

                    for (String title : Titles) {

                        if (title.equals(getSubject())) {

                            isReturn = true;

                        }

                    }

                }

                if (isReturn) {

                    _messages.add(messages[index]);

                }

 

            } catch (Exception e) {

                LOGGER.debug(e.getMessage());

            }

 

        }

        setMessages(_messages.toArray(new Message[_messages.size()]));

    }

 

 

    public String getMailUid(Message m) {

        String _uid = null;

        if (m != null)

            setMessage(m);

        try {

            _uid = ((POP3Folder) folder).getUID(m);

        } catch (MessagingException e) {

            throw new RuntimeException(e.getMessage());

        }

        return _uid;

    }

 

    /**

     * 保存邮件源文件

     */

 

    public void saveMessageAs(Message message) {

        String oriFileName;

        String fileExtend;

 

        try {

            oriFileName = getInfoBetweenBrackets(getMessageId(message)

                    .toString());

            //设置文件后缀名。若是附件则设法取得其文件后缀名作为将要保存文件的后缀名,若是正文部分则用.htm做后缀名

            String emlName = oriFileName;

            String fileNameWidthExtension = getEmlPath() + oriFileName

                    + getExtension();

            File storeFile = new File(fileNameWidthExtension);

            for (int i = 0; storeFile.exists(); i++) {

                emlName = oriFileName + i;

                fileNameWidthExtension = getEmlPath() + emlName

                        + getExtension();

                storeFile = new File(fileNameWidthExtension);

            }

            setEmlName(emlName);

            LOGGER.debug("storefile's path: " + fileNameWidthExtension);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            message.writeTo(baos);

            StringReader in = new StringReader(baos.toString());

            saveFile(fileNameWidthExtension, in);

        } catch (MessagingException e) {

            // TODO Auto-generated catch block

            throw new RuntimeException(e.getMessage());

        } catch (Exception e) {

            // TODO Auto-generated catch block

            throw new RuntimeException(e.getMessage());

        }

    }

 

    /*

    * 解析邮件

    */

    public void parseMessage(Message message) throws IOException,

            MessagingException {

        Object content = message.getContent();

        if (content instanceof Multipart) {

            handleMultipart((Multipart) content);

        } else {

            handlePart(message);

        }

    }

 

    /*

    * 解析Multipart

    */

    public void handleMultipart(Multipart multipart) throws MessagingException,

            IOException {

        for (int i = 0, n = multipart.getCount(); i < n; i++) {

            handlePart(multipart.getBodyPart(i));

        }

    }

 

    /*

    * 解析指定part,从中提取文件

    */

    public void handlePart(Part part) throws MessagingException, IOException {

        String disposition = part.getDisposition(); // Find attachment

        String contentType = part.getContentType();

        String str;

        //测试先设置附件路径

        setAttachPath("d://test1//");

        InputStreamReader sbis = new InputStreamReader(part.getInputStream());

        if (disposition == null) { // When just body

            LOGGER.debug("Null: " + contentType);

            // Check if plain

            if ((contentType.length() >= 9)

                    && (contentType.toLowerCase().substring(0, 9)

                    .equals("text/plai"))) {

 

                LOGGER.debug(getAttachPath() + getEmlName() + ".txt");

                saveFile(getAttachPath() + getEmlName() + ".txt", sbis);

            } else if ((contentType.length() >= 8) // Check if html

                    && (contentType.toLowerCase().substring(0, 8)

                    .equals("text/htm"))) {

                saveFile(getAttachPath() + getEmlName() + ".html", sbis);

            } else if ((contentType.length() >= 9) // Check if html

                    && (contentType.toLowerCase().substring(0, 9)

                    .equals("image/gif"))) {

                saveFile(getAttachPath() + getEmlName() + ".gif", sbis);

            } else if ((contentType.length() >= 10)

                    && contentType.toLowerCase().substring(0, 10).equals(

                    "multipart/")) { // Check if multipart

                LOGGER.debug("multipart body: " + contentType);

                Multipart mp = (Multipart) (part.getContent());

                handleMultipart(mp);

            } else if ((contentType.length() >= 14)

                    && contentType.toLowerCase().substring(0, 14).equals(

                    "message/rfc822")) { // Check if multipart

                LOGGER.debug("Other body: " + contentType);

                saveFile(getAttachPath() + getEmlName() + ".eml", sbis);

                setAttachName(getEmlName() + ".eml");

            } else { // Unknown type

                LOGGER.debug("Other body: " + contentType);

                saveFile(getAttachPath() + getEmlName() + ".txt", sbis);

            }

        } else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {

            LOGGER.debug("Attachment: " + part.getFileName() + " : "

                    + contentType);

            //outToFile.println("Attachment: " + part.getFileName() + " : "

            //        + contentType);

            saveFile(getAttachPath() + part.getFileName(), sbis);

        } else if (disposition.equalsIgnoreCase(Part.INLINE)) {

            LOGGER.debug("Inline: " + part.getFileName() + " : "

                    + contentType);

            //outToFile.println("Inline: " + part.getFileName() + " : "

            //        + contentType);

            saveFile(getAttachPath() + part.getFileName(), sbis);

        } else { // Should never happen

            LOGGER.debug("Other: " + disposition);

            //            outToFile.println("Other: " + disposition);

        }

    }

 

    public void saveFile(String fileName, Reader input) throws IOException {

        if (fileName == null) {

            fileName = File.createTempFile(getAttachPath() + "xx", ".out")

                    .getName();

        }

        // Do no overwrite existing file

        File file = new File(fileName);

        int lastDot = fileName.lastIndexOf(".");

        String extension = fileName.substring(lastDot);

        String fullFileName = fileName;

        fileName = fileName.substring(0, lastDot);

        for (int i = 0; file.exists(); i++) {

            file = new File(fileName + i + extension);

        }

        FileWriter fos = new FileWriter(file);

        BufferedWriter bos = new BufferedWriter(fos);

        BufferedReader bis = new BufferedReader(input);

        int aByte;

        while ((aByte = bis.read()) != -1) {

            bos.write(aByte);

        }

        bos.flush();

        bos.close();

        bis.close();

    }

 

    /**

     * 读取eml文件

     *

     * @param fileName

     * @throws MessagingException

     */

    public void readEmlFile(String fileName) throws MessagingException {

        try {

            //TODO readEmlFile

            InputStream fis = new FileInputStream(fileName);

            Object emlObj = (Object) fis;

            Session mailSession = Session.getDefaultInstance(System.getProperties(), null);

            MimeMessage msg = new MimeMessage(mailSession, fis);

            message = msg;

 

        } catch (FileNotFoundException e) {

            // TODO Auto-generated catch block

            throw new RuntimeException(e.getMessage());

        }

    }

 

    /**

     * 用于标识字符串中的"<"和">"的位置

     *

     * @param str

     * @return

     * @throws Exception

     */

    public String getInfoBetweenBrackets(String str) throws Exception {

        int i, j; //用于标识字符串中的"<"和">"的位置

        if (str == null) {

            str = "error";

            return str;

        }

        i = str.lastIndexOf("<");

        j = str.lastIndexOf(">");

        if (i != -1 && j != -1)

            str = str.substring(i + 1, j);

        return str;

    }

 

    //当有邮件无法下载时进行重试

    private void retry() {

        mailCounter = 0;

        while (retryTimeCounter < totalRetryTimes && mailDownErrorCounter != 0) {

            if (!store.isConnected() || !folder.isOpen()) {

                System.err.println("与服务器连接断开,请重新连接");

                closeConnection();

                return;

            }

 

            LOGGER.debug("第" + (retryTimeCounter + 1) + "次重试");

 

            mailDownErrorCounter = 0; //邮件下载出错计数器置零

 

            for (int index = 0; index < getMessageCount(); index++) {

                if (recordFailure[index]) {

                    try {

                        setMessage(messages[index]); //设置当前message

                        LOGGER.debug("正在获取第" + index + "封邮件. . .");

                        getMail(); //获取当前message

                        LOGGER.debug("成功获取第" + index + "封邮件");

                        mailCounter++;

                        recordFailure[index] = false;

                    } catch (Throwable e) {

                        otherError = false;

                        recordFailure[index] = true;

                        mailDownErrorCounter++;

                        System.err.println("重新下载第" + index + "封邮件时出错");

                    }

                }

            }

            retryTimeCounter++;

        }

        LOGGER.debug("成功下载" + mailCounter + "封邮件");

        mailCounter = 0; //将邮件计数置零

        mailDownErrorCounter = 0; //下载错误数量归零

    }

 

 

    /**

     * 设置邮件主机

     */

    public void setMailHost(String mailHost) {

        this.mailHost = mailHost;

    }

 

    /**

     * 获取邮件主机

     */

    public String getMailHost() {

        return this.mailHost;

    }

 

    /**

     * 设置邮件帐号

     */

    public void setUserName(String userName) {

        this.userName = userName;

    }

 

    /**

     * 获取邮件帐号

     */

    public String getUserName() {

        return this.userName;

    }

 

    /**

     * 设置邮件密码

     */

    public void setPassword(String password) {

        this.password = password;

    }

 

    /**

     * 设置Store

     */

    public void setStore(Store store) {

        this.store = store;

    }

 

    /**

     * 设置邮箱文件夹

     */

    public void setFolder(Folder folder) {

        this.folder = folder;

    }

 

    /**

     * 设置messages数组

     */

    public void setMessages(Message[] messages) {

        this.messages = messages;

    }

 

    /**

     * 设置message

     */

    public void setMessage(Message message) {

        this.message = message;

    }

 

    public void setCurMessage(int i) {

        this.message = this.messages[i];

    }

 

    /**

     * 获取message

     */

    public Message getMessage() {

        return this.message;

    }

 

 

    /**

     * 获取folder中的message数量

     *

     * @throws MessagingException

     */

    public int getAllMessageCount() throws MessagingException {

        this.allMessageCount = folder.getMessageCount();

        return allMessageCount;

    }

 

    /**

     * 设置allMessageCount

     *

     * @throws MessagingException

     */

    private void setAllMessageCount() throws MessagingException {

        this.allMessageCount = this.folder.getMessageCount();

    }

 

    /**

     * 获取messages中message的数量

     *

     * @return

     */

    public int getMessageCount() {

        this.messageCount = this.messages.length;

        return messageCount;

    }

 

    /**

     * 获得folder中新邮件的数量

     *

     * @return

     * @throws MessagingException

     */

    public int getNewMessageCount() throws MessagingException {

        return this.folder.getNewMessageCount();

    }

 

    /**

     * 获得folder中未读邮件的数量

     *

     * @return

     * @throws MessagingException

     */

    public int getUnreadMessageCount() throws MessagingException {

        return this.folder.getUnreadMessageCount();

    }

 

    /**

     * 获取Part

     */

    public Part getPart() {

        return (Part) message;

    }

 

    /**

     * 设置Part

     */

    public void setPart(Part part) {

        this.part = part;

    }

 

    /**

     * 设置附件存放路径

     */

 

    public void setAttachPath(String attachPath) {

        this.saveAttachPath = attachPath;

    }

 

    /**

     * 获得附件存放路径

     */

 

    public String getAttachPath() {

        return saveAttachPath;

    }

 

    /**

     * 设置eml存放路径

     */

 

    public void setEmlPath(String emlPath) {

        this.saveEmlPath = emlPath;

    }

 

    /**

     * 获得eml存放路径

     */

 

    public String getEmlPath() {

        return saveEmlPath;

    }

 

    public void setEmlName(String emlName) {

        this.emlName = emlName;

    }

 

    public String getEmlName() {

        return emlName;

    }

 

    public void setAttachName(String attachName) {

        this.attachName = attachName;

    }

 

    public String getAttachName() {

        return attachName;

    }

 

    public void setExtension(String extension) {

        this.extension = extension;

    }

 

    public String getExtension() {

        return extension;

    }

 

    /**

     * 设置日期显示格式

     */

 

    public void setDateFormat(String format) throws Exception {

        this.dateformat = format;

    }

 

    /**

     * 获取日期显示格式

     */

    public String getDateFormat(String format) throws Exception {

        return this.dateformat;

    }

}
测试方法如下:
public static void main(String[] args) throws Throwable {
        try {
            receiveMail mail;
            mail = new receiveMail();
            mail.setUserName("abc");
            mail.setMailHost("pop3.sohu.com");
            mail.setPassword("***");
            mail.setAttachPath("e:/test/");
            mail.setExtension(".eml");
            mail.setDateFormat("yyyydddd");
            mail.getConn();
            LOGGER.debug("Count of messages in folder: " + mail.getAllMessageCount());
            LOGGER.debug("Count of new messages in folder: " + mail.getNewMessageCount());
            LOGGER.debug("Count of unread messages in folder: " + mail.getUnreadMessageCount());
            mail.getAllMessages();
            LOGGER.debug("Count of loaded messages: " + mail.getMessageCount());
            mail.getAllMail();
            mail.setCurMessage(0);
            LOGGER.debug(mail.getSubject());
            mail.closeConnection();
            //   ReadEml mail = new ReadEml("H://My_Soft_Works//java//jteam//jmail//received//41C95D0F.008CD1.01099.eml");
        } catch (Exception e) {
            LOGGER.debug("出现未预料的错误!");
            throw new RuntimeException(e.getMessage());
        }
    }
这是在网上淘的:
我又添加了新的功能:根据邮箱地址和标题来获取某一封或部分邮件的功能
//emails:发件人的邮箱,titles:标题
getReturnMessage(String[] Eamils, String[] Titles);
这个方法把查找的邮件设置到  ReceiveMail  的属性 Message 里面
直接用 (ReceiveMail )mail.getAllMail();来获取需要的邮件(之前必须要getAllMessge())
例子:
           
            ReceiveMail mail = new ReceiveMail();
    mail.setUserName("youname@163.com");
            mail.setMailHost("pop.163.com");
            mail.setPassword("you password");
            mail.setAttachPath("d://");//
            mail.setEmlPath("d://eml");
            mail.setExtension(".eml");
            mail.setDateFormat("yyyydddd");
            mail.getConn();
            mail.getAllMessages();//获取所有邮件并设置到mail里面
            mail.getReturnMessage(_eamils, _titles);     //这里过滤掉非系统退信
            mail.getAllMail();
这时邮件已经下载到目录:d:/eml
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值