cancel
Showing results for 
Search instead for 
Did you mean: 

Getting Exception of MessagingException

dhruv_shah3
Active Contributor
0 Kudos

Hi All,

I am developing Email Application which retrieve the emails from the Gmail Account.

But I am getting the Exception:

1)

javax.mail.MessagingException: Connect failed; nested exception is: java.io.IOException: Couldn't connect using "javax.net.ssl.SSLSocketFactory" socket factory to host, port: pop.gmail.com, 995; Exception: java.lang.reflect.InvocationTargetException

2)

javax.mail.MessagingException: Not connected

Following is my program.


// ---------------------------------------------------------------------------
// This file has been generated partially by the Web Dynpro Code Generator.
// MODIFY CODE ONLY IN SECTIONS ENCLOSED BY @@begin AND @@end.
// ALL OTHER CHANGES WILL BE LOST IF THE FILE IS REGENERATED.
// ---------------------------------------------------------------------------
package com.sap.training;

// 
// IMPORTANT NOTE: 
// _ALL_ IMPORT STATEMENTS MUST BE PLACED IN THE FOLLOWING SECTION ENCLOSED
// BY @@begin imports AND @@end. FURTHERMORE, THIS SECTION MUST ALWAYS CONTAIN
// AT LEAST ONE IMPORT STATEMENT (E.G. THAT FOR IPrivateAccessGmailComp).
// OTHERWISE, USING THE ECLIPSE FUNCTION "Organize Imports" FOLLOWED BY
// A WEB DYNPRO CODE GENERATION (E.G. PROJECT BUILD) WILL RESULT IN THE LOSS
// OF IMPORT STATEMENTS.
//
//@@begin imports
import java.io.IOException;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;

import com.sap.training.wdp.IPrivateAccessGmailComp;
import com.sap.training.wdp.IPublicAccessGmailComp;
//@@end

//@@begin documentation
//@@end

public class AccessGmailComp
{
  /**
   * Logging location.
   */
  private static final com.sap.tc.logging.Location logger = 
    com.sap.tc.logging.Location.getLocation(AccessGmailComp.class);

  static 
  {
    //@@begin id
    String id = "$Id$";
    //@@end
    com.sap.tc.logging.Location.getLocation("ID.com.sap.tc.webdynpro").infoT(id);
  }

  /**
   * Private access to the generated Web Dynpro counterpart 
   * for this controller class.  </p>
   *
   * Use <code>wdThis</code> to gain typed access to the context,
   * to trigger navigation via outbound plugs, to get and enable/disable
   * actions, fire declared events, and access used controllers and/or 
   * component usages.
   *
   * @see com.sap.training.wdp.IPrivateAccessGmailComp for more details
   */
  private final IPrivateAccessGmailComp wdThis;

  /**
   * Root node of this controller's context. </p>
   *
   * Provides typed access not only to the elements of the root node 
   * but also to all nodes in the context (methods node<i>XYZ</i>()) 
   * and their currently selected element (methods current<i>XYZ</i>Element()). 
   * It also facilitates the creation of new elements for all nodes 
   * (methods create<i>XYZ</i>Element()). </p>
   *
   * @see com.sap.training.wdp.IPrivateAccessGmailComp.IContextNode for more details.
   */
  private final IPrivateAccessGmailComp.IContextNode wdContext;

  /**
   * A shortcut for <code>wdThis.wdGetAPI()</code>. </p>
   * 
   * Represents the generic API of the generic Web Dynpro counterpart 
   * for this controller. </p>
   */
  private final com.sap.tc.webdynpro.progmodel.api.IWDComponent wdControllerAPI;
  
  /**
   * A shortcut for <code>wdThis.wdGetAPI().getComponent()</code>. </p>
   * 
   * Represents the generic API of the Web Dynpro component this controller 
   * belongs to. Can be used to access the message manager, the window manager,
   * to add/remove event handlers and so on. </p>
   */
  private final com.sap.tc.webdynpro.progmodel.api.IWDComponent wdComponentAPI;
  
