Отправка почты с вложениями с Exchange 2003 и WebDav - PullRequest
0 голосов
/ 03 сентября 2010

Я пытаюсь отправить электронную почту через Exchange Server 2003 с WebDav.Отправка простого электронного письма не является проблемой, но я не могу отправить электронное письмо с вложениями.Я получаю вложение на сервер, но не знаю, правильно ли я его помещаю.Итак, мои вопросы: 1. Где на сервере должно быть загружено вложение?2. Как мне связать вложение с электронной почтой ??

Пример кода будет наиболее полезным.

1 Ответ

0 голосов
/ 10 августа 2011

Вот пример кода для отправки почты с вложением с использованием 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 = "alias@serve.com";
  string strTo = "to@server.com";

  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)
  {
  }
...