This time, we have been studying the exchange2003 server and related client applications. Instead of using Outlook on the client, we send emails programmatically. Currently, there are two main ways for exchange to send mails programmatically on the client: WebDAV and CDO. Due to its own weakness, it is usually used in combination with CDO instead of WebDAV alone. Although these two methods are not as mature and powerful as outlook, they provide great flexibility and scalability for application system installation and deployment on the client, and are undoubtedly concerned by many programmers, I want to associate the two programming methods with outlook and become an exchange server on the client side. (Exchange2003 server is praised by Microsoft as a complete, long-term enterprise messaging and collaboration platform, its related details can browse http://www.microsoft.com/china/exchange/default.mspx ). This article will first give an overview of the concept and principles of WebDAV, and then explain how to use WebDAV and CDO to send emails programmatically on the client, in the next article, we will introduce how to use CDO programming to send emails. What is WebDAV? You may search for it on the Internet. However, if you have installed the exchange SDK, you can directly obtain related instructions. Next I will paste the introduction to WebDAV In the SDK directly. Because it is easy to understand, I will not translate it here, and some experts will not abuse my English too. WebDAV items in the exchange store can be accessed remotely by using the WebDAV protocol, defined in RFC 2518. this Protocol extends the HTTP 1.1 protocol, defined by RFC 2616, to provide additional methods and capabilities. it provides a means to access both the contents of an item and an extensible set of associated properties. some of the methods defined by the Protocol are the move method, COPY method, the delete method, and the mkcol method. the encoding format used to transfer item properties within ss the network is XML, defined in the World Wide Web Consortium (W3C) Recommendation REC-xml-20001006. when a URL in the exchange store is entered in a Web browser, an XML formatted WebDAV protocol request is created and sent to the Microsoft Exchange server computer. when the server rec Eives the request, it verifies the credentials of the client and automatically parses the XML for the requested data. the server then builds an XML WebDAV protocol response containing the appropriate properties and their values and sends the response back to the client. if the web browser is able to parse XML, an XSL style sheet can be applied to the XML response and the data will be displayed in The browser. if the web browser cannot Parse XML, the information is displayed in HTML. see authentication and security using WebDAV for more information. the following validation shows how a client browser interacts with the exchange store using WebDAV. microsoft Internet Explorer 5 and later provide the Microsoft XML (MSXML) Component Object Model (COM) component, a powerful XML Parser and set Of related tools. the XMLHttpRequest com class integrates with msxml com objects to simplify the management of client-side HTTP protocol requests and responses that contain XML bodies. the transformational capabilities of the MSXML component can easily cast the response XML data into HTML, which can then be displayed by the browser .. to use WebDAV for programming in. net, you need to add references to Microsoft XML and 3.0. As for the reason, if you carefully read the above content, you will not I need to explain more (more directly, see the two arrows in the figure ). To send emails in this way, you do not need to install outlook on the client or reference other components related to exchange or outlook. The deployment is simple and convenient, but there are two obvious disadvantages: one is that the WebDAV Method itself does not support sending attachments. Someone on the Internet suggested to embed the attachments in the mail body after base64 encoding. It is troublesome to try it; the second is that BCC is not supported, that is, bcc recipients cannot receive emails. After reading some information, I did not find the crux of the second defect. I can only say that it is a "demon". The first defect is described as follows: there is no mechanism to add an attachment in WebDAV. you will need to create/recreate the item using a WebDAV put. the text you wocould be putting wocould be the mime of the item, which also contains the attachment. it's useful here to use cdosys to create a message and add an attachment-then extract the mime from the message and use it for the webda V put. you will need a header of "translate" set to "F ". if the message already exists, you will need to, you must do a get on the message stream, which will give you the message + attachments in a string. next, modify the stream to include a new attachment. after this, the string can be used in a put statement to write the attachment. we know that the CDO method is very convenient to send attachments. An addattachment () method solves the problem and supports sending multiple attachments. Sending the information contained in CDO through WebDAV can effectively solve the attachment problem. The working mechanism is as follows: to send an email with WebDAV, you will need to create/recreate the item with a WebDAV put using the mime of the message. it gets tricky when working with attachments. to get around the complexity of sending an email with an attachment, you may want to look at using cdosys to build the message to send, then extract the mime stream (MIME of the message in a string) of the resulting Message. for sending the message, you wocould use a put statement to write the stream to a file in the drafts folder of the sending person's mail box. if you need to set specific properties not set by the mime, you shoshould do a proppatch against the message in the drafts folder. next, the Code shocould use a WebDAV move in order to place the message into the mailbox's submission URL. the submission URL Is a special URL used for sending messages. "// # davmailsubmissionuri #" off of the root of the mailbox is the submission URL. finally, we will provide a C # example for sending emails in combination with WebDAV and CDO. We hope that we can help some friends who encounter the same problem and only provide the key part here, because I signed the confidentiality agreement. // Add reference Microsoft CDO for Windows 2000 library, Microsoft XML, 3.0 // This will create a cdosys message, attach a file and return the mime stream for use with WebDAV private string buildmessageandgeneratemimestream () {CDO. ibodypart obodypart; CDO. message omessage = new CDO. message (); CDO. configuration oconfig = new CDO. configuration; ADODB. fields ofields = oconfig. fields; string sfile, strmimestream; Dodb. stream omimestream; omessage. configuration = oconfig; omessage. to = "receiver@yourdomain.microsoft.com"; omessage. from = "Administrator@yourdomain.microsoft.com"; omessage. subject = "my test"; omessage. textbody = "this is my test! "; Omessage. update (); sfile = "C:/yourfilename, TXT"; obodypart = omessage. addattachment (sfile, username, password); omimestream = omessage. getstream (); strmimestream = omimestream. readtext (); omimestream = NULL; obodypart = NULL; omessage = NULL; return strmimestream;} // used to put (wirte) an item to a file in a folder. private bool dowebdavput (string mailfolder, string mailtext, string username, string Password) {msxml2.xmlhttp30 oxmlhttp = new msxml2.xmlhttp30 (); bool flag = false; int istatus; string sStatus = string. empty; string sresponse = string. empty; oxmlhttp. open ("put", mailfolder, false, username, password); oxmlhttp. setRequestHeader ("translate", "F"); oxmlhttp. send (mailtext); // send the stream into SS istatus = oxmlhttp. status; sStatus = oxmlhttp. statustext; If (istatus> = 200 & istatus <3 00) {console. writeline ("put: Success! Results = "+ istatus. tostring (); flag = true;} else if (istatus = 401) {console. writeline ("put: You don't have permission to do the job! ");} Else {console. writeline ("put: request failed. results = "+ istatus. tostring () + ":" + sStatus);} oxmlhttp = NULL; return flag;} // used to move an item from one folder to another in the same store. private bool dowebdavcopymove (string ssourceurl, string sdestinationurl, bool iscopy, string username, string password); {bool flag = false; msxml2.xmlhttp30 oxmlhttp = new msxml2.xmlhttp30 (); strin G sverb = string. empty; If (iscopy) {sverb = "copy";} else {sverb = "move";} oxmlhttp. open (sverb, ssourceurl, false, username, password);} oxmlhttp. setRequestHeader ("destination", sdestinationurl); // send the stream into SS oxmlhttp. send (); If (oxmlhttp. status> = 200 & oxmlhttp. status & lt; 300) {console. writeline ("success! Results = "+ oxmlhttp. status + ":" + oxmlhttp. statustext); flag = true;} else if (oxmlhttp. status = 401) {console. writeline ("You don't have permission to do the job! ");} Else {console. writeline ("request failed. results = "+ oxmlhttp. status + ":" + oxmlhttp. statustext);} oxmlhttp = NULL;} private void Merge () {string draftsfolder, submissionurl, attenditem, strmimestream, user, PWD; bool flag = false; user = "your username "; pwd = "your password"; draftsfolder = "http: // your exchange server name (IP)/exchange/useralias/drafts/" + mailsubject + ". Eml "; // If Exchange Server is Chinese version, change drafts to draft box submissionurl =" http: // your exchange server name (IP) /exchange/useralias/# davmailsubmissionurl # "; // use cdosys to generate the message body parts and such strmimestream = Response (); flag = dowebdavput (draftsfolder, strmimestream, user, PWD); If (FLAG) {// at this point, the email is in the drafts folder. if You don't want it sent automatically, You can // comment out the line below. if the line below does not execute, you can load the message // from Outlook of OWA (Outlook Web Access) and send it from there. flag = dowebdavcopymove (draftsfolder, submissionurl, false, user, PWD); // move it to submission!} If (FLAG) {console. writeline ("mail sending successfully! ");}}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.