sitelink1 | https://sites.google.com/site/parn755/home/java |
---|---|
sitelink2 | |
sitelink3 | |
sitelink4 | |
sitelink5 | |
sitelink6 |
I tried to find out how to connect gmail server and retrieve mails but most information seem to be too old/ambigous or they do not show complete code.
So I'm trying to make valid sources. Below code just makes connection to pop3 server.
referenced links:
https://javamail.java.net/nonav/docs/JavaMail-1.5.pdf : Javamail API document ver 1.5
http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes : Javamail FAQ
Source:
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;
public class ReadTest {
static String protocol = "pop3";
static String host = "pop.gmail.com";
static String user = "gmail ID";
static String password = "password";
static String port = "995";
static String url = null;
static String root = null;
static String pattern = "%";
static boolean recursive = false;
static boolean verbose = false;
static boolean debug = true;
public static void main(String[] args) {
// Get a Properties object
Properties props = System.getProperties();
// Let's check out what's in default Properties
System.out.println(props.toString());
//Before opening session, set port number and ssl authentication
props.put("mail.pop3.port", "995");
props.put("mail.pop3.ssl.enable", "true");
//properties are added correctly?
System.out.println(props.toString());
// Get a Session object
Session session = Session.getInstance(props, null);
// let's see what's going on by setting debug as true
session.setDebug(debug);
// Get a Store object
Store store = null;
Folder rf = null;
try {
if (url != null) {
URLName urln = new URLName(url);
store = session.getStore(urln);
store.connect();
} else {
if (protocol != null) {
store = session.getStore(protocol);
}
else {
store = session.getStore();
}
// Connect
if (host != null || user != null || password != null) {
store.connect(host, user, password);
}
else {
store.connect();
}
}
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Result (omitted properties values)
DEBUG: setDebug: JavaMail version 1.5.0
DEBUG: getProvider() returning javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]
DEBUG POP3: mail.pop3.rsetbeforequit: false
DEBUG POP3: mail.pop3.disabletop: false
DEBUG POP3: mail.pop3.forgettopheaders: false
DEBUG POP3: mail.pop3.cachewriteto: false
DEBUG POP3: mail.pop3.filecache.enable: false
DEBUG POP3: mail.pop3.keepmessagecontent: false
DEBUG POP3: mail.pop3.starttls.enable: false
DEBUG POP3: mail.pop3.starttls.required: false
DEBUG POP3: mail.pop3.apop.enable: false
DEBUG POP3: mail.pop3.disablecapa: false
DEBUG POP3: connecting to host "pop.gmail.com", port 995, isSSL true
+OK Gpop ready for requests from 61.75.27.199 yw8pf6744109pac.0
CAPA
+OK Capability list follows
USER
RESP-CODES
EXPIRE 0
LOGIN-DELAY 300
TOP
UIDL
X-GOOGLE-VERHOEVEN
X-GOOGLE-RICO
.
DEBUG POP3: authentication command trace suppressed
DEBUG POP3: authentication command succeeded