Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access gmail from Java [closed]

Tags:

java

gmail

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

like image 276
Lonzo Avatar asked Jan 27 '09 11:01

Lonzo


5 Answers

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.

like image 163
Galwegian Avatar answered Sep 29 '22 02:09

Galwegian


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)

like image 34
RealHowTo Avatar answered Sep 29 '22 02:09

RealHowTo


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

  • Getting mail from GMail into Java application using IMAP
  • How do you send email from a Java app using Gmail?

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

like image 43
Zach Scrivena Avatar answered Sep 29 '22 04:09

Zach Scrivena


Have a look at GMail API for Java.

like image 29
schnaader Avatar answered Sep 29 '22 03:09

schnaader


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

like image 26
Romain Linsolas Avatar answered Sep 29 '22 03:09

Romain Linsolas