当前位置:   article > 正文

C#使用Exchange Web服务(/EWS/exchange.asmx)发送邮件

C#使用Exchange Web服务(/EWS/exchange.asmx)发送邮件

使用Exchange Web服务发送邮件

发送代码,Mail类

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();
        }

        
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167

测试调用,Program类

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();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/427864
推荐阅读
相关标签
  

闽ICP备14008679号