设为首页 收藏本站
查看: 1845|回复: 0

[经验分享] 用WebDAV调用Exchange发送邮件

[复制链接]

尚未签到

发表于 2015-9-10 12:49:15 | 显示全部楼层 |阅读模式
最近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邮箱测试过了,可以收到所发的邮件。

  1 DSC0000.gif using System;
  2using System.Net;
  3using System.IO;
  4using System.Text;
  5
  6namespace ExchangeSDK.Snippets.CSharp
  7 DSC0001.gif DSC0002.gif DSC0003.gif {
  8 DSC0004.gif    class SendingMessageWebDAV
  9 DSC0005.gif DSC0006.gif    {
10      [STAThread]
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 DSC0007.gif          }
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 DSC0008.gif }
138
139如果要正确发送中文邮件,要把59和81行的编码改成gb2312

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-111963-1-1.html 上篇帖子: 透過 ADO 2.5 存取 Exchange 2000 下篇帖子: Exchange server 2007 出现“0x8004010F”错误的解决办法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表