赞
踩
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography.X509Certificates; using System.Net; using System.Net.Security; using Microsoft.Exchange.WebServices.Data; using System.Net.Mail; namespace Exchange { public class Mail { private string sender; /// <summary> /// 发送人 /// </summary> public string Sender { get { return sender; } set { sender = value; } } private string senderPw; /// <summary> /// 发送人密码 /// </summary> public string SenderPw { get { return senderPw; } set { senderPw = value; } } private string ewsUrl; /// <summary> /// exchange WS地址 /// </summary> public string EwsUrl { get { return ewsUrl; } set { ewsUrl = value; } } private ExchangeVersion exVersion; /// <summary> /// exchange version /// </summary> public ExchangeVersion EXVersion { get { return exVersion; } set { exVersion = value; } } /// <summary> /// /// </summary> /// <param name="sender">委托发件人</param> /// <param name="senderPw">密码</param> /// <param name="ewsUrl">exchange webservice地址</param> public Mail(string sender,string senderPw,string ewsUrl) { this.sender = sender; this.senderPw = senderPw; this.ewsUrl = ewsUrl; } public Mail() { } /// <summary> /// 发送邮件 /// </summary> /// <param name="title"></param> /// <param name="body"></param> /// <param name="receiver"></param> public void Send(string title,string body,string[] receiver) { ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }; // 实例化ExchageService ExchangeService service = new ExchangeService(EXVersion); // 指定用户名,密码,和域名 //service.Credentials = new WebCredentials(CredentialCache.DefaultCredentials); service.Credentials = new WebCredentials(this.Sender, this.SenderPw); // 指定Exchage服务的url地址 service.Url = new Uri(this.EwsUrl); EmailMessage message = new EmailMessage(service); // 邮件主题 message.Subject = title; message.Body = new MessageBody(); // 指定发送邮件的格式,可以是Text和Html格式 message.Body.BodyType = BodyType.HTML; // 邮件内容 message.Body.Text = body; // 可以添加多个邮件人.也可以添加一个集合,用 message.ToRecipients.AddRange(receiver); //message.ToRecipients.Add(userEntry.Properties["Mail"].Value.ToString()); message.Send(); } /// <summary> /// 发送邮件 /// </summary> /// <param name="title"></param> /// <param name="body"></param> /// <param name="receiver"></param> public void Send(string title, string body, string receiver) { ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }; // 实例化ExchageService ExchangeService service = new ExchangeService(Microsoft.Exchange.WebServices.Data.ExchangeVersion.Exchange2013_SP1); // 指定用户名,密码,和域名 //service.Credentials = new WebCredentials(CredentialCache.DefaultCredentials); service.Credentials = new WebCredentials(this.Sender, this.SenderPw); // 指定Exchage服务的url地址 service.Url = new Uri(this.EwsUrl); EmailMessage message = new EmailMessage(service); // 邮件主题 message.Subject = title; message.Body = new MessageBody(); // 指定发送邮件的格式,可以是Text和Html格式 message.Body.BodyType = BodyType.HTML; // 邮件内容 message.Body.Text = body; ExtendedPropertyDefinition xExperimentalHeader = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.InternetHeaders, "X-Message-ID", MapiPropertyType.String); message.SetExtendedProperty(xExperimentalHeader, "XXXXXXXX"); // 可以添加多个邮件人.也可以添加一个集合,用 //message.ToRecipients.AddRange(receiver); message.ToRecipients.Add(receiver); message.SendAndSaveCopy(); } } }
using System; namespace Exchange { class Program { static void Main(string[] args) { try { Exchange.Mail mail = new Exchange.Mail("邮箱用户名(不用带@xx.com)", "密码", "https://IP/EWS/exchange.asmx"); mail.Send("Test邮件测试", "Test123", "test@qq.com"); Console.WriteLine("发送成功"); } catch (Exception ex) { Console.WriteLine("发送错误:" + ex); } Console.ReadLine(); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。