如果我有uri,如何复制.eml文件?

时间:2009-07-28 14:35:09

标签: c# .net asp.net vb.net webdav

我正在使用webDAV和.Net 2.0来收集运行Exchange Server 2003的服务器上的电子邮件帐户的信息。我可以访问uri,如下所示:

http://my.mailserver.com/exchange/user/Inbox/someImportantEmail.EML

我试图像这样复制文件:

Dim uri As New Uri(uriNode.InnerText)
If uri.IsFile() Then
    Dim fn As String = Path.GetFileName(uri.LocalPath)
    System.IO.File.Copy(uri.LocalPath, "c:\" & fn)
End If

但是uri.IsFile()总是返回false。我注意到的另一件事是uri.Local路径是

/exchange/user/Inbox/someImportantEmail.EML

我这部分问题?如何将.eml文件从Exchange服务器复制到本地硬盘?

修改
我已经实施了AnthonyWJones的建议

    Dim cred As New System.Net.CredentialCache
    cred.Add(uri, "BASIC", New System.Net.NetworkCredential(_username, _password, _domain))
    Dim wc As New WebClient()
    wc.Credentials = cred
    wc.DownloadFile(uri, "c:\testEML.EML")

虽然这确实在我的c盘上创建了testEML.EML文件......这里是EML文件的内容:

<!--Copyright (c) 2000-2003 Microsoft Corporation. All rights reserved.-->
<!--CURRENT FILE== "NON-IE5" "NON-WIN32" frameset -->
<!--CURRENT TEMPLATE == frameset.00000000 -->
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=utf-8">
<TITLE>Microsoft Outlook Web Access</TITLE>
<BASE href="http://my.mailserver.com/exchange/user/">
</HEAD>
<SCRIPT language="JavaScript">
var g_iNewWindowWidth = 700;
var g_iNewWindowHeight = 500;
var g_fWarnOnLogOff=false;
function WarnOnLogOff()
{
if (g_fWarnOnLogOff)
alert("To help protect your mailbox from unauthorized access, close all browser windows when you finish using Outlook Web Access.");
}
</SCRIPT>
<FRAMESET OnUnload="WarnOnLogOff()" framespacing="3" cols="190,*"><FRAME bordercolor="#3D5FA3" name="navbar" title="Navigation" src="Inbox/?Cmd=navbar" marginheight="0" marginwidth="0" scrolling="auto" border="1"><FRAME name="viewer" title="Contents" src="Inbox/FW:.EML?Cmd=open" scrolling="auto">
<NOFRAMES>
<BODY><P>This page uses frames, but your browser doesn't support them.</P></BODY>
</NOFRAMES>
</FRAMESET>
</HTML>

未经授权的访问是什么?

2 个答案:

答案 0 :(得分:1)

尝试: -

 Dim webClient As New WebClient()
 webClient.UseDefaultCredentials = True
 webClient.DownloadFile(uri, fn)

您无法使用标准文件IO复制http资源

答案 1 :(得分:1)

这是我最终得到的代码,完全符合我的需要。

    Dim msgPath As String = message.Path
    Dim msgURI As String = message.URI
    Dim msgID As String = message.ID

    MyCredentialCache = New System.Net.CredentialCache()
    MyCredentialCache.Add(New System.Uri(_inboxPath), "BASIC", New System.Net.NetworkCredential(_username, _password, _domain))

    Dim request As WebRequest = WebRequest.Create(msgURI)
    request.Credentials = MyCredentialCache
    request.ContentType = "text/xml"
    request.Headers.Add("Translate", "f")
    Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)

    Dim file As FileStream = New FileStream(msgPath, FileMode.CreateNew)
    Dim stream As Stream = response.GetResponseStream()

    Dim buffer() As Byte = New Byte(4096) {}
    Dim len As Integer = stream.Read(buffer, 0, buffer.Length)

    While (len > 0)
        file.Write(buffer, 0, len)
        len = stream.Read(buffer, 0, buffer.Length)
    End While

    file.Close()
    stream.Close()