EWS:邮箱的一个开放的接口服务
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());
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, , 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();
}
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);
}
}
}
EWS:邮箱的一个开放的接口服务的更多相关文章
- Spring Cloud 微服务开放平台接口
github源码地址:https://github.com/spring-cloud/spring-cloud-security 前言: 什么是开放平台接口 场景 : 总公司与子公司 对接接口 还有 ...
- 一个简单的用python 实现系统登录的http接口服务实例
用python 开发一个登录的http接口: 用户登录数据存在缓存redis里,登录时根据session判断用户是否已登录,session有效,则直接返回用户已登录,否则进mysql查询用户名及密码, ...
- SmartSql使用教程(1)——初探,建立一个简单的CURD接口服务
一.引言 最近SmartSql被正式引入到了NCC,借着这个契机写一个使用教程系列 二.SmartSql简介[摘自官方文档] 1. SmartSql是什么? SmartSql = MyBatis + ...
- 收集免费的接口服务,做一个api的搬运工
hello, 大家好,今天给大家推荐的开源项目在某种程度上极大的方便了广大的开发者,这个开源项目统计了网上诸多的免费API,为广大开发者收集免费的接口服务,专心致志做一个API的搬运工,每月定时更新新 ...
- "解密"微信开放高级接口 企业如何应对
今天(2013年10月29日)腾讯终于对外公开了微信公众平台最新的接口,一石激起千层浪,对于很多微信公众平台的运营人员来说,今天是令人兴奋的一天!微信在向申请服务号的企业开发了大量接口.用户不想输入文 ...
- 开放数据接口 API 简介与使用场景、调用方法
此文章对开放数据接口 API 进行了功能介绍.使用场景介绍以及调用方法的说明,供用户在使用数据接口时参考之用. 在给大家分享的一系列软件开发视频课程中,以及在我们的社区微信群聊天中,都积极地鼓励大家开 ...
- 开放API接口
[开放API]——知乎.博客园等开放API接口(更新ing) Cnodejs.org: https://cnodejs.org/api/ 和风天气: http://docs.heweather.c ...
- Api接口服务的设计和安全解决方案
这个涉及到两个方面问题:一个是接口访问认证问题,主要解决谁可以使用接口(用户登录验证.来路验证)一个是数据数据传输安全,主要解决接口数据被监听(HTTPS安全传输.敏感内容加密.数字签名) 普通网站应 ...
- 手把手教你用Abp vnext构建API接口服务
ABP是一个开源应用程序框架,该项目是ASP.NET Boilerplate Web应用程序框架的下一代,专注于基于ASP.NET Core的Web应用程序开发,也支持开发控制台应用程序. 官方网站: ...
随机推荐
- HeadFirst设计模式---装饰者
定义装饰者模式 装饰者模式动态地将责任附加到对象上,若要扩展功能,装饰者提供了比继承更有弹性的替代方案.这句话摘自书中,给人读得很生硬难懂.通俗地来说,装饰者和被装饰者有相同的父类,装饰者的行为组装着 ...
- SQL注入:宽字节注入
了解GBK编码 尽管现在呼吁所有的程序都使用unicode编码,所有的网站都使用utf-8编码,来一个统一的国际规范.但仍然有很多,包括国内及国外(特别是非英语国家)的一些cms,仍然使用着自己国家的 ...
- Pinctrl子系统之一了解基础概念【转】
转自:https://blog.csdn.net/u012830148/article/details/80609337 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请 ...
- jquery 监听不起效果的小问题汇总
在写前端页面时,因为我是用jquery添加新的html字符串来实现动态添加.删除,每次新添加都需要生成新的id,当我对新的id进行监听时,却不起作用. 思考了很多方法,开始我以为,如果将监听的语句$( ...
- Qt 之 qwt 和 qwtpolar 的编译配置
1 Qwt Qwt 全称为 Qt Widgets for Technical Applications,用于专业技术领域的可视化显示,如下所示: 左图为二阶系统的频率响应:中图为德国小城 Fried ...
- 莫烦TensorFlow_09 MNIST例子
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_dat ...
- k8s进入指定pod下的指定容器的命令
访问某pod的某个容器: kubectl --namespace=default exec -it user-deployment-54469dd57-vg87g --container user - ...
- GAN与VAE
经典算法·GAN与VAE Generative Adversarial Networks 及其变体 生成对抗网络是近几年最为经典的生成模型的代表工作,Goodfellow的经典工作.通过两个神经网络结 ...
- C++17尝鲜
https://cloud.tencent.com/developer/article/1351910 [译]C++17,optional, any, 和 variant 的更多细节 用户261520 ...
- VIJOS-P1059 积木城堡
洛谷 P1504 积木城堡 https://www.luogu.org/problem/P1504 JDOJ 1240: VIJOS-P1059 积木城堡 https://neooj.com/oldo ...