【问题标题】:How do I copy a .eml file when I have the uri?当我有 uri 时如何复制 .eml 文件?
【发布时间】:2010-11-14 17:42:42
【问题描述】:

我正在使用 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 文件的内容:

Microsoft Outlook Web Access 头>

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


【解决方案1】:

试试:-

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

不能使用标准文件IO来复制http资源

【讨论】:

    【解决方案2】:

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

        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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 2011-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多