2010-09-03 68 views
0

我试图通过带有WebDav的Exchange Server 2003发送电子邮件。发送普通电子邮件没有问题,但我似乎无法发送带附件的电子邮件。我得到附件到服务器,但我不知道我是否把它放在正确的地方。 所以我的问题是: 1.在服务器上应该上传附件的位置? 2.如何将附件与电子邮件相关联?使用Exchange 2003和WebDav发送带附件的邮件

示例代码将不胜感激。

回答

0

下面是使用Exchange Server 2003使用WebDAV

System.Net.HttpWebRequest PUTRequest; 
    System.Net.WebResponse PUTResponse; 
    System.Net.HttpWebRequest MOVERequest; 
    System.Net.WebResponse MOVEResponse; 
    System.Net.HttpWebRequest PROPPATCHRequest; 
    System.Net.WebResponse PROPPATCHResponse; 
    System.Net.HttpWebRequest PUTRequest1; 
    System.Net.WebResponse PUTResponse1; 

    System.Net.CredentialCache MyCredentialCache; 
    string strMailboxURI; 
    string strSubURI; 
    string strTempURI; 
    string strServer = "mail.server.com"; 

    string strPassword = "password"; 
    string strDomain = "domain"; 
    string strAlias = "[email protected]"; 
    string strTo = "[email protected]"; 

    string strSubject = "Webdav message"; 
    string strBody = "This message was sent using WebDAV."; 
    byte[] bytes = null; 
    System.IO.Stream PUTRequestStream = null; 
    System.IO.Stream PUTRequestStream1 = null; 
    System.IO.Stream PROPPATCHRequestStream = null; 
    System.IO.Stream PROPPATCHRequestStream1 = null; 

    try 
    { 

    // Build the mailbox URI. 
    strMailboxURI = "https://" + strServer + "/exchange/" + strAlias; 

    // Build the submission URI for the message. If Secure 
    // Sockets Layer (SSL) is set up on the server, use 
    // "https://" instead of "http://". 
    strSubURI = "https://" + strServer + "/exchange/" + strAlias 
     + "/##DavMailSubmissionURI##/"; 

    // Build the temporary URI for the message. If SSL is set 
    // up on the server, use "https://" instead of "http://". 
    strTempURI = "https://" + strServer + "/exchange/" + strAlias 
      + "/drafts/" + strSubject + ".eml"; 

    // Construct the RFC 822 formatted body of the PUT request. 
    // Note: If the From: header is included here, 
    // the MOVE method request will return a 
    // 403 (Forbidden) status. The From address will 
    // be generated by the Exchange server. 
    strBody = "To: " + strTo + "\n" + 
    "Subject: " + strSubject + "\n" + 
    "Date: " + System.DateTime.Now + 
    "X-Mailer: test mailer" + "\n" + 
    "MIME-Version: 1.0" + "\n" + 
    "Content-Type: text/plain;" + "\n" + 
    "Charset = \"iso-8859-1\"" + "\n" + 
    "Content-Transfer-Encoding: 7bit" + "\n" + 
    "\n" + strBody; 



    // Create a new CredentialCache object and fill it with the network 
    // credentials required to access the server. 
    MyCredentialCache = new System.Net.CredentialCache(); 
    MyCredentialCache.Add(new System.Uri(strMailboxURI), 
    "NTLM", 
     new System.Net.NetworkCredential(strAlias, strPassword, strDomain) 
    ); 

    // Create the HttpWebRequest object. 
    PUTRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(strTempURI); 

    // Add the network credentials to the request. 
    //PUTRequest.Credentials = MyCredentialCache; 

    //add 
    string strCookies = GetCookieValue(strMailboxURI); 
    PUTRequest.Headers.Add("Cookie", strCookies); 

    // Specify the PUT method. 
    PUTRequest.Method = "PUT"; 

    // Encode the body using UTF-8. 
    bytes = Encoding.UTF8.GetBytes((string)strBody); 

    // Set the content header length. This must be 
    // done before writing data to the request stream. 
    PUTRequest.ContentLength = bytes.Length; 

    // Get a reference to the request stream. 
    PUTRequestStream = PUTRequest.GetRequestStream(); 

    // Write the message body to the request stream. 
    PUTRequestStream.Write(bytes, 0, bytes.Length); 

    // Close the Stream object to release the connection 
    // for further use. 
    PUTRequestStream.Close(); 

    // Set the Content-Type header to the RFC 822 message format. 
    PUTRequest.ContentType = "message/rfc822"; 


    // PUT the message in the Drafts folder of the 
    // sender's mailbox. 
    PUTResponse = (System.Net.HttpWebResponse)PUTRequest.GetResponse(); 

PUTResponse.Close()与附件发送邮件的样本代码;

//Do the PROPPATCH 
    string strxml = "<?xml version='1.0'?>" + 
    "<d:propertyupdate xmlns:d='DAV:'>" + 
    "<d:set>" + 
    "<d:prop>" + 
    "<isCollection xmlns='DAV:'>False</isCollection>" + 
    "</d:prop>" + 
    "</d:set>" + 
    "</d:propertyupdate>"; 


    PROPPATCHRequest = 
    (System.Net.HttpWebRequest)HttpWebRequest.Create(strTempURI); 
    //PROPPATCHRequest.Credentials = MyCredentialCache; 
    PROPPATCHRequest.Headers.Add("Cookie", strCookies); 
    PROPPATCHRequest.Headers.Set("Translate", "f"); 
    PROPPATCHRequest.ContentType = "text/xml"; 
    PROPPATCHRequest.ContentLength = strxml.Length; 
    PROPPATCHRequest.Method = "PROPPATCH"; 
    byte[] PROPPATCHbytes = Encoding.UTF8.GetBytes(strxml); 
    PROPPATCHRequest.ContentLength = PROPPATCHbytes.Length; 
    PROPPATCHRequestStream = PROPPATCHRequest.GetRequestStream(); 
    PROPPATCHRequestStream.Write(PROPPATCHbytes, 0, PROPPATCHbytes.Length); 
    PROPPATCHRequestStream.Close(); 
    PROPPATCHResponse = 
    (System.Net.HttpWebResponse)PROPPATCHRequest.GetResponse(); 
    PROPPATCHResponse.Close(); 



    //Attachment 
    string FileName = "D:\Samplefile.txt"; 
    string attachURI = strTempURI + "/" + FileName; 
    PUTRequest1 = (System.Net.HttpWebRequest)HttpWebRequest.Create(attachURI); 
    //PUTRequest1.Credentials = MyCredentialCache; 
    PUTRequest1.Headers.Add("Cookie", strCookies); 
    PUTRequest1.Method = "PUT"; 
    System.IO.FileStream inFile; 
    byte[] binaryData; 
    inFile = new System.IO.FileStream(FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); 
    binaryData = new Byte[inFile.Length]; 
    long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length); 
    inFile.Close(); 
    PUTRequest1.ContentLength = binaryData.Length; 

    PUTRequestStream1 = PUTRequest1.GetRequestStream(); 
    PUTRequestStream1.Write(binaryData, 0, binaryData.Length); 
    PUTRequestStream1.Close(); 
    PUTResponse1 = (System.Net.HttpWebResponse)PUTRequest1.GetResponse(); 

    // Create the HttpWebRequest object. 
    MOVERequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(strTempURI); 

    // Add the network credentials to the request. 
    //MOVERequest.Credentials = MyCredentialCache; 

    //add 
    MOVERequest.Headers.Add("Cookie", strCookies); 

    // Specify the MOVE method. 
    MOVERequest.Method = "MOVE"; 

    // Set the Destination header to the 
    // mail submission URI. 
    MOVERequest.Headers.Add("Destination", strSubURI); 

    // Send the message by moving it from the Drafts folder of the 
    // sender's mailbox to the mail submission URI. 
    MOVEResponse = (System.Net.HttpWebResponse)MOVERequest.GetResponse(); 

    Console.WriteLine("Message successfully sent."); 

    // Clean up. 
    PUTResponse.Close(); 
    MOVEResponse.Close(); 
    } 
    catch (Exception ex) 
    { 
    } 
+0

戈皮克里希纳恩嗨 我无法从你的示例代码获取GetCookieValue()方法,是否有其他类或命名空间?或具有相同CS文件的方法? – projo 2014-03-03 12:53:29