  public AccessGmailComp(IPrivateAccessGmailComp wdThis)
  {
    this.wdThis = wdThis;
    this.wdContext = wdThis.wdGetContext();
    this.wdControllerAPI = wdThis.wdGetAPI();
    this.wdComponentAPI = wdThis.wdGetAPI().getComponent();
  }

  //@@begin javadoc:wdDoInit()
  /** Hook method called to initialize controller. */
  //@@end
  public void wdDoInit()
  {
    //@@begin wdDoInit()
    //@@end
  }

  //@@begin javadoc:wdDoExit()
  /** Hook method called to clean up controller. */
  //@@end
  public void wdDoExit()
  {
    //@@begin wdDoExit()
    //@@end
  }

  //@@begin javadoc:wdDoPostProcessing()
  /**
   * Hook called to handle data retrieval errors before rendering.
   *
   * After doModifyView(), the Web Dynpro Framework gets all context data needed
   * for rendering by validating the contexts (which in turn calls the supply
   * functions and supplying relation roles). In this hook, the application
   * should handle the errors which occurred during validation of the contexts.
   * 
   * Using preorder depth-first traversal, this hook is called for all component
   * controllers starting with the current root component.
   *
   * Permitted operations:
   * - Flushing model queue
   * - Creating messages
   * - Reading context and model data
   *
   * Forbidden operations: 
   * - Invalidating model data
   * - Manipulating the context
   * - Firing outbound plugs
   * - Creating components
   * - ...   
   *
   * @param isCurrentRoot true if this is the root of the current request
   */
  //@@end
  public void wdDoPostProcessing(boolean isCurrentRoot)
  {
    //@@begin wdDoPostProcessing()
    //@@end
  }

  //@@begin javadoc:wdDoBeforeNavigation()
  /**
   * Hook before the navigation phase starts.
   *
   * This hook allows you to flush the model queue and handle any
   * errors that occur. Firing outbound plugs is allowed in this hook.
   *
   * Using preorder depth-first traversal, this hook is called for all component
   * controllers starting with the current root component.
   *
   * @param isCurrentRoot true if this is the root of the current request
   */
  //@@end
  public void wdDoBeforeNavigation(boolean isCurrentRoot)
  {
    //@@begin wdDoBeforeNavigation()
    //@@end
  }
  
  //@@begin javadoc:wdDoApplicationStateChange()
  /**
   * Hook that informs the application about a state change.
   * <p>
   * This hook is called e.g. to tell the application that will be
   * <ul>
   *  <li>left via a suspend plug and therefore should go into a suspend/sleep
   *      mode with minimal need of resources. errors that occur. Firing 
   *      outbound plugs is allowed in this hook.
   *  <li>left due to a timeout and could write it's state to a data base if the 
   *      user comes back later on
   * </ul>
   *
   * The concrete reason is available via IWDApplicationStateChangeInfo
   * <p>
   * <b>Important</b>: This hook is called for the top level component only!
   *
   * @param stateChangeInfo contains the information about the nature of the state change
   * @param stateChangeReturn allows the application to ask for a different state change. 
   *        The framework is allowed to ignore it considering i.e. the current resources situation.
   */
  //@@end
  public void wdDoApplicationStateChange(com.sap.tc.webdynpro.progmodel.api.IWDApplicationStateChangeInfo stateChangeInfo, com.sap.tc.webdynpro.progmodel.api.IWDApplicationStateChangeReturn stateChangeReturn)
  {
    //@@begin wdDoApplicationStateChange()
    //@@end
  }

  //@@begin javadoc:Login()
  /** Declared method. */
  //@@end
  public void Login( )
  {
    //@@begin Login()
    
	try

	{
		Properties props = System.getProperties(); 
	
		//props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY); 
		
		props.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
	
		props.setProperty("mail.pop3.socketFactory.fallback", "false"); 
	
		props.setProperty("mail.pop3.port", "995"); 
	
		props.setProperty("mail.pop3.socketFactory.port", "995"); 
	
		Session session = Session.getDefaultInstance(props,null);
	
		URLName urln = new URLName("pop3","pop.gmail.com",995,null,
		
		wdContext.nodeVnLogin().currentVnLoginElement().getVaUsername(),
		
		wdContext.nodeVnLogin().currentVnLoginElement().getVaPassword());
	
		store = session.getStore(urln);
	
		store.connect();
	
		wdComponentAPI.getMessageManager().reportSuccess("Suceesfully login"); 

	}

	catch(MessagingException e)

	{
	
		wdComponentAPI.getMessageManager().reportSuccess(e.toString());

	}


    
    //@@end
  }

