基于WebDAV通过outlook 发送 Email

 以下是一个通用的方法,如果你知道你的Exchange Server (仅限2003和2007版本的Server,往后的Server版本不再支持WebDAV协议,对于更高版本的Exchange Server微软建议调用EWS来发邮件,目前Exchange Server 2007 和 Exchange Server 2010都支持EWS) 那么你就可以调用这个方法来发送Email了,成功发送后在你的outlook的Sent Items中会有记录的.

下例中几个参数的说明:

strSenderAlias,这个参数一般情况下就是你邮箱账号@前面的那段字符串。

strServer,这个参数是你的outlook server 地址,这个可以从outlook客户端菜单栏中有个“Accounts”,进去后选中Microsoft Exchange Server选项,然后点击编辑,就可看到。


 你需要添加如下的引用:

 

using System;

using System.IO;

using System.Net;

using System.Windows.Forms;

 

public bool SendEmailMultiTo(
            string strServer,
            string userName,
            string pwd,
            string domain,
            string emailFrom,
            List<string> emailMultiTo,
            string cc,
            string subject,
            string body)
        {
            if (string.IsNullOrEmpty(strServer)
                || string.IsNullOrEmpty(userName)
                || string.IsNullOrEmpty(pwd)
                || string.IsNullOrEmpty(domain)
                || string.IsNullOrEmpty(emailFrom)
                || !IsEmail(emailFrom)
                || (emailMultiTo.Count == 0 ? true : string.IsNullOrEmpty(emailMultiTo[0]))
                || (emailMultiTo.Count == 0 ? true : !IsEmail(emailMultiTo[0]))
                || (string.IsNullOrEmpty(cc) ? false : !IsEmail(cc)))
            {
                return false;
            }

            try
            {
                string strSenderAlias = string.Empty;
                string mailBoxUri = string.Empty;
                string submissionUri = string.Empty;
                string tmpUri = string.Empty;
                string strQuery = string.Empty;

                HttpWebRequest putRequest = default(HttpWebRequest);
                HttpWebResponse putResponse = default(HttpWebResponse);
                HttpWebRequest moveRequest = default(HttpWebRequest);
                HttpWebResponse moveResponse = default(HttpWebResponse);

                strSenderAlias = emailFrom.Substring(0, emailFrom.IndexOf('@'));
                mailBoxUri = "http://" + strServer + "/exchange/" + strSenderAlias;
                submissionUri = "http://" + strServer + "/exchange/" + strSenderAlias + "/##DavMailSubmissionURI##/";
                tmpUri = "http://" + strServer + "/exchange/" + strSenderAlias + "/drafts/" + subject + ".eml";

                foreach (string email in emailMultiTo)
                {
                    if (string.IsNullOrEmpty(email))
                    {
                        continue;
                    }
                    strQuery += "To: " + email + "\n";
                }

                strQuery += (!string.IsNullOrEmpty(cc) ? ("CC: " + cc + "\n") : "") +
                 "Subject: " + subject + "\n" +
                 "Date: " + DateTime.Now.ToString() + "\n" +
                 "X-Mailer: My DAV mailer" + "\n" +
                 "MIME-Version: 1.0" + "\n" +
                 "Content-Type: text/plain;" + "\n" +
                 "Charset: \"iso-8859-1\"" + "\n" +
                 "Content-Transfer-Encoding: 7bit" + "\n" +
                 "\n" + body;

                putRequest = (HttpWebRequest)HttpWebRequest.Create(tmpUri);
                putRequest.Credentials = new NetworkCredential(userName, pwd, domain);

                // Set the headers.
                putRequest.Headers.Set("Translate", "f");
                putRequest.ContentType = "message/rfc822";
                putRequest.ContentLength = strQuery.Length;

                // Set the request timeout to 60 seconds.
                putRequest.Timeout = 60000;
                // Set the request method.
                putRequest.Method = "PUT";

                // Store the data in a byte array.
                byte[] byteQuery = System.Text.Encoding.ASCII.GetBytes(strQuery);
                putRequest.ContentLength = byteQuery.Length;

                // Get the request stream and write the post data to it.
                Stream queryStream = putRequest.GetRequestStream();
                queryStream.Write(byteQuery, 0, byteQuery.Length);
                queryStream.Close();

                // Send the request and get the response.
                putResponse = (HttpWebResponse)putRequest.GetResponse();

                moveRequest = (HttpWebRequest)HttpWebRequest.Create(tmpUri);
                moveRequest.Credentials = new NetworkCredential(userName, pwd, domain);
                moveRequest.Method = "MOVE";
                moveRequest.Headers.Add("Destination", submissionUri);
                moveResponse = (HttpWebResponse)moveRequest.GetResponse();

                // Clean up.
                putResponse.Close();
                moveResponse.Close();
                moveRequest = null;
                putRequest = null;
                queryStream = null;

                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "System", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
        }

        private bool IsEmail(string email)
        {
            bool isEmail;
            string pattern = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
            isEmail = System.Text.RegularExpressions.Regex.IsMatch(email, pattern);
            return isEmail;
        }


 

没有更多推荐了,返回首页