locked
Attach file to email RRS feed

  • Question

  • in CRM 3.0 I had this code which worked well:

    UploadFromBase64DataActivityMimeAttachmentRequest upload = new UploadFromBase64DataActivityMimeAttachmentRequest();
    upload.ActivityMimeAttachmentId = emailAttachmentID;
    upload.FileName = attachmentDetails.filename;
    upload.MimeType = attachmentDetails.mimetype;
    upload.Base64Data = fileDataString;

    UploadFromBase64DataActivityMimeAttachmentResponse uploaded = (UploadFromBase64DataActivityMimeAttachmentResponse)service.Execute(upload);

        activitymimeattachment attachmentCheck = (activitymimeattachment)service.Retrieve(EntityName.activitymimeattachment.ToString(), emailAttachmentID, new AllColumns());




    however in CRM 4.0, this is not valid. So, how do we attach a file to an email? What is the replacement for the UploadFromBase64DataActivityMimeAttachment ?


    Need 2 be back @ MS - MS All the way! Follower since 1995 MS Super Evangelist| MSDN Forums Moderator
    Thursday, February 12, 2009 1:36 PM

Answers

  •   Actually I just fixed it. It's ok, thanks for that.

    I got hold of the annotation - the annotation contains the document attachment. Once I got hold of that, I created the activitymimeattachment, giving the document body the same as the annotation documentbody etc... etc... then created it giving it the emailID of the email message I created, which is the email I am going to attach with the document (annotation)


    but going back to that old (and silly) code of downloading the attachment, yes it is a problem in the workflow as you get an access denied message from MS CRM service.... but no need to do that, since we can get hold of the attachment anyway from the dynamic entity we are looking at.


    Need 2 be back @ MS - MS All the way! Follower since 1995 MS Super Evangelist| MSDN Forums Moderator
    • Marked as answer by ahmedilyas Tuesday, February 24, 2009 5:45 PM
    • Marked as answer by ahmedilyas Tuesday, February 24, 2009 5:45 PM
    Tuesday, February 24, 2009 5:43 PM

