/// <summary> /// Another pattern that is refactored here /// Given ADODB.Recordset return a string of its xml /// </summary> /// <param name="record">The record.</param> /// <returns></returns> static internal string GetXmlFromAdoRecordset(_Recordset record) { var results = String.Empty; _Stream stream = null; try { if (null != record) { stream = new Stream(); record.Save(stream, PersistFormatEnum.adPersistXML); results = stream.ReadText(-1); } } catch (Exception ex) { LogError("GetXmlFromAdoRecordset(): Error " + ex.Message, "GetXmlFromAdoRecordset"); } finally { if (stream != null) { Marshal.ReleaseComObject(stream); } } return(results); }
static public _Recordset GetRecordsetFromXml(string xml) { _Stream stream = null; try { stream = new ADODB.Stream(); stream.Open(Missing.Value, ConnectModeEnum.adModeUnknown, StreamOpenOptionsEnum.adOpenStreamUnspecified, "", ""); stream.WriteText(xml, StreamWriteEnum.adWriteChar); stream.Position = 0; var recordset = new Recordset(); recordset.Open(stream, Missing.Value, CursorTypeEnum.adOpenUnspecified, LockTypeEnum.adLockUnspecified, 0); return(recordset); } catch (Exception ex) { ErrorLog.LogError(ex, new CallingMethod()); return(default(_Recordset)); } finally { if (stream != null) { Marshal.ReleaseComObject(stream); } } }
/// <summary> /// Save Picture to DB /// </summary> /// <param name="rsFields">Data row</param> /// <param name="sFileName">File name</param> /// <returns>True or false</returns> private dynamic SavePictureToDb(DataRow rsFields, string sFileName) { bool returnValue = false; ADODB.Stream strStream = new ADODB.Stream(); try { if (!File.Exists(sFileName)) { return(false); } strStream = new ADODB.Stream { Type = StreamTypeEnum.adTypeBinary }; strStream.Open(); strStream.LoadFromFile(sFileName); rsFields["Signature"] = strStream.Read(); returnValue = true; } catch (Exception) { // ignored } finally { strStream.Close(); } return(returnValue); }
public EML() { msg = new CDO.Message(); stm = new ADODB.Stream(); stm.Open(System.Reflection.Missing.Value, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, "", ""); stm.Type = ADODB.StreamTypeEnum.adTypeBinary; }
//Useless -> NON ho idea di come usarla correttamente public static Message ReadMessage(string emlFileName) { Message msg = new Message(); ADODB.Stream stream = new ADODB.Stream(); stream.Open(Type.Missing, ConnectModeEnum.adModeUnknown, StreamOpenOptionsEnum.adOpenStreamUnspecified, string.Empty, string.Empty); stream.LoadFromFile(emlFileName); stream.Flush(); msg.DataSource.OpenObject(stream, "_Stream"); msg.DataSource.Save(); return(msg); }
private CDO.Message GetMessage(FileInfo fileInfo) { CDO.Message msg = new CDO.Message(); ADODB.Stream stream = new ADODB.Stream(); stream.Type = StreamTypeEnum.adTypeBinary; stream.Open(Type.Missing, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, String.Empty, String.Empty); stream.LoadFromFile(fileInfo.FullName); stream.Flush(); msg.DataSource.OpenObject(stream, "_Stream"); msg.DataSource.Save(); return(msg); }
static void Main(string[] args) { string emlPath = String.Empty; if (args.Length > 0) { emlPath = args[0].ToString(); string emlContent = new StreamReader(emlPath).ReadToEnd(); CDO.Message m = new CDO.Message(); ADODB.Stream s = new ADODB.Stream(); s = m.GetStream(); s.Type = ADODB.StreamTypeEnum.adTypeText; s.LoadFromFile(emlPath); s.Flush(); if (m.To.IndexOf(ConfigurationManager.AppSettings["addressPhotos"]) >= 0) { foreach (IBodyPart bp in m.BodyPart.BodyParts) { if (bp.ContentMediaType == "image/jpeg") { string tempFilePath = ConfigurationManager.AppSettings["temporaryFilePath"] + System.Guid.NewGuid() + ".jpg"; bp.GetDecodedContentStream().SaveToFile(tempFilePath, SaveOptionsEnum.adSaveCreateOverWrite); StreamReader sr = new StreamReader(tempFilePath); Photo p = new Photo(bp.ContentMediaType, sr.BaseStream, m.Subject, m.TextBody, String.Empty); // Pass up the sending and recipient addresses as well p.SenderAddress = m.Sender; p.RecipientAddress = m.To; // Save the image p.Save(); sr.Dispose(); File.Delete(tempFilePath); } } } s.Close(); } }
/// <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(); } } }