I need save a MailMessage to .msg file. In this article have a solution but when I save as .msg file, it does not work in MS Outlook. It only great work when I save as.eml file.
How to save MailMessage object to disk as *.eml or *.msg file
Note that I get when open the .msg file:
Cannot open file: path. The file may not exist, you may not have permission to open it, or it may open in another program. Right-click the folder that contains the file, and then click Properties check your permisstions for the folder.
Thank all.
In Interop Outlook, this is how to locally save a mail as .msg.
mailItem.SaveAs(#"c:\path\to\save\mail.msg", Outlook.OlSaveAsType.olMSG);
How exactly are you creating the MSG file? It is completely different from an EML file - see https://stackoverflow.com/questions/16229591/difference-between-a-msg-file-and-a-eml-file/16230261#16230261
MSG file format is a binary IStorage file, and its format is documented. You can parse your EML (MIME) file and copy one property at a time to a programmatically created MSG file.
If using Redemption is an option (I am its author), you can use Session.CreateMessageFromMsgFile to create a new MSG file and RDOMail.Import method to import your existing EML file.
set Session = CreateObject("Redemption.RDOSession")
set Msg = Session.CreateMessageFromMsgFile("c:\temp\test.msg")
Msg.Sent = true '//since Import does not copy this property
Msg.Import("c:\temp\test.eml", 1024) ' //1024 is olRfc822
Msg.Save
Related
Is there a way to create a MailMessage object from a directory path of a .msg file?
Such as MailMessage x = new MailMessage("path")
There is no direct conversion between these two entities out of the box - the Outlook message file and the MailMessage class from the .net framework. You can automate Outlook to get the instance of the MSG file instantiated using the NameSpace.OpenSharedItem method which is used to open iCalendar appointment (.ics) files, vCard (.vcf) files, and Outlook message (.msg) files.
You can also use Redemption for that, look for RDOSession.GetMessageFromMsgFile.
Thanks for the replies but I found the answer on this existing post: C# Outlook interop and OpenSharedItem for opening MSG files
Like Eugene said, I used the Microsoft.Office.Interop.Outlook.Application to open the .msg file via Outlook.
For instance, I have a word document as an Outlook attachment. I want to have a C# program that will find that file's local location, process it into a PDF, and then change the email's attachment to that new PDF file. The next time that attachment is opened, it will open the PDF and not the word document.
Edit 1: The email attachment is in my inbox, as someone has sent this file to me. I am able to edit the actual word file just like any other document just fine. I figure this would be possible by adding
Is this function possible? and remoreceived chments to existing recieved emails as well.
Yes, that is possible. You can develop an Outlook COM add-in (for example, a VSTO based one). The Attachment class provides the SaveAsFile method which saves the attachment to the specified path. Then you can automate Word where you may open the file just saved and re-save it using the .pdf file format. Or you may consider using third-party components that don't require Word installed on the system. After converting the document and getting the required pdf file you may remove an old attachment and re-add a new one. The Attachments.Add creates a new attachment in the Attachments collection.
You may find the following articles helpful:
Attach a File to a Mail Item
Modify an Attachment of an Outlook Email Message
How To: Add an attachment to an Outlook e-mail message
I want to get full path (origin source file) of attachment in mail Outlook, I use object Attachment in Outlook to get Pathname: Attachment.PathName, but it returns null.
So, how can I get the source file's link?
Example: this is a link to image attachment in a my email
Email attachments do not have a source path. Once they were sent, they are part of the message, not part of any file system, regardless of their source path. The best you can do is get the attachment file name.
As you can read here, Attachment.PathName only works with linked files, not attached files:
Remarks:
This property is only valid for linked files.
I want to add an attachment to use as a header image in an email. But when I try to attach the file the path is reading from windows and not from my solution directory location. How do I do that?
var msg = new MailMessage(fromMailAdress, toEmail) { Subject = subject, Body = emailBody };
msg.Attachments.Add(new Attachment("../images/logo.jpg"));
reads from c:\windows\system32\images\logo.jpg'
I want it from the solution level project/images/logo.jpg
Here's what you'll want to do:
Create a folder (say attachment) in your solution to store your attachment files.
Use ~/attachment/filename to access the file.
Since ~/attachment/filename is a virtual path, you'll have to convert it to a physical path using Control.ResolveUrl() or a more appropriate way. Check this out for more on it: ResolveUrl without an ASP.NET Page
I don't think you mentioned what type of solution you're doing this from.
In a traditional Windows app, you have a few options. You can make sure the logo file is copied to your output directory by right-clicking on it in the project explorer, choosing Properties, and setting the "Copy to Output Directory" feature to "Copy if newer". You will then need to modify your code above to load the image from the executing assembly's path...it's been my experience that Visual Studio will create the "images" project folder.
string logopath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "images/logo.jpg");
A better option, as described by DJ KRAZE, is to add your logo as a resource. If you add it to your project as a resource, then you can write it to a temporary file when it doesn't exist. Then, attach it using the path that you created for the temporary file. You can then delete it when you're done, or keep it around and only recreate it when it doesn't exist.
The third option is to upload the logo to a website and then reference it in your email message by the URL, instead of including it as an attachment. If you are creating a HTML mail message, this is probably the best solution.
I'm working with the EWS Managed API 2.0.
At this moment I can save EmailMessages to my harddrive as *.eml files.
However I can't open them correctly to show the content.
How can an EmailMessage (.eml) be saved as an .html, .doc or .txt file directly?
Use following code if you want to save it as .eml
message.Load(new PropertySet(ItemSchema.MimeContent));
MimeContent mimcon = message.MimeContent;
FileStream fStream = new FileStream("c:\test.eml", FileMode.Create);
fStream.Write(mimcon.Content, 0, mimcon.Content.Length);
fStream.Close();
For msg file look at following link:
http://msdn.microsoft.com/en-us/library/cc463912%28EXCHG.80%29.aspx
After saving it as .eml file you can look at following post for parsing it:
Recommendations on parsing .eml files in C#
Hope its helpful.