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);
}
}
}
}
EWS Mail Operations (一)
最新推荐文章于 2022-10-26 21:02:18 发布