DefaultEmailLogin.java :  » J2EE » Enhydra-Demos » scioworks » imap » presentation » security » Java Open Source

Java Open Source » J2EE » Enhydra Demos 
Enhydra Demos » scioworks » imap » presentation » security » DefaultEmailLogin.java
/* -----------------------------------------------------------------------------
 * Copyright (c) 2001, Low Kin Onn
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither name of the Scioworks Pte. Ltd. nor the names of its contributors
 * may beused to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * -----------------------------------------------------------------------------
 */

package scioworks.imap.presentation.security;

import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

import scioworks.imap.spec.ImapWebConstant;
import scioworks.imap.presentation.ImapWebSessionData;

public class DefaultEmailLogin {

  String username;
  String password;

  ImapWebSessionData sessionData;

  public DefaultEmailLogin(String username, String password) {
    this.username = username;
    this.password = password;
  }

  public void login() throws ACLException {

    // Get a Store object
    Store store = null;

    // Get a Properties object
    Properties props = System.getProperties();
    // set the SMTP host
    props.put("mail.smtp.host", ImapWebConstant.singleton().smtpHost());

    // Get a Session object
    Session session = Session.getDefaultInstance(props, null);

    // set debug on
    // session.setDebug(true);

    try {
      store = session.getStore("imap");

    } catch (NoSuchProviderException e) {
      throw new ACLException("Error getting IMAP provider.", e);
    }

    try {
      store.connect(ImapWebConstant.singleton().imapHost(),
                    ImapWebConstant.singleton().imapPort(),
                    username, password);

    } catch (MessagingException e) {
       
      throw new ACLException("Error connection to email server.", e);
   
    }

    // If we are here, it means that the login to the email server is ok

    // Create a ImapWebSessionData obj, set the values and return
    URLName url = new URLName("imap",
                              ImapWebConstant.singleton().imapHost(),
                              ImapWebConstant.singleton().imapPort(),
                              "INBOX",
                              username,
                              password);

    //try {
    //Folder folder = store.getFolder("INBOX");
    //folder.open(Folder.READ_WRITE);

    sessionData = new ImapWebSessionData();
    sessionData.setImapURL    (url);
    sessionData.setImapSession(session);
    sessionData.setImapStore  (store);
    //sessionData.setFolder     (folder);
/*
    } catch (MessagingException e) {
      e.printStackTrace();
    }
*/
  }

  public ImapWebSessionData getSessionData() {
    return sessionData;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.