KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scioworks > imap > presentation > security > DefaultEmailLogin


1 /* -----------------------------------------------------------------------------
2  * Copyright (c) 2001, Low Kin Onn
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *
8  * Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * Neither name of the Scioworks Pte. Ltd. nor the names of its contributors
16  * may beused to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * -----------------------------------------------------------------------------
31  */

32
33 package scioworks.imap.presentation.security;
34
35 import java.util.Properties JavaDoc;
36
37 import javax.mail.*;
38 import javax.mail.internet.*;
39 import javax.activation.*;
40
41 import scioworks.imap.spec.ImapWebConstant;
42 import scioworks.imap.presentation.ImapWebSessionData;
43
44 public class DefaultEmailLogin {
45
46   String JavaDoc username;
47   String JavaDoc password;
48
49   ImapWebSessionData sessionData;
50
51   public DefaultEmailLogin(String JavaDoc username, String JavaDoc password) {
52     this.username = username;
53     this.password = password;
54   }
55
56   public void login() throws ACLException {
57
58     // Get a Store object
59
Store store = null;
60
61     // Get a Properties object
62
Properties JavaDoc props = System.getProperties();
63     // set the SMTP host
64
props.put("mail.smtp.host", ImapWebConstant.singleton().smtpHost());
65
66     // Get a Session object
67
Session session = Session.getDefaultInstance(props, null);
68
69     // set debug on
70
// session.setDebug(true);
71

72     try {
73       store = session.getStore("imap");
74
75     } catch (NoSuchProviderException e) {
76       throw new ACLException("Error getting IMAP provider.", e);
77     }
78
79     try {
80       store.connect(ImapWebConstant.singleton().imapHost(),
81                     ImapWebConstant.singleton().imapPort(),
82                     username, password);
83
84     } catch (MessagingException e) {
85        
86       throw new ACLException("Error connection to email server.", e);
87    
88     }
89
90     // If we are here, it means that the login to the email server is ok
91

92     // Create a ImapWebSessionData obj, set the values and return
93
URLName url = new URLName("imap",
94                               ImapWebConstant.singleton().imapHost(),
95                               ImapWebConstant.singleton().imapPort(),
96                               "INBOX",
97                               username,
98                               password);
99
100     //try {
101
//Folder folder = store.getFolder("INBOX");
102
//folder.open(Folder.READ_WRITE);
103

104     sessionData = new ImapWebSessionData();
105     sessionData.setImapURL (url);
106     sessionData.setImapSession(session);
107     sessionData.setImapStore (store);
108     //sessionData.setFolder (folder);
109
/*
110     } catch (MessagingException e) {
111       e.printStackTrace();
112     }
113 */

114   }
115
116   public ImapWebSessionData getSessionData() {
117     return sessionData;
118   }
119 }
120
Popular Tags