使用.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邮件的问题。
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邮件的问题。
相关文章推荐
- 使用.net和java两种方式收取Gmail邮件。
- 无法使用POP方式收取Gmail邮件的解决方法【应用】
- java 读取文件——按照行取出(使用BufferedReader和一次将数据保存到内存两种实现方式)
- EBS中使用JAVA方式发送HTML格式邮件
- 大数据IMF传奇行动绝密课程第83课:透彻讲解使用Scala和Java两种方式实战Spark Streaming开发
- Java中使用根据模板拼接字符串的两种方式
- JAVA中Arrays.sort()使用两种方式(Comparable和Comparator接口)对对象或者引用进行排序
- .net中使用Gmail发送邮件
- 两种方式实现java定时器,使用quartz定时器框架和java自带Timer定时器,编写定时任务
- 不使用spring的情况下用java原生代码操作mongodb数据库的两种方式
- EBS中使用JAVA方式发送HTML格式邮件
- 在.net中使用Gmail发送邮件
- java基础复习之对于String对象,能够使用“=”赋值,也能够使用newkeyword赋值,两种方式有什么差别?
- JAVA中Arrays.sort()使用两种方式(Comparable和Comparator接口)对对象或者引用进行排序
- JAVA中Arrays.sort()使用两种方式(Comparable和Comparator接口)对对象或者引用进行排序
- 在.net中使用Gmail发送邮件
- JAVA中Arrays.sort()使用两种方式(Comparable和Comparator接口)对对象或者引用进行排序
- java application应用程序 使用JDBC和proxool两种方式连接数据库 的测试程序代码
- 在.net中使用Gmail发送邮件
- Java中使用Redis的两种方式