https://3gstudent.github.io/3gstudent.github.io/exchange-web-service(ews)%e5%bc%80%e5%8f%91%e6%8c%87%e5%8d%97/

ews是邮箱的一个开放的接口服务,可以取到邮箱的各种信息,邮件收发、会议、日期安排

首先需要引入 microsoft.exchange.webservices.dll 引用

 

   class program
    {
        /// <summary>
        /// exchange服务对象    
        /// </summary>
        //private static exchangeservice _exchangeservice = new exchangeservice(exchangeversion.exchange2010_sp1);
        static void main(string[] args)
        {
            #region 需要依赖 microsoft.exchange.webservices 读取未邮件箱
            //exchangeservice版本为2007sp1
            exchangeservice service = new exchangeservice(exchangeversion.exchange2007_sp1);
            //参数是用户名,密码,域
            service.credentials = new webcredentials("email.qq.com", "zh.123456", "qq.com");
            //给出exchange server的url https://xxxxxxx
            service.url = new uri("https://mail.xxxx.com/ews/exchange.asmx");
            //你自己的邮件地址 xxx@xxx.xxx 
            service.autodiscoverurl("email.com", redirectioncallback);
            //创建过滤器, 条件为邮件未读.
            searchfilter sf = new searchfilter.isequalto(emailmessageschema.isread, true);
            //查找inbox,加入过滤器条件,结果10条
            finditemsresults<item> findresults = service.finditems(wellknownfoldername.inbox, sf, new itemview(10));
            foreach (item item in findresults.items)
            {
                emailmessage email = emailmessage.bind(service, item.id);
                if (!email.isread)
                {
                    console.writeline(email.body);
                    //标记为已读
                    email.isread = true;
                    //将对邮件的改动提交到服务器
                    email.update(conflictresolutionmode.alwaysoverwrite);
                }
            }
            #endregion
    
            #region 不需要依赖 microsoft.exchange.webservices 邮件发送
            string user = "email@qq.com";
            string password = "zh.123456"; 
            servicepointmanager.servercertificatevalidationcallback = (sender, certificate, chain, sslpolicyerrors) => { return true; };
            streamreader senddata = new streamreader("ews.xml", encoding.default);
            byte[] senddatabyte = encoding.utf8.getbytes(senddata.readtoend());
            senddata.close();
            try
            {
                httpwebrequest request = (httpwebrequest)webrequest.create("https://mail.xxxxx.com/ews/exchange.asmx");
                request.method = "post";
                request.contenttype = "text/xml";
                request.contentlength = senddatabyte.length;
                request.allowautoredirect = false;
                request.credentials = new networkcredential(user, password);
                stream requeststream = request.getrequeststream();
                requeststream.write(senddatabyte, 0, senddatabyte.length);
                requeststream.close();

                httpwebresponse response = (httpwebresponse)request.getresponse();
                if (response.statuscode != httpstatuscode.ok)
                {
                    throw new webexception(response.statusdescription);
                }
                stream receivestream = response.getresponsestream();

                streamreader readstream = new streamreader(receivestream, encoding.utf8);

                string receivestring = readstream.readtoend();
                response.close();
                readstream.close();

                streamwriter receivedata = new streamwriter("out.xml");
                receivedata.write(receivestring);
                receivedata.close();
            }
            catch (webexception e)
            {
                console.writeline("[!]{0}", e.message);
                environment.exit(0);
            }
            console.writeline("[+]done");

            #endregion
        }
        static bool redirectioncallback(string url)
        {
            // return true if the url is an https url.
            return true;
        }

        public static void sendbyexchange(string[] toemails, string[] ctoemails, string title, string body, string[] filename)
        {
            try
            {
                servicepointmanager.servercertificatevalidationcallback = delegate { return true; };//至关重要的一句 否则会报错:the autodiscover service couldn't be located.
                exchangeservice service = new exchangeservice(exchangeversion.exchange2007_sp1);
                service.credentials = new networkcredential("email@qq.com", "zh.123456");
                service.url = new uri(@"https://mail.xxxxx.com/ews/exchange.asmx");//exchange服务器上接口地址
                service.traceenabled = false;
                service.autodiscoverurl("email@qq.com");
                emailmessage message = new emailmessage(service);
                message.subject = title;
                message.body = body;
                if (filename != null)
                {
                    foreach (var str in filename)
                        message.attachments.addfileattachment(str);
                }
                if (ctoemails != null)
                {
                    foreach (var email in ctoemails)
                        message.ccrecipients.add(email);
                }
                if (toemails != null)
                {
                    foreach (var email in toemails)
                        message.torecipients.add(email);
                }

                message.sendandsavecopy();
            }
            catch (exception ex)
            {
                //utility.loghelper.writelog("发送邮件失败", ex);
            }
        }
    }