23 Temmuz 2013 Salı

CRM 2011-Emaile Dosya Ekleme (Add Attachment To Email )

public static void AddAttachmentToEmail(EntityReference email, IOrganizationService service)
        {
            // Open a file and read the contents into a byte array
            FileStream stream = File.OpenRead(@"C:\Lakshman\sample.txt");
            byte[] byteData = new byte[stream.Length];
            stream.Read(byteData, 0, byteData.Length);
            stream.Close();

            // Encode the data using base64.
            string encodedData = System.Convert.ToBase64String(byteData);
         
            // Add attachment
            ActivityMimeAttachment attachment = new ActivityMimeAttachment();

            // Set the body
            attachment.Body = encodedData;

            // Set the attachment Type
            attachment.MimeType = @"text/plain";
            attachment.FileName = "Sample.txt";

            // Set the regarding object. In my case it is Email entity
            attachment.ObjectId = email;
            attachment.ObjectTypeCode = email.LogicalName;

            // Create the attachment
            service.Create(attachment);
        }

Hiç yorum yok:

Yorum Gönder