I need a library that allows me to do email operations(e.g Sent/Receive mail) in Gmail using Java.
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.
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)
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.
Have a look at GMail API for Java.
First, configure your Gmail account to accept POP3 access. Then, simply access your mail account using Javamail !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With