2009-01-27 162 views
10

我需要一個庫,允許我使用Java在Gmail中執行電子郵件操作(例如發送/接收郵件)。從Java訪問Gmail

+6

該死的,谷歌再次下跌? – Bombe 2009-01-27 11:36:01

+1

通過IMAP?通過POP3/SMTP?提供更多信息,否則這是一個「詢問Google」的問題。 – guerda 2009-01-27 11:39:19

回答

13

你見過g4j - GMail API for Java?爲Java(G4J)

GMailer API被設置 API,允許Java程序員到 通信到Gmail。使用G4J 程序員可以製作基於Java的 應用程序,該應用程序基於GMail的巨大存儲空間 。

+0

使用pop3怎麼樣?當gmail更新/更改他們的html rendererd客戶端時,g4j會保持最新狀態嗎? – Zombies 2010-05-18 02:16:27

1

首先,將您的Gmail帳戶配置爲接受POP3訪問。 然後,只需使用Javamail訪問您的郵件帳戶!

9

您可以使用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(); 
    } 
} 

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