用WebDAV调用Exchange发送邮件
最近ESS系统要增加一个功能,给partner发邮件。内部网没有smtp,只能用Exchange,先在网上查到CDO的方法,要Microsoft CDO for Exchange 2000 Server Library,但是我的机器上只有Microsoft CDO 1.21 Library和Microsoft CDO for Windows 2000 Library,说那个CDO要装Exchange才有,但是web服务器上可没有Exchange。后来在Exchang SDK中发现了WebDAV的方法,说实话,这个WebDAV可真够麻烦的,要不是SDK上有例子,鬼才知道该怎么用。代码如下,我用我的yahoo邮箱测试过了,可以收到所发的邮件。
1using System;
2using System.Net;
3using System.IO;
4using System.Text;
5
6namespace ExchangeSDK.Snippets.CSharp
7{
8 class SendingMessageWebDAV
9 {
10
11 static void Main(string[] args)
12 {
13 System.Net.HttpWebRequest PUTRequest;
14 System.Net.WebResponse PUTResponse;
15 System.Net.HttpWebRequest MOVERequest;
16 System.Net.WebResponse MOVEResponse;
17 System.Net.CredentialCache MyCredentialCache;
18 string strMailboxURI = "";
19 string strSubURI = "";
20 string strTempURI = "";
21 string strServer = "ServerName";
22 string strPassword = "!Password";
23 string strDomain = "Domain";
24 string strAlias = "SenderAlias";
25 string strTo = "recipient@example.com";
26 string strSubject = "WebDAV message test.";
27 string strText = "This message was sent using WebDAV.";
28 string strBody = "";
29 byte[] bytes = null;
30 System.IO.Stream PUTRequestStream = null;
31
32 try
33 {
34 // Build the mailbox URI.
35 strMailboxURI = "http://" + strServer + "/exchange/" + strAlias;
36
37 // Build the submission URI for the message.If Secure
38 // Sockets Layer (SSL) is set up on the server, use
39 // "https://" instead of "http://".
40 strSubURI = "http://" + strServer + "/exchange/" + strAlias
41 + "/##DavMailSubmissionURI##/";
42
43 // Build the temporary URI for the message. If SSL is set
44 // up on the server, use "https://" instead of "http://".
45 strTempURI = "http://" + strServer + "/exchange/" + strAlias
46 + "/drafts/" + strSubject + ".eml";
47
48 // Construct the RFC 822 formatted body of the PUT request.
49 // Note: If the From: header is included here,
50 // the MOVE method request will return a
51 // 403 (Forbidden) status. The From address will
52 // be generated by the Exchange server.
53 strBody = "To: " + strTo + "\n" +
54 "Subject: " + strSubject + "\n" +
55 "Date: " + System.DateTime.Now +
56 "X-Mailer: test mailer" + "\n" +
57 "MIME-Version: 1.0" + "\n" +
58 "Content-Type: text/plain;" + "\n" +
59 "Charset = \"iso-8859-1\"" + "\n" +
60 "Content-Transfer-Encoding: 7bit" + "\n" +
61 "\n" + strText;
62
63 // Create a new CredentialCache object and fill it with the network
64 // credentials required to access the server.
65 MyCredentialCache = new System.Net.CredentialCache();
66 MyCredentialCache.Add( new System.Uri(strMailboxURI),
67 "NTLM",
68 new System.Net.NetworkCredential(strAlias, strPassword, strDomain)
69 );
70
71 // Create the HttpWebRequest object.
72 PUTRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(strTempURI);
73
74 // Add the network credentials to the request.
75 PUTRequest.Credentials = MyCredentialCache;
76
77 // Specify the PUT method.
78 PUTRequest.Method = "PUT";
79
80 // Encode the body using UTF-8.
81 bytes = Encoding.UTF8.GetBytes((string)strBody);
82
83 // Set the content header length.This must be
84 // done before writing data to the request stream.
85 PUTRequest.ContentLength = bytes.Length;
86
87 // Get a reference to the request stream.
88 PUTRequestStream = PUTRequest.GetRequestStream();
89
90 // Write the message body to the request stream.
91 PUTRequestStream.Write(bytes, 0, bytes.Length);
92
93 // Close the Stream object to release the connection
94 // for further use.
95 PUTRequestStream.Close();
96
97 // Set the Content-Type header to the RFC 822 message format.
98 PUTRequest.ContentType = "message/rfc822";
99
100 // PUT the message in the Drafts folder of the
101 // sender's mailbox.
102 PUTResponse = (System.Net.HttpWebResponse)PUTRequest.GetResponse();
103
104 // Create the HttpWebRequest object.
105 MOVERequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(strTempURI);
106
107 // Add the network credentials to the request.
108 MOVERequest.Credentials = MyCredentialCache;
109
110 // Specify the MOVE method.
111 MOVERequest.Method = "MOVE";
112
113 // Set the Destination header to the
114 // mail submission URI.
115 MOVERequest.Headers.Add("Destination", strSubURI);
116
117 // Send the message by moving it from the Drafts folder of the
118 // sender's mailbox to the mail submission URI.
119 MOVEResponse = (System.Net.HttpWebResponse)MOVERequest.GetResponse();
120
121 Console.WriteLine("Message successfully sent.");
122
123 // Clean up.
124 PUTResponse.Close();
125 MOVEResponse.Close();
126
127 }
128 catch(Exception ex)
129 {
130 // Catch any exceptions. Any error codes from the PUT
131 // or MOVE method requests on the server will be caught
132 // here, also.
133 Console.WriteLine(ex.Message);
134 }
135 }
136 }
137}
138
139如果要正确发送中文邮件,要把59和81行的编码改成gb2312
页:
[1]