/* GiM 2010 * gim.jogger.pl */ using System; using System.IO; using System.Text; using System.Text.RegularExpressions; using System.Collections.Generic; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using ews.ExchangeWebServices; namespace ews { class Ews { public static bool ValidateCert(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { if (errors == SslPolicyErrors.None) return true; Console.WriteLine("Certificate error: {0}", errors); return false; } public static ExchangeServiceBinding CreateBinding() { ExchangeServiceBinding esb = new ExchangeServiceBinding(); esb.Credentials = new NetworkCredential("username", "password", "domainName"); esb.Url = @"https://hostname/ews/Exchange.asmx"; esb.RequestServerVersionValue = new RequestServerVersion(); esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007; return esb; } public static CreateItemType CreateEmailRequest() { CreateItemType createEmailRequest = new CreateItemType(); createEmailRequest.MessageDisposition = MessageDispositionType.SendAndSaveCopy; createEmailRequest.MessageDispositionSpecified = true; createEmailRequest.SavedItemFolderId = new TargetFolderIdType(); DistinguishedFolderIdType sent = new DistinguishedFolderIdType(); sent.Id = DistinguishedFolderIdNameType.sentitems; createEmailRequest.SavedItemFolderId.Item = sent; createEmailRequest.Items = new NonEmptyArrayOfAllItemsType(); return createEmailRequest; } public static int SendEmail(List rec, string fileName) { //ServicePointManager.ServerCertificateValidationCallback = ValidateCert; ExchangeServiceBinding esb = CreateBinding(); CreateItemType createEmailRequest = CreateEmailRequest(); StreamReader sr = File.OpenText(fileName); MessageType message = new MessageType(); message.Subject = sr.ReadLine(); message.Body = new BodyType(); message.Body.BodyType1 = BodyTypeType.Text; message.Body.Value = sr.ReadToEnd(); message.Sender = new SingleRecipientType(); message.Sender.Item = new EmailAddressType(); /* MS Exchange will change it anyway to proper address basing * on credentials used */ message.Sender.Item.EmailAddress = "dummy@address.com"; message.ToRecipients = new EmailAddressType[rec.Count]; int idx = 0; foreach (string recipient in rec) { message.ToRecipients[idx] = new EmailAddressType(); message.ToRecipients[idx].EmailAddress = recipient; idx++; } message.Sensitivity = SensitivityChoicesType.Normal; createEmailRequest.Items.Items = new ItemType[1]; createEmailRequest.Items.Items[0] = message; try { CreateItemResponseType createItemResponse = esb.CreateItem(createEmailRequest); ArrayOfResponseMessagesType responses = createItemResponse.ResponseMessages; foreach(ResponseMessageType response in responses.Items) { Console.WriteLine("{0} : {1} ", response.ResponseCode, response.MessageText); } return 0; } catch (Exception ex) { Console.WriteLine("oh noez error"); Console.WriteLine(ex.Message); return 1; } } public static List parseRecipients(string fileName) { StreamReader strm = File.OpenText(fileName); string s = ""; List ret = new List(); while ((s = strm.ReadLine()) != null) { Match m = Regex.Match(s, @"recipients = (.*)"); if (m.Success) { string[] names = m.Groups[1].Value.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries); foreach (string foo in names) { if (foo.Trim().Length > 0) ret.Add(foo.Trim()); } } } return ret; } static int Main(string[] args) { List rec = parseRecipients("config.txt"); return SendEmail(rec, args[0]); } } }