您的位置:首页 > 编程语言 > Java开发

使用.net和java两种方式收取Gmail邮件。

2006-10-24 23:45 567 查看
公司最近安排做收取用户邮件的模块,主要针对一些知名的大牌信箱。首先考虑Gmail,它有2G的容量、Web2.0的框架,是当然不让的首选。

GMail有一个特点,它不直接提供POP3的接口。不过有一个XML的Feeds地址,需要登陆才能查看。地址如下:
https://mail.google.com/mail/feed/atom

登陆进去后,XML格式是这样的:




<?xml version="1.0" encoding="UTF-8" ?>


<feed version="0.3" xmlns="http://purl.org/atom/ns#">


<title>Gmail - Inbox for li.jadesun@gmail.com</title>


<tagline>New messages in your Gmail Inbox</tagline>


<fullcount>1</fullcount>


<link rel="alternate" href="http://mail.google.com/mail" type="text/html" />


<modified>2006-10-24T15:31:48Z</modified>


<entry>


<title>欢迎光临Yahoo! - 请激活您的帐户</title>


<summary>width=1 帐户信息 帮助 不要回复此封邮件。如果您没有创建一个帐户,请 …</summary>


<link rel="alternate" href="http://mail.google.com/mail?xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" type="text/html" />


<modified>2006-10-24T09:34:37Z</modified>


<issued>2006-10-24T09:34:37Z</issued>


<id>tag:gmail.google.com,2004:1218112365137369397</id>


<author>


<name>my-yahoo-register</name>


<email>my-yahoo-register@yahoo-inc.com</email>


</author>


</entry>


</feed>

研究了一天,用.net实现了登陆收取的方法,采用WebRequest对象来实现的。


wrGETURL =


WebRequest.Create("https://mail.google.com/mail/feed/atom");




if(clsMain.g_UseProxy)




...{


wrGETURL.Proxy = clsMain.GetProxy();


}




bytes = Encoding.ASCII.GetBytes(txtUserName.Text.Trim() + ":"


+ txtUserPassword.Text.Trim());




wrGETURL.Headers.Add("Authorization",


"Basic " + Convert.ToBase64String(bytes));








Stream feedStream =


wrGETURL.GetResponse().GetResponseStream();




Atom.Core.AtomFeed myFeed;




myFeed = Atom.Core.AtomFeed.Load(feedStream);




// Get the date and time of the feed.


strFeedTime = myFeed.Modified.DateTime.Day.ToString() + "-"


+ myFeed.Modified.DateTime.ToString("MMM") + "-"


+ myFeed.Modified.DateTime.Year.ToString() + " "


+ myFeed.Modified.DateTime.TimeOfDay.ToString();




feedTime= DateTime.Parse(strFeedTime);




Atom.Core.Collections.AtomEntryCollection ents =


myFeed.Entries;




foreach(Atom.Core.AtomEntry ent in ents)




...{ ...{


FromName = ent.Author.Name;


Title = ent.Title.Content;


Content = ent.Summary.Content;


//Do something else


}



这里用到了一个开源的组件支持(Atom),把Stream流数据转成了XML数据,并进行了解析,可以在http://atomnet.sourceforge.net/下载得到。

该代码经同事帮忙,改写在JAVA下面了,终于能在项目中应用,我顺便把JAVA代码贴出来,写在Action中的。


import java.net.URL;


import java.net.URLConnection;




import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;




import org.apache.struts.action.Action;


import org.apache.struts.action.ActionForm;


import org.apache.struts.action.ActionForward;


import org.apache.struts.action.ActionMapping;




import sun.misc.BASE64Encoder;






public class GmailAction extends Action...{






/** *//**


* Gmail feed get


*/




@Override


public ActionForward execute(ActionMapping mapping,ActionForm form,




HttpServletRequest request,HttpServletResponse response) throws Exception ...{






String username =request.getParameter("username");


String password =request.getParameter("password");


URL server = new URL("https://mail.google.com/mail/feed/atom");


URLConnection connection = server.openConnection();


PasswordAuthentication pa = Authenticator.requestPasswordAuthentication(


InetAddress.getByName(connection.getURL().getHost()),


connection.getURL().getPort(),


connection.getURL().getProtocol(),


null,


"Basic");


StringBuffer buf = new StringBuffer(username);


buf.append(":");


buf.append(password);


String encoded = new BASE64Encoder().encode(buf.toString().getBytes());;


connection.setRequestProperty ("Authorization", "Basic " + encoded);


connection.connect();


InputStream in = connection.getInputStream();




byte [] data = new byte [1<<10];


ByteArrayOutputStream baos = new ByteArrayOutputStream();


for(int i; (i=in.read(data))!=-1; baos.write(data,0,i));


byte [] badata = baos.toByteArray();


response.getOutputStream().write(badata);


return null;


}








}

response.getOutputStream().write(badata); 输出的就是XML数据,拿着用就是了。解决了收取GMail邮件的问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