EWS Mail Operations (一)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Exchange.WebServices;
using Microsoft.Exchange.WebServices.Data;
using System.Net;
using System.IO;

namespace OfficeUtility
{
    public class EWSUtility
    {
        /// <summary>
        /// Create an Exchange Web Service instance. Need provide a web service URL.
        /// </summary>
        /// <param name="credential">User credential. Including user name, password and domain.</param>
        /// <param name="url">Web service URL.</param>
        /// <returns>Exchange Web Service instance.</returns>
        public static ExchangeService CreateService(NetworkCredential credential, Uri url)
        {
            ExchangeService service = new ExchangeService();
            service.Credentials = credential;
            service.Url = url;

            return service;
        }

        /// <summary>
        /// Create an Exchange Web Service instance. Doesn't need web service URL, but will be slower.
        /// </summary>
        /// <param name="credential">User credential. Including user name, password and domain.</param>
        /// <param name="emailAddress">Email address for URL Autodiscover.</param>
        /// <returns>Exchange Web Service instance.</returns>
        public static ExchangeService CreateService(NetworkCredential credential, string emailAddress)
        {
            ExchangeService service = new ExchangeService();
            service.Credentials = credential;
            service.AutodiscoverUrl(emailAddress);

            return service;
        }

        /// <summary>
        /// Create and send mail with attched files through EWS.
        /// </summary>
        /// <param name="service">The current Exchange Web Service instance.</param>
        /// <param name="toTypeRecipients">The email addresses which will be in "To" line.</param>
        /// <param name="ccTypeRecipients">The email addresses which will be in "CC" line.</param>
        /// <param name="bccTypeRecipients">The email addresses which will be in "BCC" line.</param>
        /// <param name="subject">The mail subject.</param>
        /// <param name="mailBody">The content of the mail.</param>
        /// <param name="attachedFileList">The files will be attached to the mail.</param>
        public static void CreateAndSendMail(
            ExchangeService service, 
            List<string> toTypeRecipients, 
            List<string> ccTypeRecipients, 
            List<string> bccTypeRecipients,
            string subject,
            string mailBody,
            List<string> attachedFilesList)
        {
            try
            {
                EmailMessage message = new EmailMessage(service);
                message.Subject = subject;
                message.Body = mailBody;
                message.ToRecipients.AddRange(toTypeRecipients);
                message.CcRecipients.AddRange(ccTypeRecipients);
                message.BccRecipients.AddRange(bccTypeRecipients);

                if (attachedFilesList != null)
                {
                    foreach (string file in attachedFilesList)
                    {
                        if (File.Exists(file))
                        {
                            string name = file.Substring(file.LastIndexOf('\\') + 1);

                            if (!string.IsNullOrEmpty(name))
                            {
                                message.Attachments.AddFileAttachment(name, file);
                                continue;
                            }

                            throw new FileNotFoundException("Cannot find file when attach to mail message.", file);
                        }
                    }
                }

                message.SendAndSaveCopy();
            }

            catch (Exception ex)
            {
                throw new System.Exception(
                    string.Format("Error happens when create/send mail \"{0}\", exception message: {1}\r\nStack trace: {2}", subject, ex.Message, ex.StackTrace),
                    ex);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值