[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.0

using javamail to read email from exchange server

Asked by vlad_oz in JavaMail

Tags: javamail, exchange, read

Hi Experts!

I'm trying to write a java program to read emails from our company's exchange server. I've written similar programs before accessing a pop3 account, but I'm having problems accessing my email on the exchange server using the same technique. I understand that the Exchange server doesn't (by default anyway) use pop3, so I've tried using imap, but unsuccessfully. Below is my code (its modeled on one of the javamail tutorials). I've tried using different account name combinations ie. DMAIN_NAME\account_name etc. But nothing seems to work. the error message I keep getting is (I've turned session debugging on):

DEBUG: setDebug: JavaMail version 1.3
DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc]
javax.mail.MessagingException: Connection refused: connect;
  nested exception is:
      java.net.ConnectException: Connection refused: connect
      at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:303)
      at javax.mail.Service.connect(Service.java:233)
      at javax.mail.Service.connect(Service.java:134)
      at au.com.covermore.EmailReader.receive(EmailReader.java:89)
      at au.com.covermore.EmailReader.main(EmailReader.java:55)

The code:

/**
 * @author leiberov
 * this program is intended to retrieve and parse emails sent by the ashop
 * application, the information extracted from the emails is to be stored
 * in an xml or text file to be analised and reported from later
 */

package au.com.covermore;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;

public class EmailReader {

      /* variables */
      
      /**
       * @param args
       * popServer - the url of the POP3 server eg. pop.iprimus.com.au
       * popUser - POP server user name
       * popPassword - password associated with the above username
       */
      public static void main(String[] args)
      {
            // TODO Auto-generated method stub
            try
          {
                String popServer=args[0];
                String popUser=args[1];
                String popPassword=args[2];
               
                System.out.println("java EmailReader "
                            + popServer + " " + popUser + " " + popPassword);
               
                receive(popServer, popUser, popPassword);
          }
          catch (Exception ex)
          {
                System.out.println("Usage: EmailReader"
                            +" popServer popUser popPassword");
          }

          System.exit(0);

      }
      
      /**
       * this method will retrieve and read the emails from the INBOX
       * @param popServer
       * @param popUser
       * @param popPassword
       */
      public static void receive(String popServer, String popUser, String popPassword)
      {

            Store store=null;
            Folder folder=null;

          try
          {
                // -- Get hold of the default session --
                Properties props = System.getProperties();
                Session session = Session.getDefaultInstance(props, null);
                session.setDebug(true);
      
                // -- Get hold of a POP3 message store, and connect to it --
                //store = session.getStore("pop3");
                store = session.getStore("imap");
                store.connect(popServer, popUser, popPassword);
        
                // -- Try to get hold of the default folder --
                folder = store.getDefaultFolder();
                if (folder == null) throw new Exception("No default folder");
      
                // -- ...and its INBOX --
                folder = folder.getFolder("INBOX");
                if (folder == null) throw new Exception("No POP3 INBOX");
      
                // -- Open the folder for read only --
                folder.open(Folder.READ_ONLY);
      
                // -- Get the message wrappers and process them --
                Message[] msgs = folder.getMessages();
                
                System.out.println("ava au.com.covermore.EmailReader"
                            +" msgs " + msgs.length);
                
                for (int msgNum = 0; msgNum < msgs.length; msgNum++)
                {
                      printMessage(msgs[msgNum]);
                      //processMessage(msgs[msgNum]);
                }
      
          }
          catch (Exception ex)
          {
                ex.printStackTrace();
          }
          finally
          {
                // -- Close down nicely --
                try
                {
                      if (folder!=null) folder.close(false);
                      if (store!=null) store.close();
                }
                catch (Exception ex2) {ex2.printStackTrace();}
          }
      }
      
      /**
       * this method will print the message
       * @param message
       */
      public static void printMessage(Message message)
      {
            try
            {
                  // Get the header information
                  String from=((InternetAddress)message.getFrom()[0]).getPersonal();
                  if (from==null) from=((InternetAddress)message.getFrom()[0]).getAddress();
                  System.out.println("FROM: "+from);
                  
                  String subject=message.getSubject();
                  System.out.println("SUBJECT: "+subject);
                  
                  //String dateTime = message.getSentDate().toString();
                  System.out.println("DATE: "+ message.getSentDate());
                  
                  // -- Get the message part (i.e. the message itself) --
                  Part messagePart=message;
                  Object content=messagePart.getContent();
                  
                  // -- or its first body part if it is a multipart message --
                  if (content instanceof Multipart)
                  {
                        messagePart=((Multipart)content).getBodyPart(0);
                      System.out.println("[ Multipart Message ]");
                  }

                  // -- Get the content type --
                  String contentType=messagePart.getContentType();

                  // -- If the content is plain text, we can print it --
                  System.out.println("CONTENT:"+contentType);

                  if (contentType.startsWith("text/plain") || contentType.startsWith("text/html"))
                  {
                        InputStream is = messagePart.getInputStream();

                        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                        String thisLine=reader.readLine();
                        
                        while (thisLine!=null)
                        {
                              System.out.println(thisLine);
                              thisLine=reader.readLine();
                        }
                  }

                  System.out.println("-----------------------------");
            }
            catch (Exception ex)
            {
                  ex.printStackTrace();
            }
      }
      
      /**
       * this method will process an email message and write
       * relevant contents into the xml file
       * @param message
       */
      private static void processMessage(Message message)
      {
            try
            {
                  // -- Get the message part (i.e. the message itself) --
                  Part messagePart=message;
                  Object content=messagePart.getContent();
            
                  // -- or its first body part if it is a multipart message --
                  if (content instanceof Multipart)
                  {
                        messagePart=((Multipart)content).getBodyPart(0);
                  }
                  
                  // -- Get the content type --
                  String contentType=messagePart.getContentType();
                  
                  if (contentType.startsWith("text/plain") || contentType.startsWith("text/html"))
                  {
                        InputStream is = messagePart.getInputStream();

                        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                                                
                        
                        // now read the message into a List - each message line is a list item
                        // a List will be easier to manipulate and its indexed
                        // remove blank lines at the same time
                        List msgList = new LinkedList();
                        
                        String thisLine=reader.readLine();
                        while (thisLine!=null)
                        {
                              if(thisLine.trim().length()>0)
                                    msgList.add(thisLine);
                              thisLine=reader.readLine();
                        }
                        
                  }
            }
            catch(Exception x)
            {
                  x.printStackTrace();
            }
            
      }
      
}
[+][-]03/25/07 09:12 PM, ID: 18790671Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/25/07 09:16 PM, ID: 18790684Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/25/07 09:32 PM, ID: 18790716Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/25/07 09:55 PM, ID: 18790776Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/25/07 10:12 PM, ID: 18790824Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: JavaMail
Tags: javamail, exchange, read
Sign Up Now!
Solution Provided By: Ajay-Singh
Participating Experts: 2
Solution Grade: B
 
[+][-]03/25/07 10:16 PM, ID: 18790833Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/28/07 06:25 PM, ID: 18813127Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/28/07 06:27 PM, ID: 18813137Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81