Retain MSG internet headers after modifying message

Hi,

Please can you help me to retain a .MSG internet headers after modifying and saving the message?

Thanksv

Hi,


Thank you for your inquiry and sorry for the delayed response on this.

I am assuming that your using the .NET version of the product. Please check the below source code to retrieve the Headers from a modified message.

var message = Aspose.Email.Outlook.MapiMessage.FromFile(“HTMLMail2.msg”);
var headers = message.Headers;

If I'm wrong in my understanding then please elaborate your question further.

Thank you

Hi,

Thanks for you reply. I have managed to get that working but I do have another problem.

I'm busy creating an Email Branding application for my company. We currently just using the trial version of the .NET Component. We have managed to insert an embedded MHT and JPEG file. My requirement is to remove my embedded MHT and JPEG files from an email coversation when replying. In this way only the latest reply gets branded with my companies details, all my other previous branding will be stripped.

Please can you assist?

Thanks

Hi,

You can identify the embedded attachments with below source code,

foreach (MapiAttachment att in message.Attachments)
{
if (att.Properties.Contains(MapiPropertyTag.PR_ATTACH_CONTENT_ID_A) || att.Properties.Contains(MapiPropertyTag.PR_ATTACH_CONTENT_ID_W))
{
Console.WriteLine("Embedded, inline or linked Attachment File Name: " + att.LongFileName);
}
else
{
Console.WriteLine("Regular Attachment File Name: " + att.LongFileName);
}
[//att.Save](https://att.save/)(att.FileName);
}

Once identified, you can remove any embedded message with MapiAttachmentCollection .Remove() method.

For adding any image as an embedded resource in your email message, please check out the video tutorial for your reference.

HiThanks for this, but I still have an issue. I'm not using the MAPI reference, I'm using the Aspose.Email.Mail; reference. When I perform the below code I get an error "Collection was modified; enumeration operation may not execute.Collection was modified; enumeration operation may not execute."

foreach (Attachment att in msg.Attachments)
{
Console.WriteLine(att.ContentId);
if (att.ContentId.Substring(att.ContentId.Length - 2, 2) == "XT")
{
att.Save(@"c:\test\" + att.Name);
msg.Attachments.Remove(att);
};

} foreach (Attachment att in msg.Attachments)
{
Console.WriteLine(att.ContentId);
if (att.ContentId.Substring(att.ContentId.Length - 2, 2) == "XT")
{
att.Save(@"c:\test\" + att.Name);
msg.Attachments.Remove(att);
};

}

Can you tell me if there is a way for us to add some kind of tag to the embedded image/file when I am adding it to the new email and then when there is a reply being sent off can I remove that previous embedded image?

Hi,


Thanks for the details.

I am afraid, there isn’t any way to tag the embedded image but you can identify the embedded images from an email message as MailMessage.LinkedResources. Regarding the exception, please share a sample message file for our review.

Hi,

Thanks for the reply. I have managed to fix the error. It was an error in the coding sample in my previous reply.

My current approach is

1. fetch message from server

2. load a MHT file into a seperate variable (msg2) - this is the header for my message

3. i then have a string variable (body) which will get the htmlBody from msg2

4. append the fetched message body to the "body" variable which will provide the both messages joined and I will have a header.

Is there any other way to include a header/footer to the email?

Thanks

Hi.


I am little confused about your question regarding the Header/Footer. Please note that the email header is the information that travels with every email, containing details about the sender, route and receiver. If you are talking about creating a custom Header like a Word document that repeats on each page in the document then this facility is unavailable using Aspose.Email for .NET.

Please share a sample message, so we can better understand your requirement and point you in a right direction to achieve your goals.

Hi,

See attached an example of what I am trying to achieve.

Thanks

Hi,


Thank you for the sample file.

I guess, you need to change the embedded image on run time. You are calling the top image as Header and the bottom as Footer.

You can identify the embedded images from LinkedResource collection of the message. Please check out the below sample code. I have created an email message with an embedded image by defining the tag in HtmlBody. This message can be sent to the recipients. When this message comes back to you, you can iterate over the LinkedResource collection of the message trying to find the ContendId of the Header image. Once found, you can either remove the associated LinkedResource and re-define a new one with another image. Or you can simply replace the ContentStream of the respective LinkedResource. The example below works on the first concept (Remove the LinkedResource and re-define a new one with same ContentId).

C#

var mailMessage = new Aspose.Email.Mail.MailMessage();
mailMessage.HtmlBody = “”;
var linkedResource = new Aspose.Email.Mail.LinkedResource(“Header.jpg”);
linkedResource.ContentId = “header”;
mailMessage.LinkedResources.Add(linkedResource);
mailMessage.Save(“output.msg”, MailMessageSaveType.OutlookMessageFormat);

mailMessage = Aspose.Email.Mail.MailMessage.Load(“output.msg”, MessageFormat.Msg);
foreach(var iterator in mailMessage.LinkedResources)
{
if (iterator.ContentId == “header”)
{
mailMessage.LinkedResources.Remove(iterator);
var newLinkedResource = new Aspose.Email.Mail.LinkedResource(“anotherHeader.jpg”);
newLinkedResource.ContentId = “header”;
mailMessage.LinkedResources.Add(newLinkedResource);
break;
}
}
mailMessage.Save(“anotherOutput.msg”, MailMessageSaveType.OutlookMessageFormat);