开发者

How to solve issue with JavaMail -> javax.mail.MessagingException: 220

I'm trying to generate a connection with the Exchange Server of my company, and retrieve some email from it. I'm using a code that I took from the following page, http://www.vipan.com/htdocs/javamail.html , and I'm being able to get connected, but I'm getting the following error:

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)开发者_如何学Go;
        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

The trace of the error is the following:

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


I have not dabbled into email for a while but I think your issue is you are connecting to the SMTP port (25) but the protocol you are using is IMAP.

Take a look at: http://www.hascode.com/2010/04/snippet-simple-one-minute-imap-client/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