Чудеса для меня не понятные: код приведенный ниже отрабатывает без ошибок, письмо создается и отправляется успешно, но в прикрепленном письме пропадает вложение!
X++:
using (CrmService service = new CrmService())
{
setServiceProperty(service);
Guid salesliteratureId = new Guid(@"{5B852BEB-B626-DF11-A36C-000423D90837}");
Guid accountId = new Guid("{231EF12D-6A25-DF11-A36C-000423D90837}");
Guid userId = new Guid("{2D2AD46B-5321-DF11-9B70-000423D90837}");
Guid templateId = new Guid("{339A1896-B526-DF11-A36C-000423D90837}");
ColumnSet colsAccount = new ColumnSet();
colsAccount.Attributes = new string [] {"emailaddress1"};
account account = (account) service.Retrieve(EntityName.account.ToString(), accountId, colsAccount);
if (account.emailaddress1.Length != 0)
{
InstantiateTemplateRequest templateReq = new InstantiateTemplateRequest();
templateReq.TemplateId = templateId;
templateReq.ObjectId = accountId;
templateReq.ObjectType = EntityName.account.ToString();
templateReq.ReturnDynamicEntities = false;
InstantiateTemplateResponse templateRes = (InstantiateTemplateResponse) service.Execute(templateReq);
email email = (email) templateRes.BusinessEntityCollection.BusinessEntities[0];
email.trackingtoken = " ";
email.ownerid = new Owner();
email.ownerid.type = EntityName.systemuser.ToString();
email.ownerid.Value = userId;
activityparty fromparty = new activityparty();
fromparty.partyid = new Lookup();
fromparty.partyid.type = EntityName.systemuser.ToString();
fromparty.partyid.Value = userId;
email.from = new activityparty[] {fromparty};
activityparty toparty = new activityparty();
toparty.partyid = new Lookup();
toparty.partyid.type = EntityName.account.ToString();
toparty.partyid.Value = accountId;
email.to = new activityparty[] {toparty};
Guid emailId = service.Create(email);
Console.WriteLine("emailId=" + emailId.ToString());
BusinessEntityCollection entities;
QueryExpression query = new QueryExpression();
query.EntityName = EntityName.salesliteratureitem.ToString();
ColumnSet cols = new ColumnSet();
cols.Attributes = new string[]{"filename", "title", "salesliteratureitemid", "mimetype"};
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "salesliteratureid";
condition.Values = new string[1]{salesliteratureId.ToString()};
condition.Operator = ConditionOperator.Equal;
FilterExpression filter = new FilterExpression();
filter.Conditions = new ConditionExpression[] {condition};
query.ColumnSet = cols;
query.Criteria = filter;
entities = service.RetrieveMultiple(query);
activitymimeattachment attach;
if (entities.BusinessEntities.Length !=0)
{
System.Net.WebClient webClient = new System.Net.WebClient();
webClient.Credentials = getNetworkCredential();
for(int i=0; i < entities.BusinessEntities.Length; i++)
{
salesliteratureitem entity = (salesliteratureitem)entities.BusinessEntities[i];
string attachmentUrl = getAttachmentUrl(entity.salesliteratureitemid.Value);
byte[] attachBody = webClient.DownloadData(attachmentUrl);
attach = new activitymimeattachment();
attach.attachmentnumber = new CrmNumber();
attach.attachmentnumber.Value = i;
attach.activityid = new Lookup();
attach.activityid.type = EntityName.email.ToString();
attach.activityid.Value = emailId;
attach.body = System.Convert.ToBase64String(attachBody);
attach.subject = entity.title;
attach.filename = entity.filename;
attach.mimetype = entity.mimetype;
Guid attachId = service.Create(attach);
Console.WriteLine("attachId=" + attachId.ToString());
}
}
SendEmailRequest sendEmailReq = new SendEmailRequest();
sendEmailReq.EmailId = emailId;
sendEmailReq.TrackingToken = " ";
sendEmailReq.IssueSend = true;
SendEmailResponse sendEmailRes = (SendEmailResponse)service.Execute(sendEmailReq);
}