从 Java 访问 gmail

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/483048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:13:58  来源:igfitidea点击:

Access gmail from Java

javagmail

提问by Lonzo

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

我需要一个允许我使用 Java 在 Gmail 中执行电子邮件操作(例如发送/接收邮件)的库。

采纳答案by Galwegian

Have you seen g4j - GMail API for Java?

你见过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.

GMailer API for Java (g4j) 是一组允许 Java 程序员与 GMail 通信的 API。使用 G4J 程序员可以制作基于 GMail 海量存储的基于 Java 的应用程序。

回答by schnaader

Have a look at GMail API for Java.

看看GMail API for Java

回答by Romain Linsolas

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

首先,配置您的 Gmail 帐户以接受 POP3 访问。然后,只需使用 Javamail 访问您的邮件帐户!

回答by RealHowTo

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

您可以为此使用 Javamail。需要记住的是 GMail 使用 SMTPS 而不是 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)

ref :使用 SMTPS 发送电子邮件(例如 Google GMail)(Javamail)

回答by Zach Scrivena

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

这个问题的变化已经在之前的几篇文章中得到了解决:

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

一般方法是通过JavaMail使用 IMAP/SMTP 。在常见问题甚至还有一个特殊的入境与Gmail的工作