/* Read gmail box and save in db PostgresQL.
Copyright (C) 2008 Sergiy Soroka
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package mailerg;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import com.sun.mail.imap.IMAPFolder;
public class Mail {
Properties props;
private IMAPFolder fld;
private Store store;
private Session session;
public int last_message_id;
private String user;
private String password;
public int getMessagesCount(){
int messagesCount=0;
if (!store.isConnected()){
try {
store.connect("imap.gmail.com", user,password);
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
Folder folder = store.getFolder("Inbox");
fld=(IMAPFolder)folder;
fld.open(Folder.READ_ONLY);
messagesCount=fld.getMessageCount();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return messagesCount;
}
public gMessage getMessage(int max_uid){
gMessage gm=new gMessage();
if (!store.isConnected()){
try {
store.connect("imap.gmail.com", user, password);
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
Folder folder;
try {
folder = store.getFolder("Inbox");
fld=(IMAPFolder)folder;
fld.open(Folder.READ_ONLY);
Message message=fld.getMessage(max_uid);
gm.message_id=max_uid;
gm.subject=message.getSubject();
try{
gm.message_body=(String) message.getContent();
}catch(ClassCastException e){
gm.message_body="";
e.printStackTrace();
}
gm.sender=message.getFrom()[0].toString();
fld.close(false);
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return gm;
}
Mail(String cUser,String cPassword){
props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
user=cUser;
password=cPassword;
try {
session = Session.getDefaultInstance(props, null);
store = session.getStore("imaps");
if (!store.isConnected()){
try {
store.connect("imap.gmail.com", user, password);
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} catch (NoSuchProviderException e) {
e.printStackTrace();
System.exit(1);
}
}
}