How do I get the "gmail inbox" using SMTP
I'm trying to get the contents of my gmail inbox through code using SMTP.
Below is my code snippet
public static SMTPTransport connectToSmtp(String host,
int port,
String userEmail,
String oauthToken,
boolean debug) throws
Exception {
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.sasl.enable", "true");
props.put("mail.smtp.sasl.mechanisms", "XOAUTH2");
props.put(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, oauthToken);
Session session = Session.getInstance(props);
session.setDebug(debug);
final URLName unusedUrlName = null;
SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
// If the password is non-null, SMTP tries to do AUTH LOGIN.
final String emptyPassword = null;
transport.connect(host, port, userEmail, emptyPassword);
System.out.println("The return code is
"+transport.getLastReturnCode());//prints 250 so Im guessing the
connection is successful
return transport;
}
And here is the caller
public static void main(String args[]) throws Exception {
String email = "username@gmail.com";
String oauthToken = GoogleExample.getOAuthCode();
initialize();
SMTPTransport smtpTransport = connectToSmtp("smtp.gmail.com",
587,
email,
oauthToken,
true);
// **How do I retreive contents of the inbox from a validated
SMTPTransport object?**
System.out.println("Successfully authenticated to SMTP.");
}
Any links or code snippets would be helpful
No comments:
Post a Comment