Ejemplo n.º 1
0
        public string SendMessage(string to, string subject = "", string body = "", Dictionary <string, byte[]> attachments = null, string cc = "", string bcc = "", bool saveItem = true)
        {
            var type = attachments != null && attachments.Count > 0 ? "SaveOnly" : saveItem ? "SendAndSaveCopy" : "SendOnly";
            var _to  = ""; foreach (var v in to.Split(";"))
            {
                _to += "<t:Mailbox><t:EmailAddress>" + v.Trim() + "</t:EmailAddress></t:Mailbox>";
            }
            var _cc = ""; foreach (var v in cc.Split(";"))
            {
                _cc += "<t:Mailbox><t:EmailAddress>" + v.Trim() + "</t:EmailAddress></t:Mailbox>";
            }
            var _bcc = ""; foreach (var v in bcc.Split(";"))
            {
                _bcc += "<t:Mailbox><t:EmailAddress>" + v.Trim() + "</t:EmailAddress></t:Mailbox>";
            }


            var xml = GetResponse("<m:CreateItem MessageDisposition=\"" + type + "\"><m:SavedItemFolderId><t:DistinguishedFolderId Id=\"sentitems\" /></m:SavedItemFolderId><m:Items>" +
                                  "<t:Message><t:Subject>" + subject + "</t:Subject><t:Body BodyType=\"HTML\">" + XMLUtils.Encode(body) + "</t:Body>" +
                                  "<t:ToRecipients>" + _to + "</t:ToRecipients><t:CcRecipients>" + _cc + "</t:CcRecipients><t:BccRecipients>" + _bcc + "</t:BccRecipients></t:Message></m:Items></m:CreateItem>");

            if (type == "SaveOnly")
            {
                var pid = xml.SelectSingleNode("//t:ItemId/@Id", _namespaceManager).Value;
                var s   = "<CreateAttachment xmlns=\"" + URI_MSG + "\" xmlns:t=\"" + URI_TYPES + "\"><ParentItemId Id=\"" + pid + "\"/><Attachments>";
                foreach (var att in attachments)
                {
                    s += "<t:FileAttachment><t:Name>" + att.Key + "</t:Name><t:Content>" + Convert.ToBase64String(att.Value) + "</t:Content></t:FileAttachment>";
                }
                xml = GetResponse(s + "</Attachments></CreateAttachment>");
                xml = GetResponse("<SendItem xmlns=\"" + URI_MSG + "\" SaveItemToFolder=\"" + (saveItem + "").ToLower() + "\"><ItemIds><t:ItemId Id=\"" + pid + "\" ChangeKey=\"" +
                                  xml.SelectSingleNode("//t:AttachmentId/@RootItemChangeKey", _namespaceManager).Value + "\"/></ItemIds></SendItem>");
            }
            return(xml.InnerXml);
        }