locked
convert attachment to string in post create email plugin RRS feed

  • Question

  • hi,

    i am writing a post create email activity plugin.  the email may contain an attachment which in turn is another email.  how do successfully convert the original attachment.Attribute['body"] to a string, or even better an Email entity?

    thanks,

    Inna

    Thursday, April 2, 2015 8:58 PM

Answers

  • Hello Inna,

    Recheck following code:

    string result = System.Text.Encoding.Default.GetString(Convert.FromBase64String((string)attachment["body"]));
    
    


    Dynamics CRM MVP
    My blog

    • Marked as answer by inna raykhman Monday, April 6, 2015 3:58 PM
    Friday, April 3, 2015 8:28 AM
    Moderator

All replies

  • Hello Inna,

    First, try attachment["body"].ToString() and if it returns a string object... if not, cast is to byte[]: (byte[])attachment["body"] . Here are some helpful methods for conversion from byte[] to string and viceversa:

    static byte[] GetBytes(string str)
    {
        byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }
    
    static string GetString(byte[] bytes)
    {
        char[] chars = new char[bytes.Length / sizeof(char)];
        System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
        return new string(chars);
    }

    After you get the desired string you can simply create a new Email entity and set email["body"] attribute to the output string from the attachment.


    Cornel Croitoriu - Senior Software Developer

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Biz-Forward.comCroitoriu.NET

    Friday, April 3, 2015 8:06 AM
  • Hello Inna,

    Recheck following code:

    string result = System.Text.Encoding.Default.GetString(Convert.FromBase64String((string)attachment["body"]));
    
    


    Dynamics CRM MVP
    My blog

    • Marked as answer by inna raykhman Monday, April 6, 2015 3:58 PM
    Friday, April 3, 2015 8:28 AM
    Moderator