I'm trying to attach an email (Which has a FileAttachment in it) to another email as an ItemAttachment. Just like below.
var originalEmail = new Message{...}; // Consider I have all the required properties filled in, Including FileAttachments
var anotherEmail = new Message{...}; // Consider I have filled in required details
anotherEmail.Attachments.Add(new ItemAttachment {Name = "Att1", Item= originalEmail, IsInline = false } );
var messagePayload = new SendMailPostRequestBody {Message = anotherEmail, SaveToSentItems = true };
await graphClient.Users[userEmail].SendMail.PostAsync(messagePayload) // Sending an email that has an outlook item but the outlook item upon opening has no FileAttachments in it.
I tried to add the originalEmail to draft first (I could see the attachment in the draft) and attached it to anotherEmail, still the FileAttachment inside the originalEmail disappears when I open it in outlook.
This is how I'm preparing the originalEmail and attaching it as an ItemAttachment in a new simple email later (referred as anotherEmail above).
private Message PrepareMessage(Email email, bool isHtml, string inputFolderPath)
{
var from = _configuration["FromAccount"];
var subject = email.Subject + "_" + _sessionId;
var to = _configuration["TestInbox"];
var message = new Message();
message.ToRecipients = new List<Recipient> { new Recipient { EmailAddress = new EmailAddress { Address = to } } };
message.From = new Recipient { EmailAddress = new EmailAddress { Address = from } };
message.Subject = subject;
message.Body = new ItemBody { Content = email.Body, ContentType = isHtml ? BodyType.Html : BodyType.Text };
if (message.Attachments == null)
message.Attachments = new List<Attachment>();
foreach (var attachmentFile in email.Attachments)
{
var attachmentPath = Path.Combine(inputFolderPath, "Attachments", attachmentFile);
var attachment = new FileAttachment
{
OdataType = "#microsoft.graph.fileAttachment",
Name = attachmentFile,
ContentBytes = File.ReadAllBytes(attachmentPath),
ContentType = MimeTypeHelper.GetMimeType(attachmentFile),
};
message.Attachments.Add(attachment);
}
return message;
}
What am I missing here?
I'm trying to attach an email (Which has a FileAttachment in it) to another email as an ItemAttachment. Just like below.
var originalEmail = new Message{...}; // Consider I have all the required properties filled in, Including FileAttachments
var anotherEmail = new Message{...}; // Consider I have filled in required details
anotherEmail.Attachments.Add(new ItemAttachment {Name = "Att1", Item= originalEmail, IsInline = false } );
var messagePayload = new SendMailPostRequestBody {Message = anotherEmail, SaveToSentItems = true };
await graphClient.Users[userEmail].SendMail.PostAsync(messagePayload) // Sending an email that has an outlook item but the outlook item upon opening has no FileAttachments in it.
I tried to add the originalEmail to draft first (I could see the attachment in the draft) and attached it to anotherEmail, still the FileAttachment inside the originalEmail disappears when I open it in outlook.
This is how I'm preparing the originalEmail and attaching it as an ItemAttachment in a new simple email later (referred as anotherEmail above).
private Message PrepareMessage(Email email, bool isHtml, string inputFolderPath)
{
var from = _configuration["FromAccount"];
var subject = email.Subject + "_" + _sessionId;
var to = _configuration["TestInbox"];
var message = new Message();
message.ToRecipients = new List<Recipient> { new Recipient { EmailAddress = new EmailAddress { Address = to } } };
message.From = new Recipient { EmailAddress = new EmailAddress { Address = from } };
message.Subject = subject;
message.Body = new ItemBody { Content = email.Body, ContentType = isHtml ? BodyType.Html : BodyType.Text };
if (message.Attachments == null)
message.Attachments = new List<Attachment>();
foreach (var attachmentFile in email.Attachments)
{
var attachmentPath = Path.Combine(inputFolderPath, "Attachments", attachmentFile);
var attachment = new FileAttachment
{
OdataType = "#microsoft.graph.fileAttachment",
Name = attachmentFile,
ContentBytes = File.ReadAllBytes(attachmentPath),
ContentType = MimeTypeHelper.GetMimeType(attachmentFile),
};
message.Attachments.Add(attachment);
}
return message;
}
What am I missing here?
Share Improve this question edited Mar 26 at 8:49 Tiny Wang 16.4k2 gold badges18 silver badges38 bronze badges asked Mar 25 at 19:49 Venkatesh DharavathVenkatesh Dharavath 5201 gold badge6 silver badges21 bronze badges 1 |2 Answers
Reset to default 0Instead of attaching originalEmail as an ItemAttachment, extract its FileAttachments and add them directly to anotherEmail as individual FileAttachments.
var originalEmail = PrepareMessage(email, isHtml, inputFolderPath);
var anotherEmail = new Message
{
Subject = "Forwarded Email with Attachments",
Body = new ItemBody { Content = "See attachments below", ContentType = BodyType.Text },
ToRecipients = new List<Recipient> { new Recipient { EmailAddress = new EmailAddress { Address = to } } }
};
if (anotherEmail.Attachments == null)
anotherEmail.Attachments = new List<Attachment>();
foreach (var attachment in originalEmail.Attachments)
{
anotherEmail.Attachments.Add(attachment);
}
var messagePayload = new SendMailPostRequestBody { Message = anotherEmail, SaveToSentItems = true };
await graphClient.Users[userEmail].SendMail.PostAsync(messagePayload);
I had a test in my side with my outlook, I can create a draft email with an attachment inside it. Then I create a new message and add the draft as the attachment, then I can see the origin message still contains the attachment file which is different from your description, see screenshot below. Therefore, I think what you hope to do is feasible.
But since I don't have test resource, I just write sample codes and I can't test it. Creating a draft message first. According to API document, we shall get the message ID in the response. Then we can get the message with message ID and then use your codes.
public async Task test() {
var requestBody = new Message
{
ReceivedDateTime = DateTimeOffset.Parse("datetime-value"),
SentDateTime = DateTimeOffset.Parse("datetime-value"),
HasAttachments = true,
Subject = "subject-value",
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = "content-value",
},
BodyPreview = "bodyPreview-value",
};
var attachment = new FileAttachment
{
OdataType = "microsoft.graph.fileAttachment",
Name = "name-value",
ContentType = "contentType-value",
IsInline = false,
ContentLocation = "contentLocation-value",
ContentBytes = Convert.FromBase64String("base64-contentBytes-value"),
};
requestBody.Attachments.Add(attachment);
var result = await _graphServiceClient.Me.MailFolders["{mailFolder-id}"].Messages.PostAsync(requestBody);
//var result = await _graphServiceClient.Me.Messages.PostAsync(requestBody);
var mesgId = result.Id;
var originalEmail = await _graphServiceClient.Me.Messages[mesgId].GetAsync();
//var originalEmail = await _graphServiceClient.Me.Messages[mesgId].GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Expand = new string[] { "attachments" };
});
var attachments = originalEmail.Attachments;
var anotherEmail = new Message { }; // Consider I have filled in required details
anotherEmail.Attachments.Add(new ItemAttachment { Name = "Att1", Item = originalEmail, IsInline = false });
}
PrepareMessage
you returned the message content. I'm afraid we could try to create a draft message instead. Could you pls try it? – Tiny Wang Commented Mar 26 at 8:52