how to read inbox using javamail from microsoft exchange server
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Neema_Jacob
Posted On:   Friday, January 21, 2011 02:15 AM

Hi, I want to read mails from microsoft exchange server using javamail.I use the following code String username = "ggg@fff.com"; String password = "sdasdas"; Properties props = System.getProperties(); Authenticator auth = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username , password ); } }; Session session = Session.getDefaultInstance(props, auth); System.out.   More>>

Hi,



I want to read mails from microsoft exchange server using javamail.I use the following code





String username = "ggg@fff.com";
String password = "sdasdas";




Properties props = System.getProperties();







Authenticator auth = new Authenticator()


{




public PasswordAuthentication getPasswordAuthentication()
{


return new PasswordAuthentication(username , password );




}


};





Session session = Session.getDefaultInstance(props, auth);





System.out.println(session);




Store store = session.getStore("imaps");




System.out.println(store);


store.connect(host, username, password);



// Get folder
Folder folder = store.getFolder("INBOX");


folder.open(Folder.READ_ONLY);


// Get directory
Message message[] = folder.getMessages();


for (int i=0, n=message.length; i

{
System.out.println(i + ": " + message[i].getFrom()[0]
+ " " + message[i].getSubject());


}


// Close connection
folder.close(false);


store.close();




But I got error

javax.mail.MessagingException: Connection timed out: connect;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:571)
at javax.mail.Service.connect(Service.java:288)
at javax.mail.Service.connect(Service.java:169)
at Shipmate.MyJavaMail.reaadMail(MyJavaMail.java:580)
at Shipmate.MyJavaMail.main(MyJavaMail.java:46)



I can access mails through Microsoft accesss office outlook in the configuration of that 1.it use connect to mymailbox using HTTP


2.In connection setting a Url is provided to connect to my proxyserver exchange.like https//sadsadsa.asdasds@asdsf.com


3.mutually authenticate the session with ssl and provide the principal name for proxy server


4.And also basic authentication is used for connecting my proxy server for exchange.


The above are the settings done in my microsoft outlook 2003 so that I can read the mail in that.



But I would like to read inbox using Javamail.How can I do that?


Thanks in Advance


Nimmi

   <<Less

Re: how to read inbox using javamail from microsoft exchange server

Posted By:   yves_arikoglu  
Posted On:   Wednesday, January 26, 2011 12:54 PM

Connecting to Exchange can be a real pain...
I havenĀ“t used a Proxy for a connection to exchange yet, but maybe my code helps you a little bit..:




import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Session;
import javax.mail.Store;

import com.sun.mail.imap.IMAPFolder;

public class IMAP_Connect {

static Store store=null;
static IMAPFolder inbox;
static String IMAPServer="your host";
static String username="your username";
static String password="your password";
static int IMAPPort=143;
static String Protocol="imap";

public static void main(String[] arg) throws Exception{
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = new Properties();
props.setProperty("mail.store.protocol", Protocol);
props.setProperty("mail.debug", "false");
props.setProperty("mail.imap.host", IMAPServer);
props.setProperty("mail.imap.port", IMAPPort+"");



if (mustUseSSL()){
props.setProperty( "mail.imap.socketFactory.class", SSL_FACTORY);
props.setProperty( "mail.imap.socketFactory.fallback", "false");
props.setProperty( "mail.imap.socketFactory.port", "993");
java.security.Security.setProperty( "ssl.SocketFactory.provider", SSL_FACTORY);
}

Session s = Session.getDefaultInstance(props, null);

try{
store = s.getStore(Protocol);
store.connect(IMAPServer, IMAPPort, username, password);
inbox = (IMAPFolder)store.getFolder("Inbox");
inbox.open(Folder.READ_WRITE);
System.out.println("Messages waiting: "+inbox.getMessageCount());
inbox.close(true);
}
catch (Exception e){
e.printStackTrace(System.out);
}
}


public static boolean mustUseSSL(){
return false;
}

}

About | Sitemap | Contact