private static void PrintMail(string fileName) { var stream = new StreamClass(); stream.Open(); stream.LoadFromFile(fileName); stream.Flush(); var mail = new MessageClass(); mail.DataSource.OpenObject(stream, "_Stream"); mail.DataSource.Save(); Console.WriteLine("From: {0}", mail.From); Console.WriteLine("To: {0}", mail.To); Console.WriteLine("CC: {0}", mail.CC); Console.WriteLine("BCC: {0}", mail.BCC); Console.WriteLine("Subject: {0}", mail.Subject); Console.WriteLine("Body:"); Console.WriteLine(mail.TextBody); Console.WriteLine(); Console.WriteLine("Attachments:"); foreach (IBodyPart attachment in mail.Attachments) { Console.Write(attachment.FileName); Console.Write(" Open it? [y/N] "); var key = Console.ReadKey(); Console.WriteLine(); if (key.KeyChar != 'Y' && key.KeyChar != 'y') continue; var tempFile = Path.Combine(Path.GetTempPath(), attachment.FileName); var contentStream = attachment.GetDecodedContentStream(); contentStream.SaveToFile(tempFile, SaveOptionsEnum.adSaveCreateOverWrite); Process.Start(tempFile); } stream.Close(); }
public static RecordsetClass XmlToRecordset(string xml) { var stream = new StreamClass(); stream.Open(Missing.Value, ConnectModeEnum.adModeUnknown, StreamOpenOptionsEnum.adOpenStreamUnspecified, null, null); stream.WriteText(xml, 0); stream.Position = 0; var recordset = new RecordsetClass(); recordset.Open(stream, Missing.Value, CursorTypeEnum.adOpenUnspecified, LockTypeEnum.adLockUnspecified, 0); return(recordset); }
public void OpenEml(string filepath) { try { var stream = new StreamClass(); stream.Open(); stream.LoadFromFile(filepath); stream.Flush(); var mail = new MessageClass(); mail.DataSource.OpenObject(stream, "_Stream"); mail.DataSource.Save(); fromBox.Text = mail.From; toBox.Text = mail.To; ccBox.Text = mail.CC; bccBox.Text = mail.BCC; subjectBox.Text = mail.Subject; bodyBox.Text = mail.TextBody; attachmentsPanel.Controls.Clear(); foreach (IBodyPart attachment in mail.Attachments) { var linkLabel = new LinkLabel { Text = attachment.FileName, AutoSize = true, }; linkLabel.Links.Add(0, linkLabel.Text.Length, attachment.GetDecodedContentStream()); linkLabel.LinkClicked += LinkLabelLinkClicked; linkLabel.Disposed += LinkLabelDisposed; attachmentsPanel.Controls.Add(linkLabel); } stream.Close(); } catch (Exception) { MessageBox.Show(this, "An error occured while opening the file", "Email could not be opened", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Implementation of a Synchronous Event. This event will be triggered every time a /// mail message arrives at the mailbox where it is registered. It is synchronous because /// message will wait for the code here implemented to end execution before beeing available in mailbox. /// </summary> /// <param name="pEventInfo">A pointer to an IExStoreEventInfo interface that can be used to obtain further information related to the event.</param> /// <param name="bstrURLItem">A string containing the URL of the item on which the event is occurring.</param> /// <param name="lFlags">Bitwise AND flag gives further information about the save event.</param> public void OnSyncSave(IExStoreEventInfo pEventInfo, string bstrURLItem, int lFlags) { // Initialize local variables OWMailConfiguration configuration = null; EventLog log = null; Stream stream = null; Record recItem = null; try { // These values are checked to identify a saving event before beeing commited // Exchange Server SDK value and Exchange Server 2003 have presented different values // so both were included in this implementation. The enum is also checked here, just in case... if (lFlags == 33554440 || lFlags == 33554432 || lFlags == (int)EVT_SINK_FLAGS.EVT_SYNC_COMMITTED) { // These two must have the same value string sourceApplication = "OWMail"; //string logName = "OWMail"; // If Event Viewer entry does not exists (e.g., first time), then create it if (!EventLog.SourceExists(sourceApplication)) { EventLog.CreateEventSource(sourceApplication, sourceApplication); } log = new EventLog(sourceApplication); log.Log = sourceApplication; log.Source = sourceApplication; // Get settings from OWMail 2.0 XML configuration file configuration = new OWMailConfiguration(ConfigurationFilePath); // Get a ADO Record object from pEventInfo parameter IExStoreDispEventInfo pDispEventInfo = (IExStoreDispEventInfo)pEventInfo; recItem = (Record)pDispEventInfo.EventRecord; // Get a ADO Stream object from ADO Record stream = new StreamClass(); stream.Open(recItem, ConnectModeEnum.adModeRead, StreamOpenOptionsEnum.adOpenStreamFromRecord, string.Empty, string.Empty); // Creates a new Guid to use when saving message and attachments to TEMP directory. // TEMP directory path is stored in configuration file. string guid = Guid.NewGuid().ToString(); string filePath = configuration.TempDirectoryPath + @"\" + guid + ".eml"; // Saves the ADO Stream to TEMP directory stream.SaveToFile(filePath, SaveOptionsEnum.adSaveCreateNotExist); // Creates a CDO Message object Message message = new MessageClass(); // Gets message data loaded from ADO Record object message.DataSource.OpenObject(recItem, "_Record"); // Assigns local variables from loaded message fields string from = message.From; string to = message.To; string cc = message.CC; string subject = (string)message.Fields["urn:schemas:httpmail:normalizedsubject"].Value; DateTime mailDate = message.ReceivedTime; string body = (string)message.Fields["urn:schemas:httpmail:textdescription"].Value; // Use event source URL to extract mailbox name string[] urlElements = bstrURLItem.Split('/'); string mailboxName = urlElements[urlElements.Length - 3]; // Use mailbox name to extract its operation settings from configuration string userLogin = configuration.GetUserLoginByMailboxName(mailboxName, '@'); // OWApi Web Service accepts blank credentials string password = string.Empty; //string entityName = string.Empty; // This list will hold all message entities encapsulated in MailContact objects ArrayList list = new ArrayList(); // This object has functions to ease the extraction of message entity fields MailContactHelper mailContactHelper = new MailContactHelper(); // Get a list with all entities referred in FROM, TO, CC, BCC message fields list = mailContactHelper.GetMailContactsList(from, to, cc); // Directory name to be created in case Extract Attachments option is activated string directoryName = configuration.TempDirectoryPath + @"\" + guid; // OWApiHelper is a wrapper for OWApi Web Service InsertRegistry method. // If call is successfull, result will contain the ID of record created in tblEntities int result = OWApiHelper.InsertRegistry(userLogin, password, list, configuration, directoryName, guid, filePath, message.Attachments, subject, mailDate, body); //Not Register if (result == 0) { throw new Exception("Não é possivel inserir o registo."); } // Clean up temporary message file if (File.Exists(filePath)) { File.Delete(filePath); } // Clean up temporary attachments directory if (Directory.Exists(directoryName)) { Directory.Delete(directoryName, true); } } } catch (Exception ex) { Exception ex2 = null; log.WriteEntry(ex.Message + " " + ex.StackTrace, EventLogEntryType.Error); ex2 = ex.InnerException; while (ex2 != null) { log.WriteEntry(ex2.Message + " " + ex2.StackTrace, EventLogEntryType.Error); ex2 = ex.InnerException; } throw ex; } finally { // Always cleans up unmanaged resources if (stream != null) { stream.Close(); } if (recItem != null) { recItem.Close(); } } }