我想從Java代碼中獲取我的gmail帳戶的電子郵件。我怎麼能這樣做呢?如何從Java應用程序訪問我的Gmail郵件?
2
A
回答
1
1
另一種選擇:如果您不介意這是一個Gmail特定的解決方案,請注意,Gmail還會向您的郵箱提供RSS源,然後您可以使用正常的XML處理API訪問該郵箱。
0
下面是使用POST OFFICE PROTOCOL(pop3)從gmail帳戶獲取郵件及其附件(如果有)的代碼。
import com.sun.mail.pop3.POP3Folder;
import com.sun.mail.pop3.POP3SSLStore;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.MimeBodyPart;
public class MailfetchingPop3
{
private Session session;
private POP3SSLStore store;
private String username;
private String password;
private POP3Folder folder;
public static String numberOfFiles = null;
public static int toCheck = 0;
public static Writer output = null;
URLName url;
public static String receiving_attachments="C:\\download";
public MailfetchingPop3()
{
session = null;
store = null;
}
public void setUserPass(String username, String password)
{
this.username = username;
this.password = password;
}
public void connect()
throws Exception
{
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties pop3Props = new Properties();
pop3Props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
pop3Props.setProperty("mail.pop3.socketFactory.fallback", "false");
pop3Props.setProperty("mail.pop3.port", "995");
pop3Props.setProperty("mail.pop3.socketFactory.port", "995");
url = new URLName("pop3", "pop.gmail.com", 995, "", username, password);
session = Session.getInstance(pop3Props, null);
store = new POP3SSLStore(session, url);
store.connect();
}
public void openFolder(String folderName)
throws Exception
{
folder = (POP3Folder)store.getFolder(folderName);
System.out.println((new StringBuilder("For test----")).append(folder.getParent().getFullName()).toString());
if(folder == null)
throw new Exception("Invalid folder");
try
{
folder.open(2);
System.out.println((new StringBuilder("Folder name----")).append(folder.getFullName()).toString());
}
catch(Exception ex)
{
System.out.println((new StringBuilder("Folder Opening Exception..")).append(ex).toString());
}
}
public void closeFolder()
throws Exception
{
folder.close(false);
}
public int getMessageCount()
throws Exception
{
return folder.getMessageCount();
}
public int getNewMessageCount()
throws Exception
{
return folder.getNewMessageCount();
}
public void disconnect()
throws Exception
{
store.close();
}
public void printAllMessages()
throws Exception
{
Message msgs[] = folder.getMessages();
FetchProfile fp = new FetchProfile();
folder.fetch(msgs, fp);
for(int i = 0; i < msgs.length; i++)
dumpEnvelope(msgs[i]);
}
public static int saveFile(File saveFile, Part part) throws Exception {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(saveFile));
byte[] buff = new byte[2048];
InputStream is = part.getInputStream();
int ret = 0, count = 0;
while((ret = is.read(buff)) > 0){
bos.write(buff, 0, ret);
count += ret;
}
bos.close();
is.close();
return count;
}
private static void dumpEnvelope(Message m) throws Exception
{
String body="";
String path="";
int size=0;
Object content = m.getContent();
if(content instanceof String){
body = (String)content;
}
else if(content instanceof Multipart)
{
Multipart mp = (Multipart)content;
for (int j=0; j < mp.getCount(); j++)
{
Part part = mp.getBodyPart(j);
String disposition = part.getDisposition();
//System.out.println("test disposition---->>"+disposition);
if (disposition == null) {
// Check if plain
MimeBodyPart mbp = (MimeBodyPart)part;
if (mbp.isMimeType("text/plain")) {
body += mbp.getContent().toString();
}
else if (mbp.isMimeType("TEXT/HTML")) {
body += mbp.getContent().toString();
}
else {
//unknown
}
} else if ((disposition != null) &&
(disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE) || disposition.equals("ATTACHMENT") || disposition.equals("INLINE")))
{
// Check if plain
MimeBodyPart mbp = (MimeBodyPart)part;
if (mbp.isMimeType("text/plain")) {
body += (String)mbp.getContent();
}
else if (mbp.isMimeType("TEXT/HTML")) {
body += mbp.getContent().toString();
}
else {
File savedir = new File(receiving_attachments);
savedir.mkdirs();
File savefile = new File(savedir+"\\"+part.getFileName());
path = savefile.getAbsolutePath();
size = saveFile(savefile, part);
}
}
}
}
}
public static void main(String args[])
{
try
{
MailfetchingPop3 gmail = new MailfetchingPop3();
gmail.setUserPass("your_gmail_Id", "your_gmail_mail_id_password");
gmail.connect();
gmail.openFolder("INBOX");
gmail.printAllMessages();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(-1);
}
}
}
運行,你需要下載javamail.jar和activation.jar
3
這裏是刷新工作代碼,顯示在一個適當的格式在控制檯中的電子郵件封郵件有附件也一起被下載了這個java類...
import com.sun.mail.pop3.POP3Folder;
import com.sun.mail.pop3.POP3SSLStore;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.MimeBodyPart;
public class MailfetchingPop3
{
private Session session;
private POP3SSLStore store;
private String username;
private String password;
private POP3Folder folder;
public static String numberOfFiles = null;
public static int toCheck = 0;
public static Writer output = null;
URLName url;
public static String receiving_attachments="C:\\download";
public MailfetchingPop3()
{
session = null;
store = null;
}
public void setUserPass(String username, String password)
{
this.username = username;
this.password = password;
}
public void connect()
throws Exception
{
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties pop3Props = new Properties();
pop3Props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
pop3Props.setProperty("mail.pop3.socketFactory.fallback", "false");
pop3Props.setProperty("mail.pop3.port", "995");
pop3Props.setProperty("mail.pop3.socketFactory.port", "995");
url = new URLName("pop3", "pop.gmail.com", 995, "", username, password);
session = Session.getInstance(pop3Props, null);
store = new POP3SSLStore(session, url);
store.connect();
}
public void openFolder(String folderName)
throws Exception
{
folder = (POP3Folder)store.getFolder(folderName);
System.out.println((new StringBuilder("For test----")).append
(folder.getParent().getFullName()).toString());
if(folder == null)
throw new Exception("Invalid folder");
try
{
folder.open(2);
System.out.println((new StringBuilder("Folder name----")).append
(folder.getFullName()).toString());
}
catch(Exception ex)
{
System.out.println((new StringBuilder("Folder Opening Exception..")).append(ex).toString());
}
}
public void closeFolder()
throws Exception
{
folder.close(false);
}
public int getMessageCount()
throws Exception
{
return folder.getMessageCount();
}
public int getNewMessageCount()
throws Exception
{
return folder.getNewMessageCount();
}
public void disconnect()
throws Exception
{
store.close();
}
public void printAllMessages()
throws Exception
{
Message msgs[] = folder.getMessages();
FetchProfile fp = new FetchProfile();
folder.fetch(msgs, fp);
for(int i = 0; i < msgs.length; i++){
Message message = msgs[i];
dumpEnvelope(msgs[i]);
System.out.println("==============================");
System.out.println("Email #" + (i + 1));
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + message.getFrom()[0]);
System.out.println("Text: " + message.getContent().toString());
}
}
public static int saveFile(File saveFile, Part part) throws Exception {
BufferedOutputStream bos = new BufferedOutputStream(new
FileOutputStream(saveFile));
byte[] buff = new byte[2048];
InputStream is = part.getInputStream();
int ret = 0, count = 0;
while((ret = is.read(buff)) > 0){
bos.write(buff, 0, ret);
count += ret;
}
bos.close();
is.close();
return count;
}
private static void dumpEnvelope(Message m) throws Exception
{
String body="";
String path="";
int size=0;
Object content = m.getContent();
if(content instanceof String){
body = (String)content;
}
else if(content instanceof Multipart)
{
Multipart mp = (Multipart)content;
for (int j=0; j < mp.getCount(); j++)
{
Part part = mp.getBodyPart(j);
String disposition = part.getDisposition();
//System.out.println("test disposition---->>"+disposition);
if (disposition == null) {
// Check if plain
MimeBodyPart mbp = (MimeBodyPart)part;
if (mbp.isMimeType("text/plain")) {
body += mbp.getContent().toString();
}
else if (mbp.isMimeType("TEXT/HTML")) {
body += mbp.getContent().toString();
}
else {
//unknown
}
} else if ((disposition != null) &&
(disposition.equals(Part.ATTACHMENT) || disposition.equals
(Part.INLINE) || disposition.equals("ATTACHMENT") || disposition.equals
("INLINE")))
{
// Check if plain
MimeBodyPart mbp = (MimeBodyPart)part;
if (mbp.isMimeType("text/plain")) {
body += (String)mbp.getContent();
}
else if (mbp.isMimeType("TEXT/HTML")) {
body += mbp.getContent().toString();
}
else {
File savedir = new File(receiving_attachments);
savedir.mkdirs();
File savefile = new File(savedir+"\\"+part.getFileName());
path = savefile.getAbsolutePath();
size = saveFile(savefile, part);
}
}
}
}
}
public static void main(String args[])
{
try
{
MailfetchingPop3 gmail = new MailfetchingPop3();
gmail.setUserPass("your-gmail-username", "your-gmail-password");
gmail.connect();
gmail.openFolder("INBOX");
gmail.printAllMessages();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(-1);
}
}
}
相關問題
- 1. 如何從Gmail中訪問郵件
- 2. 如何從Web應用程序訪問用戶的Gmail帳戶?
- 3. 使用谷歌oAuth2從java應用程序收集Gmail郵件
- 4. 如何在從我的Google應用程序應用程序訪問Gmail上的電子郵件時始終保持對電子郵件的引用?
- 5. 從Java訪問Gmail
- 6. 如何從Gmail /電子郵件打開Android應用程序?
- 7. 如何訪問我的應用程序中的Gmail附件數據
- 8. 如何發送電子郵件與Gmail從我的應用程序撰寫?
- 9. 從Gmail API訪問郵件內容
- 10. 如何從Java EE Web應用程序訪問屬性文件?
- 11. Android Gmail應用程序 - 郵件附件URI問題
- 12. 我可以從我的Android應用程序識別新郵件的Gmail通知
- 13. 如何從我的應用程序訪問我的CircleProgressBar?
- 14. 如何使用我的java swing應用程序登錄gmail?
- 15. 訪問到Java應用程序從PHP
- 16. 從PHP訪問java應用程序
- 17. gmail從我的應用程序短信
- 18. php郵件腳本將gmail同步到我的應用程序
- 19. 我如何從Spotify應用程序訪問我的數據庫
- 20. 我如何從我的應用程序訪問iphone聯繫人?
- 21. 我使用Gmail來管理電子郵件,我的Rails應用程序域從Rails應用程序
- 22. 這個程序如何訪問GMail?
- 23. 從自己的應用程序訪問電子郵件配置
- 24. 如何在我的項目中訪問android應用程序中的gmail帳戶?
- 25. 從Xamarin.Forms應用程序中的Gmail發送電子郵件
- 26. 如何從應用程序訪問?
- 27. 如何在我的ASP.NET應用程序中接收來自Gmail的郵件?
- 28. 如何通過IMAP/POP3從Gmail禁止訪問電子郵件
- 29. 發送郵件從Gmail使用SMTPMailer flex AIR應用程序
- 30. 使用Gmail從CakePHP應用程序發送電子郵件
你好,這功課嗎? – 2010-07-31 09:06:32
[使用IMAP從GMail將郵件發送到Java應用程序](http://stackoverflow.com/questions/61176/getting-mail-from-gmail-into-java-application-using-imap) – 2010-07-31 09:16:10
搜索框在右上方。 – 2010-07-31 10:53:33