  //@@begin javadoc:getInboxMsg()
  /** Declared method. */
  //@@end
  public void getInboxMsg( )
  {
    //@@begin getInboxMsg()
    
	try
	
		{
	
		Folder folder = store.getFolder("INBOX");
	
		folder.open(Folder.READ_ONLY);
	
		message = folder.getMessages();
	
		IPublicAccessGmailComp.IVnInboxElement inBoxElement;
	
			for(int i=0;i<message.length;i++)
	
			{
		
				inBoxElement=wdContext.nodeVnInbox().createVnInboxElement();
			
				inBoxElement.setVaFrom(message<i>.getFrom()[0].toString());
			
				inBoxElement.setVaSubject(message<i>.getSubject());
			
//				inBoxElement.setVaIndex(i); 
							
				wdContext.nodeVnInbox().addElement(inBoxElement);
		
			}

		wdComponentAPI.getMessageManager().reportSuccess("You have"+wdContext.nodeVnInbox().size()+"message");
	
		wdContext.nodeVnInbox().setLeadSelection(-1);

		}

	catch(MessagingException e)

	{
		wdComponentAPI.getMessageManager().reportSuccess(e.toString()); 

	}


    
    //@@end
  }

  //@@begin javadoc:getTextContent()
  /** Declared method. */
  //@@end
  public void getTextContent( )
  {
    //@@begin getTextContent()
    
	try

	{
		String temp;

		for(int j=0;j<wdContext.nodeVnInbox().size();j++)

		{
	
			if(j<0)
			{
				 temp = "false";
			}
			else
			{
				 temp = "true";
			}
			
			if(wdContext.nodeVnInbox().currentVnInboxElement().getVaIndex() == temp )

			  {

					Object content = message[j].getContent();
				
					Multipart multipart=(Multipart)content;
				
					Part part=multipart.getBodyPart(0);
				
					wdContext.currentContextElement().setCaBodyText(part.getContent().toString());

		   	 }

		}

	}

	catch(MessagingException e)

	{
		wdComponentAPI.getMessageManager().reportSuccess(e.toString());

	}
	catch(IOException e)

		{
			wdComponentAPI.getMessageManager().reportSuccess(e.toString());

		}

    
    //@@end
  }

  //@@begin javadoc:Logout()
  /** Declared method. */
  //@@end
  public void Logout( )
  {
    //@@begin Logout()
    
	try

	{

		store.close(); 

	}

	catch(MessagingException e)

	{

		wdComponentAPI.getMessageManager().reportSuccess(e.toString());

	}


    
    //@@end
  }

  /*
   * The following code section can be used for any Java code that is 
   * not to be visible to other controllers/views or that contains constructs
   * currently not supported directly by Web Dynpro (such as inner classes or
   * member variables etc.). </p>
   *
   * Note: The content of this section is in no way managed/controlled
   * by the Web Dynpro Designtime or the Web Dynpro Runtime. 
   */
  //@@begin others
  
  
  			//final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

  			Store store;

  			javax.mail.Message message[];

  
  //@@end
}

Pls help me out asap

Thanks & Regards,

Dhruv Shah

Edited by: DS on Feb 20, 2008 3:06 PM

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Dhruv,

Are you sure you can connect to port 995 from the server?

Jeschael

Answers (2)

Answers (2)

nikhil_bose
Active Contributor
0 Kudos

Coding looks fine. Check in your Gmail account for POP enabled or not.

regards,

nikhil

ThatSAPGuy
Advisor
Advisor
0 Kudos

Dhruv-

Looks like a firewall problem. Contact your network administrator.

Cheers-

Atul