如何解决JavaMail的问题 - > javax.mail.MessagingException:220

时间:2011-09-13 21:02:57

标签: java exchange-server javamail

我正在尝试与我公司的Exchange Server生成连接,并从中检索一些电子邮件。我正在使用我从以下页面http://www.vipan.com/htdocs/javamail.html获取的代码,并且我能够连接,但是我收到以下错误:

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.search.*;
import javax.activation.*;


public class FetchMailUsage {

public static void main(String[] args) {

    // SUBSTITUTE YOUR ISP's POP3 SERVER HERE!!!
    String host = "pop.yourisp.net";
    // SUBSTITUTE YOUR USERNAME AND PASSWORD TO ACCESS E-MAIL HERE!!!
    String user = "your_username";
    String password = "your_password";
    // SUBSTITUTE YOUR SUBJECT SUBSTRING TO SEARCH HERE!!!
    String subjectSubstringToSearch = "Test E-Mail through Java";

    Properties props = new Properties();
    props.put("mail.smtp.host", host);
    // To see what is going on behind the scene
    props.put("mail.debug", "true");
    props.put("mail.imap.port", "25");

    // Get a session.  Use a blank Properties object.
    Session session = Session.getInstance(props, null);

    try {

        // Get a Store object
        Store store = session.getStore("pop3");
        store.connect(host, user, password);

        // Get "INBOX"
        Folder fldr = store.getFolder("INBOX");
        fldr.open(Folder.READ_WRITE);
        int count = fldr.getMessageCount();
        System.out.println(count  + " total messages");

        // Message numebers start at 1
        for(int i = 1; i <= count; i++) {
                            // Get  a message by its sequence number
            Message m = fldr.getMessage(i);

            // Get some headers
            Date date = m.getSentDate();
            Address [] from = m.getFrom();
            String subj = m.getSubject();
            String mimeType = m.getContentType();
            System.out.println(date + "\t" + from[0] + "\t" +
                                subj + "\t" + mimeType);
        }

        // Search for e-mails by some subject substring
        String pattern = subjectSubstringToSearch;
        SubjectTerm st = new SubjectTerm(pattern);
        // Get some message references
        Message [] found = fldr.search(st);

        System.out.println(found.length +
                            " messages matched Subject pattern \"" +
                            pattern + "\"");

        for (int i = 0; i < found.length; i++) {
            Message m = found[i];
            // Get some headers
            Date date = m.getSentDate();
            Address [] from = m.getFrom();
            String subj = m.getSubject();
            String mimeType = m.getContentType();
            System.out.println(date + "\t" + from[0] + "\t" +
                                subj + "\t" + mimeType);

            Object o = m.getContent();
            if (o instanceof String) {
                System.out.println("**This is a String Message**");
                System.out.println((String)o);
            }
            else if (o instanceof Multipart) {
                System.out.print("**This is a Multipart Message.  ");
                Multipart mp = (Multipart)o;
                int count3 = mp.getCount();
                System.out.println("It has " + count3 +
                    " BodyParts in it**");
                for (int j = 0; j < count3; j++) {
                    // Part are numbered starting at 0
                    BodyPart b = mp.getBodyPart(j);
                    String mimeType2 = b.getContentType();
                    System.out.println( "BodyPart " + (j + 1) +
                                        " is of MimeType " + mimeType);

                    Object o2 = b.getContent();
                    if (o2 instanceof String) {
                        System.out.println("**This is a String BodyPart**");
                        System.out.println((String)o2);
                    }
                    else if (o2 instanceof Multipart) {
                        System.out.print(
                            "**This BodyPart is a nested Multipart.  ");
                        Multipart mp2 = (Multipart)o2;
                        int count2 = mp2.getCount();
                        System.out.println("It has " + count2 +
                            "further BodyParts in it**");
                    }
                    else if (o2 instanceof InputStream) {
                        System.out.println(
                            "**This is an InputStream BodyPart**");
                    }
                } //End of for
            }
            else if (o instanceof InputStream) {
                System.out.println("**This is an InputStream message**");
                InputStream is = (InputStream)o;
                // Assumes character content (not binary images)
                int c;
                while ((c = is.read()) != -1) {
                    System.out.write(c);
                }
            }

            // Uncomment to set "delete" flag on the message
            //m.setFlag(Flags.Flag.DELETED,true);

        } //End of for

        // "true" actually deletes flagged messages from folder
        fldr.close(true);
        store.close();

    }
    catch (MessagingException mex) {
        // Prints all nested (chained) exceptions as well
        mex.printStackTrace();
    }
    catch (IOException ioex) {
        ioex.printStackTrace();

       }
     }

} //End of class

错误的痕迹如下:

DEBUG: JavaMail version 1.4.4
DEBUG: URL jar:file:/C:/Documents%20and%20Settings/me/My%20Documents/QA/Automation/javamail-1.4.4/lib/smtp.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/C:/Documents%20and%20Settings/me/My%20Documents/QA/Automation/javamail-1.4.4/lib/smtp.jar!/META-INF/javamail.providers
DEBUG: URL jar:file:/C:/Documents%20and%20Settings/me/My%20Documents/QA/Automation/javamail-1.4.4/lib/imap.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/C:/Documents%20and%20Settings/me/My%20Documents/QA/Automation/javamail-1.4.4/lib/imap.jar!/META-INF/javamail.providers
DEBUG: URL jar:file:/C:/Documents%20and%20Settings/me/My%20Documents/QA/Automation/javamail-1.4.4/lib/pop3.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/C:/Documents%20and%20Settings/me/My%20Documents/QA/Automation/javamail-1.4.4/lib/pop3.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsy stems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: URL jar:file:/C:/Documents%20and%20Settings/me/My%20Documents/QA/Automation/javamail-1.4.4/lib/smtp.jar!/META-INF/javamail.address.map
DEBUG: successfully loaded resource: jar:file:/C:/Documents%20and%20Settings/me/My%20Documents/QA/Automation/javamail-1.4.4/lib/smtp.jar!/META-INF/javamail.address.map
DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: trying to connect to host "167.107.252.184", port 25, isSSL false
220 SERVER Microsoft ESMTP MAIL Service, Version: 6.0.3790.4675 ready at  Tue, 13 Sep 2011 15:38:38 -0500 
javax.mail.MessagingException: 220 SERVER Microsoft ESMTP MAIL Service, Version: 6.0.3790.4675 ready at  Tue, 13 Sep 2011 15:38:38 -0500 ;
  nested exception is:
    com.sun.mail.iap.ConnectionException: 220 SERVER Microsoft ESMTP MAIL Service, Version: 6.0.3790.4675 ready at  Tue, 13 Sep 2011 15:38:38 -0500 
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:663)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at com.test.ewacs.test.pageobjects.utilities.FetchMailUsage.main(FetchMailUsage.java:38)
Caused by: com.sun.mail.iap.ConnectionException: 220 SERVER Microsoft ESMTP MAIL Service, Version: 6.0.3790.4675 ready at  Tue, 13 Sep 2011 15:38:38 -0500 
    at com.sun.mail.imap.protocol.IMAPProtocol.processGreeting(IMAPProtocol.java:236)
    at com.sun.mail.iap.Protocol.<init>(Protocol.java:120)
    at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:110)
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:632)
    ... 3 more

1 个答案:

答案 0 :(得分:3)

我有一段时间没有涉及电子邮件,但我认为您的问题是您正在连接到SMTP端口(25),但您使用的协议是IMAP。

看看: http://www.hascode.com/2010/04/snippet-simple-one-minute-imap-client/