2010-09-03 4 views
0

J'essaie d'envoyer des e-mails via Exchange Server 2003 avec WebDav. Envoyer un e-mail simple n'est pas un problème, mais je n'arrive pas à envoyer un e-mail avec des pièces jointes. Je reçois la pièce jointe au serveur mais je ne sais pas si je la mets au bon endroit. Donc mes questions sont: 1. Où sur le serveur la pièce jointe doit-elle être téléchargée? 2. Comment associer la pièce jointe à l'e-mail?Envoyer un e-mail avec des pièces jointes avec Exchange 2003 et WebDav

Un exemple de code serait très apprécié.

Répondre

0

Voici l'exemple de code pour envoyer des mail avec pièce jointe à l'aide d'Exchange Server 2003 avec 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

Salut gopikrishnan Je ne peux pas obtenir la méthode GetCookieValue() à partir de votre exemple de code, est-il une autre classe ou espace de noms? ou méthode avec le même fichier CS? – projo

Questions connexes