vaj*_*rov 89 c# email mapi exchange-server
我需要能够监视和读取来自MS Exchange Server(我公司内部)上的特定邮箱的电子邮件.我还需要能够阅读发件人的电子邮件地址,主题,邮件正文并下载附件(如果有的话).
使用C#(或Vb.net)执行此操作的最佳方法是什么?
Nic*_*cki 90
一团糟.通过.NET互操作DLL的MAPI或CDO 正式不受Microsoft支持 - 它似乎工作正常,但由于它们的内存模型不同而存在内存泄漏问题.您可以使用CDOEX,但这只适用于Exchange服务器本身,而不是远程; 无用.你可以与Outlook互操作,但现在你只是依赖于Outlook; 矫枉过正.最后,您可以使用Exchange 2003的WebDAV支持,但WebDAV很复杂,.NET内置支持很差,并且(为了加重损害)Exchange 2007 几乎完全放弃了 WebDAV支持.
什么人要做?我最终使用AfterLogic的IMAP组件通过IMAP与我的Exchange 2003服务器进行通信,最终效果非常好.(我通常会寻找免费或开源的库,但我发现所有的.NET都需要 - 特别是当涉及到2003年IMAP实施的一些怪癖时 - 这个很便宜并且在第一个工作试试.我知道那里还有其他人.)
但是,如果您的组织在Exchange 2007上,那么您很幸运.Exchange 2007附带了基于SOAP的Web服务接口,最终提供了与Exchange服务器交互的统一,与语言无关的方式.如果您可以将2007+作为一项要求,那么这绝对是您的选择.(对我来说可悲的是,我的公司有一个"但2003年没有破坏"的政策.)
如果您需要桥接Exchange 2003和2007,那么IMAP或POP3绝对是您的选择.
War*_*War 65
嗯,
我可能在这里有点太晚了,但这不是EWS的重点吗?
https://msdn.microsoft.com/en-us/library/dd633710(EXCHG.80).aspx
需要大约6行代码才能从邮箱中获取邮件:
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
//service.Credentials = new NetworkCredential( "{Active Directory ID}", "{Password}", "{Domain Name}" );
service.AutodiscoverUrl( "First.Last@MyCompany.com" );
FindItemsResults<Item> findResults = service.FindItems(
WellKnownFolderName.Inbox,
new ItemView( 10 )
);
foreach ( Item item in findResults.Items )
{
Console.WriteLine( item.Subject );
}
Run Code Online (Sandbox Code Playgroud)
Dmi*_*nko 17
当前首选(Exchange 2013和2016)API是EWS.它纯粹基于HTTP,可以从任何语言访问,但有.Net和Java特定的库.
您可以使用EWSEditor来使用API.
扩展MAPI.这是Outlook使用的本机API.它最终使用MSEMSExchange MAPI提供程序,该提供程序可以使用RPC(Exchange 2013不再支持它)或RPC-over-HTTP(Exchange 2007或更高版本)或MAPI-over-HTTP(Exchange 2013和更高版本)与Exchange通信.
API本身只能从非托管C++或Delphi访问.您还可以使用Redemption(任何语言) - 其RDO系列对象是扩展MAPI包装器.若要使用扩展MAPI,您需要安装Outlook或MAPI的独立(Exchange)版本(在扩展支持上,它不支持Unicode PST和MSG文件,并且无法访问Exchange 2016).扩展MAPI可用于服务.
您可以使用OutlookSpy或MFCMAPI来使用API .
Outlook对象模型 - 不是特定于Exchange的,但它允许访问运行代码的计算机上的Outlook中可用的所有数据.不能用于服务.
Exchange Active Sync.Microsoft不再向此协议投入任何重要资源.
Outlook用于安装CDO 1.21库(它包装了扩展MAPI),但它已被Microsoft弃用,不再接收任何更新.
曾经有一个名为MAPI33的第三方.Net MAPI包装器,但不再开发或支持它.
WebDAV - 已弃用.
Exchange协作数据对象(CDOEX) - 已弃用.
Exchange OLE DB提供程序(EXOLEDB) - 已弃用.
Cod*_*ike 10
这是我用来做WebDAV的一些旧代码.我认为它是针对Exchange 2003编写的,但我不记得了.如果它有用,请随意借用它...
class MailUtil
{
private CredentialCache creds = new CredentialCache();
public MailUtil()
{
// set up webdav connection to exchange
this.creds = new CredentialCache();
this.creds.Add(new Uri("http://mail.domain.com/Exchange/me@domain.com/Inbox/"), "Basic", new NetworkCredential("myUserName", "myPassword", "WINDOWSDOMAIN"));
}
/// <summary>
/// Gets all unread emails in a user's Inbox
/// </summary>
/// <returns>A list of unread mail messages</returns>
public List<model.Mail> GetUnreadMail()
{
List<model.Mail> unreadMail = new List<model.Mail>();
string reqStr =
@"<?xml version=""1.0""?>
<g:searchrequest xmlns:g=""DAV:"">
<g:sql>
SELECT
""urn:schemas:mailheader:from"", ""urn:schemas:httpmail:textdescription""
FROM
""http://mail.domain.com/Exchange/me@domain.com/Inbox/""
WHERE
""urn:schemas:httpmail:read"" = FALSE
AND ""urn:schemas:httpmail:subject"" = 'tbintg'
AND ""DAV:contentclass"" = 'urn:content-classes:message'
</g:sql>
</g:searchrequest>";
byte[] reqBytes = Encoding.UTF8.GetBytes(reqStr);
// set up web request
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://mail.domain.com/Exchange/me@domain.com/Inbox/");
request.Credentials = this.creds;
request.Method = "SEARCH";
request.ContentLength = reqBytes.Length;
request.ContentType = "text/xml";
request.Timeout = 300000;
using (Stream requestStream = request.GetRequestStream())
{
try
{
requestStream.Write(reqBytes, 0, reqBytes.Length);
}
catch
{
}
finally
{
requestStream.Close();
}
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
try
{
XmlDocument document = new XmlDocument();
document.Load(responseStream);
// set up namespaces
XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);
nsmgr.AddNamespace("a", "DAV:");
nsmgr.AddNamespace("b", "urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/");
nsmgr.AddNamespace("c", "xml:");
nsmgr.AddNamespace("d", "urn:schemas:mailheader:");
nsmgr.AddNamespace("e", "urn:schemas:httpmail:");
// Load each response (each mail item) into an object
XmlNodeList responseNodes = document.GetElementsByTagName("a:response");
foreach (XmlNode responseNode in responseNodes)
{
// get the <propstat> node that contains valid HTTP responses
XmlNode uriNode = responseNode.SelectSingleNode("child::a:href", nsmgr);
XmlNode propstatNode = responseNode.SelectSingleNode("descendant::a:propstat[a:status='HTTP/1.1 200 OK']", nsmgr);
if (propstatNode != null)
{
// read properties of this response, and load into a data object
XmlNode fromNode = propstatNode.SelectSingleNode("descendant::d:from", nsmgr);
XmlNode descNode = propstatNode.SelectSingleNode("descendant::e:textdescription", nsmgr);
// make new data object
model.Mail mail = new model.Mail();
if (uriNode != null)
mail.Uri = uriNode.InnerText;
if (fromNode != null)
mail.From = fromNode.InnerText;
if (descNode != null)
mail.Body = descNode.InnerText;
unreadMail.Add(mail);
}
}
}
catch (Exception e)
{
string msg = e.Message;
}
finally
{
responseStream.Close();
}
}
return unreadMail;
}
}
Run Code Online (Sandbox Code Playgroud)
和model.Mail:
class Mail
{
private string uri;
private string from;
private string body;
public string Uri
{
get { return this.uri; }
set { this.uri = value; }
}
public string From
{
get { return this.from; }
set { this.from = value; }
}
public string Body
{
get { return this.body; }
set { this.body = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
164370 次 |
| 最近记录: |