c#如何访问exchangeServer获取邮件?

jackey25 2009-07-24 01:03:46
请问如何使用c#访问exchangeServer,并获取邮件?
请大家给我点思路或者做法?

我在网上看到有这样的做法 :

有以下疑问 :1、 oCn.Provider 是做什么的?
2、strSql组装的信息是什么呢?
3、若有例子请指点一下我,谢谢!
using System;

namespace Samples
{
class Class1
{
static void Main(string[] args)
{
try
{
ADODB.Connection oCn = new ADODB.Connection();
ADODB.Recordset oRs = new ADODB.Recordset();

ADODB.Fields oFields;
ADODB.Field oField;

// TODO:
string sFdUrl = "http://ExchServer/Exchange/UserAlias/Inbox";

oCn.Provider = "exoledb.datasource";
oCn.Open(sFdUrl, "", "", -1);

if(oCn.State == 1)
{
Console.WriteLine("Good Connection");
}
else
{
Console.WriteLine("Bad Connection");
}


string strSql;
strSql = "";
strSql = "select ";
strSql = strSql + " \"urn:schemas:mailheader:content-class\"";
strSql = strSql + ", \"DAV:href\" ";
strSql = strSql + ", \"urn:schemas:mailheader:content-class\" ";
strSql = strSql + ", \"DAV:displayname\"";
strSql = strSql + " from scope ('shallow traversal of " + "\"";
strSql = strSql + sFdUrl + "\"') ";
strSql = strSql + " WHERE \"DAV:ishidden\" = false";
strSql = strSql + " AND \"DAV:isfolder\" = false";


oRs.Open(strSql, oCn,
ADODB.CursorTypeEnum.adOpenUnspecified,
ADODB.LockTypeEnum.adLockOptimistic, 1);

// As an example, you only retrieve the first message.
// You can use a while loop through each message.

// Get the first message.
oRs.MoveFirst();

// Get Recordset fields.
oFields = oRs.Fields;

string sUrl;
oField = oFields["DAV:href"];
sUrl = oField.Value.ToString();

CDO.Message iMsg = new CDO.Message();
iMsg.DataSource.Open(sUrl, oRs.ActiveConnection,
ADODB.ConnectModeEnum.adModeReadWrite,
ADODB.RecordCreateOptionsEnum.adFailIfNotExists,
ADODB.RecordOpenOptionsEnum.adOpenSource,
"", "");

Console.WriteLine("{0}", iMsg.Sender);
Console.WriteLine("{0}", iMsg.Subject);
Console.WriteLine("{0}", iMsg.TextBody);

// Get message fields.
oFields = iMsg.Fields;


for(int i = 0; i < oFields.Count; i++)
{
oField = oFields[i];
Console.WriteLine("{0} : {1}", oField.Name, oField.Value);
}


oRs.Close();
oCn.Close();

oCn = null;
oRs = null;
oFields = null;
oField = null;
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
}
}
}
...全文
166 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackey25 2009-08-10
  • 打赏
  • 举报
回复
??
jackey25 2009-07-25
  • 打赏
  • 举报
回复
??
jackey25 2009-07-24
  • 打赏
  • 举报
回复
?

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