jueves, 12 de septiembre de 2013

CRM 4.0 - Adjuntar un fichero a un email por código

        CRMWeb.email mail = new email();
        activityparty from = new activityparty();
        from.partyid = new Lookup();
        //David GUID
        from.partyid.Value = new Guid("616BF2A2-187B-DC11-8EC5-000C29925837");
        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("C0AFAD72-9C7C-DC11-8EC5-000C29925837");
        to.partyid.type = EntityName.contact.ToString();
        mail.to = new activityparty[] { to };
        mail.subject = "Mail con imagen incluida" + System.DateTime.Now;
        mail.description = "";
        CrmBoolean direccion = new CrmBoolean();
        direccion.Value = true;
        mail.directioncode = direccion;
        Guid mailguid = service.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 = "Caras.jpg";
        attachment.mimetype = "image/jpeg";
        attachment.subject = "Caras.jpg";
       
        Guid guidatachment = service.Create(attachment);
       
        FileStream stream = File.OpenRead(@"C:\Caras.jpg");
        byte[] byteData = new byte[stream.Length];
        stream.Read(byteData, 0, byteData.Length);
        //byteData = System.Text.Encoding.UTF8.GetBytes(data);
        string encodedData = System.Convert.ToBase64String(byteData);
        // Create the request object to upload the file to CRM. 
        UploadFromBase64DataActivityMimeAttachmentRequest upload = new UploadFromBase64DataActivityMimeAttachmentRequest();
        // Set the properties of the request object.
        upload.ActivityMimeAttachmentId = guidatachment;
        upload.FileName = "Caras.jpg";
        upload.MimeType = "image/jpeg";
        upload.Base64Data = encodedData;
        UploadFromBase64DataActivityMimeAttachmentResponse uploaded = (UploadFromBase64DataActivityMimeAttachmentResponse)service.Execute(upload);
        bool success2 = false;
        activitymimeattachment attachmentCheck = (activitymimeattachment)service.Retrieve(EntityName.activitymimeattachment.ToString(), guidatachment, new AllColumns());
        if (attachmentCheck.filename != null) {
            success2 = true;
        }

No hay comentarios:

Publicar un comentario