I need to parse a few .msg files which have trail mails. Is there any way to get the sub messages and identify the initiated and the responded emails.
I do not want to use any third party tools. I am allowed to use the Outlook interop.
Below is the code that I have used to read the msg file.I am able to get the Body ,HTMLBody and other details.But I actually need all the trailing messages.
outlook._Application app = null;
outlook.MailItem item = null;
outlook.NameSpace session = null;
try
{
app = new outlook.Application();
session = app.Session;
item = session.OpenSharedItem(file) as outlook.MailItem;
}
catch(Exception ex)
{ }
If you are limited to OOM only, the only way to do that is to save each embedded message attachment as an MSG file (Attachment.SaveAsFile), then open it using Namespace.OpenSharedItem.
If using Redemption (I am its author) is an option, an MSG file can be opened using RDOSession.GetMessageFromMsgFile (similar to Namespace.OpenSharedItem in OOM), and the embedded message attachment can be accessed using the RDOAttachment.EmbeddedMsg property (returns RDOMail object) - no need to save the attachment first.
Related
I need to open mail file attachment directly from mail. Let say I have .txt file. I have already attached it to my mail. But now I need to open it, change some words and save it (it is manual part). How can I do this? My code is:
private void button2_Click(object sender, EventArgs e)
{
Outlook.Inspector currInspector = null;
Outlook.MailItem mail = null;
Outlook.Attachments attachments = null;
currInspector = Globals.ThisAddIn.Application.ActiveInspector();
if (currInspector != null) {
mail = (Outlook.MailItem)currInspector.CurrentItem;
attachments = mail.Attachments;
attachments.Add(#"C:\install\CSharp\tulemus.txt", Outlook.OlAttachmentType.olByValue);
}
The Outlook object model doesn't provide any property or method for that. You can try to read the attached files from the cache folder maintained by Outlook. See Finding Outlook temporary folder for email attachments for more information.
Also, you can use a low-level API (Extended MAPI) where you can access the PR_ATTACH_DATA_BIN property, read more about the algorithm in the Opening an attachment article.
In addition to what Eugene suggested - use Attachment.PropertyAccessor or MAPI (C++ or Delphi) to access the PR_ATTACH_DATA_BIN property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x37010102") - you can also use Redemption (I am its author): its RDOAttach.AsText / AsArray / AsStream properties allow to modify attachment contents on the fly
in c # I create a message m before sending it I show the user who can edit it and send or close it. How can I track the user closed this message or sent.
OutLookRef.Application oApp;
oApp = new OutLookRef.Application();
OutLookRef.MailItem mail = oApp.CreateItem(OutLookRef.OlItemType.olMailItem);
var pInspector = mail.GetInspector;
mail.Recipients.Add(address);
mail.Subject = subject;
mail.HTMLBody = body;
mail.Display();
All I got was to pause the code while this window is open
while (pInspector.CurrentItem is OutLookRef.MailItem)
{
System.Threading.Thread.Sleep(500);
}
also after sending, I would like to save this message to a disk, let's say mail.msg
It seems you are interested in the following properties:
MailItem.SendUsingAccount returns an Account object that represents the account under which the MailItem is to be sent.
MailItem.SentOnBehalfOfName returns a string indicating the display name for the intended sender of the mail message.
Namespace.CurrentUser returns the display name of the currently logged-on user as a Recipient object.
also after sending, I would like to save this message to a disk, let's say mail.msg
You may hook up to the ItemAdd event of the Items class which belongs to the Sent Items folder where you can save the item on the disk. The MailItem.SaveAs method saves the Microsoft Outlook item to the specified path and in the format of the specified file type. If the file type is not specified, the MSG format (.msg) is used.
I need help with converting EML to MSG, using the Exchange web service (EWS) in a Outlook Web Add-In. When i create an EML file from the MimeContent (EmailMessage.MimeContent.Content), the file output looks bad, some tags are not convert currently.
The files open good just from Windows mail app, but from Ooutlook(2016) looks bad.
I tried to find some solution from Microsoft and found this Independentsoft, a third party solution, and it is work great. the file looks good while the format is MSG. but it is to expansive licence solution for the customer (used 30 days demo).
This is what i used and work well, and try to found something like this:
//1.This code use the EWS mimeContent(the message on bytes - eml format)
//2.Create Independentsoft message object
//Independentsoft.Msg.Message constractor do the convert by
// making an msg object from an eml object.
//3. save the msg file.
Independentsoft.Email.Mime.Message mimeMessage = new Independentsoft.Email.Mime.Message(emailMessage.MimeContent.Content);
Independentsoft.Msg.Message msgMessage = Independentsoft.Msg.Message(mimeMessage);
using (MemoryStream memStream = new MemoryStream(emailMessage.MimeContent.Content.Length))
{
Directory.CreateDirectory(TempMsgDirectory);
msgMessage.Save(TempMsgDirectory + "mail.msg", true);
}
I am not aware of any Outlook problems with displaying EML files. It uses the same EML parser used to parse incoming POP3/IMAP4 messages. Please post a specific EML file that Outlook does not display correctly.
As for converting EML files to MSG, you can also use Redemption (I am its author) and its RDOSession.CreateMessageFromMsgFIle and RDOMail.Import methods. Just keep in mind that it requires the MAPI system to be present to function properly, which means Outlook must be installed locally.
Off the top of my head:
RDOSession session = new RDOSession();
RDOMail msg = session.CreateMessageFromMsgFile(TempMsgDirectory + "mail.msg");
msg.Import(TempMsgDirectory + "YouEmlFile.eml", rdoSaveAsType.olRFC822);
msg.Save();
Also keep in mind that retrieving MIME content from Exchange might not be the best idea - you will lose all MAPI specific properties and will end up with a corrupted message if the original was in the RTF format (with or without embedded OLE objects). In that case, you can use ExportItems EWS operation. Its format is not documented, but it is very close to the MSG format, and you can convert it to MSG using code similar to that above, but specifying olFTS format instead of olRFC822.
In Outlook 2013, I want the content of the mail body in a new mail programmatically.
Below is my code:
void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
{
Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
Outlook.Application oApp = new Outlook.Application();
Outlook.Explorer oExplorer = oApp.ActiveExplorer();
Outlook.Selection oSelection = oExplorer.Selection;
foreach (object item in oSelection)
{
Outlook.MailItem mi = (Outlook.MailItem)item;
mailItem.HTMLBody = mi.HTMLBody;
}
}
Everything works fine, but the image present in the original mail is not displayed. Instead it shows something like cid:image002.png.
Not sure what is the reason.
Also I want to give it to the client, so I can't save mail content locally.
If you see cid:image002.png statements in the HTML markup of the message body you need to attach embedded items to new emails as well.
The basic principle of adding an embedded image is to attach the image to the item and then using the HTMLBody to write HTML to add the attachment cid as a reference in the HTML.
Attachment attachment = newMail.Attachments.Add(
#"E:\Pictures\image001.jpg"
, OlAttachmentType.olEmbeddeditem
, null
, "Some image display name"
);
string imageCid = "image001.jpg#123";
attachment.PropertyAccessor.SetProperty(
"http://schemas.microsoft.com/mapi/proptag/0x3712001E"
, imageCid
);
newMail.HTMLBody = String.Format(
"<body><img src=\"cid:{0}\"></body>"
, imageCid
);
Be aware, you will need to save the file on disk and the re-attach it to the new email. The Add method of the Attachment class accepts a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment.
Also I'd recommend avoiding the foreach loop in the code with OOM objects. Use the for loop instead. It allows to release underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about that in the Systematically Releasing Objects article.
As i understand you want to copy the attachments from existing mailitem to new one... Then you can try this
foreach( var x in mailItem.Attachments)
{
mi.Attachments.Add(x);
}
I am trying to add a feature to my C# / .Net app for a user to email a file.
When a user has Outlook installed, I can successfully use the Outlook interop APIs to do exactly what I want.
However on a new Windows 10 install, I cannot work out how to open an email with an attachment in the default Mail app, which is from the Windows Store.
I have tried:
Using EML files, as per https://stackoverflow.com/a/25586282/2102158
The Mail app does not register itself to open EML files
Using the MAPI32.dll etc. (I used the code from https://github.com/metageek-llc/inSSIDer-2/blob/master/MetaScanner/UnhandledException/MapiMailMessage.cs)
A dialog box pops up saying there is no email program registered. It seems the mail app does not interact with MAPI
Using mailto: links.
The mail program opens, but it does not respect Attachment= or Attach= parameters
Also
Windows.ApplicationModel.Email.EmailMessage seems to be only availble on phones.
I do not want to use SMTP to send the message server side.
I also tried the MS-UNISTORE_EMAIL: and OUTLOOKMAIL: url schemes, which are associated to the Mail app, they seemed to behave the same as mailto:
There does not seem to be any way to start the Mail app from the command line
Try this:
a href='mailto:yourname#domain.com?Subject=yoursubject&Body=yourbody&Attachment=file path '
Or try by using file upload to attach the file in mail:
Msg.Attachments.Add(new Attachment(FileUpload1.FileContent, System.IO.Path.GetFileName(FileUpload1.FileName)));
Please try the following example
private async void SendEmailButton_Click(object sender, RoutedEventArgs e)
{
EmailMessage emailMessage = new EmailMessage();
emailMessage.To.Add(new EmailRecipient("***#***.com"));
string messageBody = "Hello World";
emailMessage.Body = messageBody;
StorageFolder MyFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile attachmentFile =await MyFolder.GetFileAsync("MyTestFile.txt");
if (attachmentFile != null)
{
var stream = Windows.Storage.Streams.RandomAccessStreamReference.CreateFromFile(attachmentFile);
var attachment = new Windows.ApplicationModel.Email.EmailAttachment(
attachmentFile.Name,
stream);
emailMessage.Attachments.Add(attachment);
}
await EmailManager.ShowComposeNewEmailAsync(emailMessage);
}
The ShowComposeNewEmailAsny(...) part is the magic part.