domingo, 15 de septiembre de 2013

CRM 4.0 - Adjuntar un fichero a un email

 //Cojo el fichero y lo atacheo en una tarea del tipo mail de CRM y lo envió
            email mail = new email();
            activityparty from = new activityparty();
            from.partyid = new Lookup();
            //Administrator GUID
            from.partyid.Value = new Guid("5B61411D-0147-DD11-BCB8-000C299FD622");
            from.partyid.type = EntityName.systemuser.ToString();
            mail.from = new activityparty[] { from };
            activityparty to = new activityparty();
            to.partyid = new Lookup();
            //Mario Robles GUID
            to.partyid.Value = new Guid("5279C129-9B47-DD11-9B3A-000C299FD622");
            to.partyid.type = EntityName.contact.ToString();
            mail.to = new activityparty[] { to };
            mail.subject = "Mail con documento mergeado" + System.DateTime.Now;
            mail.description = "Mail con documento mergeado" + System.DateTime.Now;
            CrmBoolean direccion = new CrmBoolean();
            direccion.Value = true;
            mail.directioncode = direccion;
            FileStream stream = File.OpenRead(@"C:\DocumentoMergeado.xml");
            byte[] byteData = new byte[stream.Length];
            stream.Read(byteData, 0, byteData.Length);
            string encodedData = System.Convert.ToBase64String(byteData);
            Guid mailguid = crmService.Create(mail);
            activitymimeattachment attachment = new activitymimeattachment();
            attachment.activityid = new Lookup();
            attachment.activityid.Value = mailguid;
            attachment.activityid.type = EntityName.email.ToString();
            attachment.attachmentnumber = new CrmNumber();
            attachment.attachmentnumber.Value = 1;
            attachment.filename = "DocumentoMergeado.xml";
            attachment.mimetype = "application/xml";
            attachment.subject = "DocumentoMergeado.xml";
            attachment.body = encodedData;
            Guid guidatachment = crmService.Create(attachment);
            activitymimeattachment attachmentCheck = (activitymimeattachment)crmService.Retrieve(EntityName.activitymimeattachment.ToString(), guidatachment, new AllColumns());

No hay comentarios:

Publicar un comentario