我想从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. 如何从Web应用程序访问用户的Gmail帐户?
- 2. 如何在从我的Google应用程序应用程序访问Gmail上的电子邮件时始终保持对电子邮件的引用?
- 3. 从Java访问Gmail
- 4. 如何从Gmail /电子邮件打开Android应用程序?
- 5. 如何从Java EE Web应用程序访问属性文件?
- 6. 我可以从我的Android应用程序识别新邮件的Gmail通知
- 7. 如何使用我的java swing应用程序登录gmail?
- 8. 从PHP访问java应用程序
- 9. gmail从我的应用程序短信
- 10. php邮件脚本将gmail同步到我的应用程序
- 11. 我如何从我的应用程序访问iphone联系人?
- 12. 从自己的应用程序访问电子邮件配置
- 13. 这个程序如何访问GMail?
- 14. 从Xamarin.Forms应用程序中的Gmail发送电子邮件
- 15. 如何在我的ASP.NET应用程序中接收来自Gmail的邮件?
- 16. 如何从应用程序访问?
- 17. 发送邮件从Gmail使用SMTPMailer flex AIR应用程序
- 18. 使用Gmail从CakePHP应用程序发送电子邮件
- 19. Java邮件API:访问GMAIL邮箱的“所有邮件”文件夹
- 20. 从Java应用程序发送电子邮件的问题
- 21. 如何从我的应用程序中启动邮件应用程序?
- 22. 如何从外部应用程序访问电子邮件中的pouchdb?
- 23. 从Gmail应用程序获取未读邮件
- 24. javamail api访问Gmail收件箱邮件
- 25. 如何从另一个应用程序访问java类?
- 26. 如何从互联网访问我的网络应用程序?
- 27. 如何从我的iPhone应用程序访问SOAP Web服务
- 28. 如何从应用程序中的联系人应用程序访问电子邮件?
- 29. 如何从Java Web应用程序发送电子邮件
- 30. 如何从我的Facebook应用程序发送电子邮件
你好,这功课吗? – 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