tags:

views:

2195

answers:

5

I need a library that allows me to do email operations(e.g Sent/Receive mail) in Gmail using Java.

+7  A: 

Have you seen g4j - GMail API for Java?

GMailer API for Java (g4j) is set of API that allows Java programmer to communicate to GMail. With G4J programmers can made Java based application that based on huge storage of GMail.

Galwegian
What about using pop3? And does g4j stay current when/if gmail updates/changes their html rendererd client?
Zombies
+3  A: 

Have a look at GMail API for Java.

schnaader
+1  A: 

Hello,

First, configure your Gmail account to accept POP3 access. Then, simply access your mail account using Javamail !

romaintaz
even better: IMAP
jamesh
A: 

You can use the Javamail for that. The thing to remember is that GMail uses SMTPS not SMTP.

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

import java.util.Properties;


public class SimpleSSLMail {

    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final int SMTP_HOST_PORT = 465;
    private static final String SMTP_AUTH_USER = "[email protected]";
    private static final String SMTP_AUTH_PWD  = "mypwd";

    public static void main(String[] args) throws Exception{
       new SimpleSSLMail().test();
    }

    public void test() throws Exception{
        Properties props = new Properties();

        props.put("mail.transport.protocol", "smtps");
        props.put("mail.smtps.host", SMTP_HOST_NAME);
        props.put("mail.smtps.auth", "true");
        // props.put("mail.smtps.quitwait", "false");

        Session mailSession = Session.getDefaultInstance(props);
        mailSession.setDebug(true);
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject("Testing SMTP-SSL");
        message.setContent("This is a test", "text/plain");

        message.addRecipient(Message.RecipientType.TO,
             new InternetAddress("[email protected]"));

        transport.connect
          (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);

        transport.sendMessage(message,
            message.getRecipients(Message.RecipientType.TO));
        transport.close();
    }
}

ref : Send email with SMTPS (eg. Google GMail) (Javamail)

RealHowTo
+2  A: 

Variations of this question have been addressed in several earlier posts:

The general approach is to use IMAP/SMTP via JavaMail. The FAQ even has a special entry for working with Gmail.

Zach Scrivena