All replies

  • Hi

    You can use the activitymimeattachment to create an attachment to an email:


      
     activitymimeattachment attachment  = new   
    activitymimeattachment();<BR>&nbsp;&nbsp;&nbsp;attachment.activityid&nbsp;&nbsp;&nbsp;=   
    new Lookup(EntityName.email.ToString(),   
    emailguid);<BR>&nbsp;&nbsp;&nbsp;attachment.attachmentnumber &nbsp;&nbsp;= new   
    CrmNumber(0);<BR>&nbsp;&nbsp;&nbsp;attachment.body&nbsp;&nbsp;&nbsp;&nbsp;=   
    orderconfirmation;<BR>&nbsp;&nbsp;&nbsp;attachment.filename&nbsp;&nbsp;&nbsp;=   
    "Order   
    Confirmation.pdf";<BR>&nbsp;&nbsp;&nbsp;attachment.mimetype&nbsp;&nbsp;&nbsp;=   
    "application/pdf";<BR>&nbsp;&nbsp;&nbsp;attachment.subject&nbsp;&nbsp;&nbsp;=   
    "Order Confirmation";<BR>&nbsp;&nbsp;&nbsp;service.Create(attachment); 

    /c
    • Proposed as answer by hadi i teo Friday, February 13, 2009 6:22 AM
    • Unproposed as answer by ahmedilyas Tuesday, February 24, 2009 10:51 AM
    Thursday, February 12, 2009 2:49 PM
  • Thanks, I almost had that code!

    I will come back later when I've completed the migration of the workflow and the common assembly logic I am developing and hopefully, this will work.... and the answer will be marked should it be the case!
    Need 2 be back @ MS - MS All the way! Follower since 1995 MS Super Evangelist| MSDN Forums Moderator
    • Proposed as answer by hadi i teo Friday, February 13, 2009 6:22 AM
    • Unproposed as answer by ahmedilyas Tuesday, February 24, 2009 10:51 AM
    Thursday, February 12, 2009 3:23 PM
  • Here is a sample:

    email email = new email();  
    email.subject = "Requested article(s)...";  
    .......  
    Guid emailId = service.Create(email);  
     
     
    string path = base.Request.MapPath(attachmentPath);  
    File.Exists(path);  
    byte[] buffer = new byte[new FileInfo(path).Length];  
    new FileStream(path, FileMode.Open, FileAccess.Read).Read(buffer, 0, buffer.Length);  
    string str2 = Convert.ToBase64String(buffer);  
    activitymimeattachment activitymimeattachment = new activitymimeattachment();  
    activitymimeattachment.activityid = CrmTypes.CreateLookup("email", emailId);  
    activitymimeattachment.subject = Path.GetFileName(path);  
    activitymimeattachment.filename = Path.GetFileName(path);  
    activitymimeattachment.mimetype ="application/msword";  
    activitymimeattachment.body = str2;  
    service.Create(activitymimeattachment); 

    Ronald
    Thursday, February 12, 2009 5:10 PM
  • ok im working on this today and it doesnt work.
    I created the email, I get a GUID back from the service call, I then do the following code and execute the request and the error I get is that the email ID basically doesn't exist in the database!

    <description>activitypointer With Id = e5dd1061-0602-de11-8950-0003ffa902cc Does Not Exist</description>

    code:

    activitymimeattachment attachment = new activitymimeattachment();
    attachment.activityid = new Lookup();
    attachment.activityid.type = EntityName.email.ToString();
    attachment.activityid.Value = emailAttachmentID;
    attachment.mimetype = attachmentDetails.mimetype;
    attachment.filename = attachmentDetails.filename;
    attachment.body = attachmentDetails.notetext;

    attachment.attachmentnumber = new CrmSdk.CrmNumber();
    attachment.attachmentnumber.Value = 1;

    service.Create(attachment);


    Need 2 be back @ MS - MS All the way! Follower since 1995 MS Super Evangelist| MSDN Forums Moderator
    Tuesday, February 24, 2009 8:39 AM
  •   Curious to know if its related to the way I am getting the attachment file but cannot see why.

    I can create the URL to download the attachment, I paste it in IE and gets the correct attachment
    if I save it to disk whilst debugging, it saves it fine but its not the correct file/states that the current user on the domain or the password maybe incorrect. :-/

    ?!

    wierd thing is, if I use the bit of code to download the file on my other computer, it downloads it fine, so seems like internally on the workflow plugin, it doesnt like it. what gives?


    WebClient client = new WebClient();
    client.Credentials = System.Net.CredentialCache.DefaultCredentials;
    string downloadableFileLink = this.GetAnnotateDownloadableFileLink(attachmentDetails.annotationid.Value.ToString());

    // downloadableFileLink value is:
    // http://myServer:5555/Activities/Attachment/download.aspx?AttachmentType=5&AttachmentId=d3241395-bc5e-4f49-8e60-273625c3d1bd

    byte[] fileData = client.DownloadData(downloadableFileLink);




    Suspicions is the fact that the workflow is running on the network service, which doesnt have permission to authenticate to download the file?


    Need 2 be back @ MS - MS All the way! Follower since 1995 MS Super Evangelist| MSDN Forums Moderator
    Tuesday, February 24, 2009 1:06 PM
  • Could you summarize again what problems you are having? Are you having problem uploading the attachment to an email and downloading it within a workflow activity? Or is it jsut problem with downloading within a workflow activity?


    Ronald
    Tuesday, February 24, 2009 2:30 PM
  •   Actually I just fixed it. It's ok, thanks for that.

    I got hold of the annotation - the annotation contains the document attachment. Once I got hold of that, I created the activitymimeattachment, giving the document body the same as the annotation documentbody etc... etc... then created it giving it the emailID of the email message I created, which is the email I am going to attach with the document (annotation)


    but going back to that old (and silly) code of downloading the attachment, yes it is a problem in the workflow as you get an access denied message from MS CRM service.... but no need to do that, since we can get hold of the attachment anyway from the dynamic entity we are looking at.


    Need 2 be back @ MS - MS All the way! Follower since 1995 MS Super Evangelist| MSDN Forums Moderator
    • Marked as answer by ahmedilyas Tuesday, February 24, 2009 5:45 PM
    • Marked as answer by ahmedilyas Tuesday, February 24, 2009 5:45 PM
    Tuesday, February 24, 2009 5:43 PM