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);
}
}
}
猜你喜欢:
暂无回复。