/// <summary> /// process an email and call processAttachement for every attachement /// which fires an event for every new attachement /// </summary> /// <param name="m"></param> /// <returns></returns> public int processMail(IMailMessage m)// (Microsoft.Exchange.WebServices.Data.EmailMessage m) { int iRet = 0; if (m == null) { utils.helpers.addLog("processMail: null msg"); OnStateChanged(new StatusEventArgs(StatusType.error, "NULL msg")); return(iRet); } try { OnStateChanged(new StatusEventArgs(StatusType.none, "processing " + m.Attachements.Length.ToString() + " attachements")); utils.helpers.addLog(m.User + "," + m.Subject + ", # attachements: " + m.Attachements.Length.ToString() + "\r\n"); //get data from email string sReceivededBy = m.User; DateTime dtSendAt = m.timestamp; //get data from body LicenseMailBodyData bodyData = new LicenseMailBodyData(); OnStateChanged(new StatusEventArgs(StatusType.none, "processing mail body")); utils.helpers.addLog("processing mail body"); bodyData = processMailBody(m); utils.helpers.addLog("mail body='" + m.Body + "'"); utils.helpers.addLog(bodyData.dump()); if (m.Attachements.Length > 0) { //process each attachement foreach (Attachement a in m.Attachements) { try { utils.helpers.addLog("start processAttachement...\r\n"); iRet += processAttachement(a, bodyData, m); OnStateChanged(new StatusEventArgs(StatusType.none, "processed " + iRet.ToString() + " licenses")); utils.helpers.addLog("processAttachement done\r\n"); } catch (Exception ex) { utils.helpers.addExceptionLog(ex.Message); OnStateChanged(new StatusEventArgs(StatusType.error, "Exception1 in processAttachement: " + ex.Message)); } } } } catch (Exception ex) { utils.helpers.addLog("Exception: " + ex.Message); OnStateChanged(new StatusEventArgs(StatusType.error, "Exception2 in processAttachement: " + ex.Message)); } utils.helpers.addLog("processMail did process " + iRet.ToString() + " files"); return(iRet); }
public static LicenseMailBodyData get(IMailMessage msg) { LicenseMailBodyData data = new LicenseMailBodyData(); string sBody = msg.Body; sBody = sBody.Replace("\r\n", ";"); var expression = new Regex( @"Order Number:[ ]+(?<order_number>[\S]+);" + @".+Order Date:[ ]+(?<order_date>[\S]+);" + @".+Your PO Number:[ ]+(?<po_number>[\S]+);" + @".+End Customer:[ ]+(?<end_customer>[\S]+);" + "" ); string currentMatch = "START"; string gMatch = ""; try { var match = expression.Match(sBody); currentMatch = "order_number"; //utils.helpers.addLog(string.Format("order_number......{0}", match.Groups["order_number"])); data.OrderNumber = match.Groups["order_number"].Value; currentMatch = "order_date"; //utils.helpers.addLog(string.Format("order_date........{0}", match.Groups["order_date"])); gMatch = match.Groups["order_date"].Value.TrimEnd(new char[] { ';' }); data.OrderDate = getDateTimeFromUSdateString(gMatch); currentMatch = "po_number"; //utils.helpers.addLog(string.Format("po_number........ {0}", match.Groups["po_number"])); data.yourPOnumber = match.Groups["po_number"].Value; currentMatch = "end_customer"; //utils.helpers.addLog(string.Format("end_customer..... {0}", match.Groups["end_customer"])); data.EndCustomer = match.Groups["end_customer"].Value; currentMatch = "Product"; expression = new Regex(@".+Product:[ ]+(?<product>.+);.+Quantity"); match = expression.Match(sBody); //utils.helpers.addLog(string.Format("product...........{0}", match.Groups["product"])); data.Product = match.Groups["product"].Value; currentMatch = "Quantity"; expression = new Regex(@".+Quantity:[ ]+(?<quantity>[0-9]+);"); match = expression.Match(sBody); //utils.helpers.addLog(string.Format("quantity..........{0}", match.Groups["quantity"])); data.Quantity = Convert.ToInt16(match.Groups["quantity"].Value.TrimEnd(new char[] { ';' })); } catch (Exception ex) { utils.helpers.addLog("Exception processing mail body regex: " + ex.Message + "\r\ncurrent expression: " + expression + "\r\ncurrentMatch: " + currentMatch); throw new FormatException("RegEx fails for MailBody"); } return(data); }
public int processAttachement(Attachement att, LicenseMailBodyData data, IMailMessage mail) { int iCount = 0; LicenseXML xmlData = LicenseXML.Deserialize(att.data); foreach (license ldata in xmlData.licenses) { utils.helpers.addLog("processAttachement: new LicenseData...\r\n"); LicenseData licenseData = new LicenseData(ldata.id, ldata.user, ldata.key, data.OrderNumber, data.OrderDate, data.yourPOnumber, data.EndCustomer, data.Product, data.Quantity, mail.User, mail.timestamp); //if (_licenseDataBase.addQueued(licenseData)) utils.helpers.addLog("firing license_mail event\r\n"); OnStateChanged(new StatusEventArgs(StatusType.license_mail, licenseData)); iCount++; //if (_licenseDataBase.add(ldata.id, ldata.user, ldata.key, data.OrderNumber, data.OrderDate, data.yourPOnumber, data.EndCustomer, data.Product, data.Quantity, mail.User, mail.timestamp)) // iCount++; //utils.helpers.addLog("start _licenseDataBase.add() done\r\n"); } #region alternative_code /* * // Request all the attachments on the email message. This results in a GetItem operation call to EWS. * m.Load(new Microsoft.Exchange.WebServices.Data.PropertySet(Microsoft.Exchange.WebServices.Data.EmailMessageSchema.Attachments)); * foreach (Microsoft.Exchange.WebServices.Data.Attachment att in m.Attachments) * { * if (att is Microsoft.Exchange.WebServices.Data.FileAttachment) * { * Microsoft.Exchange.WebServices.Data.FileAttachment fileAttachment = att as Microsoft.Exchange.WebServices.Data.FileAttachment; * * //get a temp file name * string fname = System.IO.Path.GetTempFileName(); //utils.helpers.getAppPath() + fileAttachment.Id.ToString() + "_" + fileAttachment.Name * * // Load the file attachment into memory. This gives you access to the attachment content, which * // is a byte array that you can use to attach this file to another item. This results in a GetAttachment operation * // call to EWS. * fileAttachment.Load(); * * // Load attachment contents into a file. This results in a GetAttachment operation call to EWS. * fileAttachment.Load(fname); * addLog("Attachement file saved to: " + fname); * * // Put attachment contents into a stream. * using (System.IO.FileStream theStream = * new System.IO.FileStream(utils.helpers.getAppPath() + fileAttachment.Id.ToString() + "_" + fileAttachment.Name, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite)) * { * //This results in a GetAttachment operation call to EWS. * fileAttachment.Load(theStream); * } * * //load into memory stream, seems the only stream supported * using (System.IO.MemoryStream ms = new System.IO.MemoryStream(att.Size)) * { * fileAttachment.Load(ms); * using (System.IO.FileStream fs = new System.IO.FileStream(fname, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite)) * { * ms.CopyTo(fs); * fs.Flush(); * } * } * addLog("saved attachement: " + fname); * iRet++; * } * } */ #endregion return(iCount); }
public static LicenseMailBodyData processMailBody(IMailMessage msg) { LicenseMailBodyData bodyData = LicenseMailBodyData.get(msg); return(bodyData); }
public int processAttachement(Attachement att, LicenseMailBodyData data, IMailMessage mail) { int iCount=0; LicenseXML xmlData = LicenseXML.Deserialize(att.data); foreach(license ldata in xmlData.licenses){ utils.helpers.addLog("processAttachement: new LicenseData...\r\n"); LicenseData licenseData = new LicenseData(ldata.id, ldata.user, ldata.key, data.OrderNumber, data.OrderDate, data.yourPOnumber, data.EndCustomer, data.Product, data.Quantity, mail.User, mail.timestamp); //if (_licenseDataBase.addQueued(licenseData)) utils.helpers.addLog("firing license_mail event\r\n"); OnStateChanged(new StatusEventArgs(StatusType.license_mail, licenseData)); iCount++; //if (_licenseDataBase.add(ldata.id, ldata.user, ldata.key, data.OrderNumber, data.OrderDate, data.yourPOnumber, data.EndCustomer, data.Product, data.Quantity, mail.User, mail.timestamp)) // iCount++; //utils.helpers.addLog("start _licenseDataBase.add() done\r\n"); } #region alternative_code /* // Request all the attachments on the email message. This results in a GetItem operation call to EWS. m.Load(new Microsoft.Exchange.WebServices.Data.PropertySet(Microsoft.Exchange.WebServices.Data.EmailMessageSchema.Attachments)); foreach (Microsoft.Exchange.WebServices.Data.Attachment att in m.Attachments) { if (att is Microsoft.Exchange.WebServices.Data.FileAttachment) { Microsoft.Exchange.WebServices.Data.FileAttachment fileAttachment = att as Microsoft.Exchange.WebServices.Data.FileAttachment; //get a temp file name string fname = System.IO.Path.GetTempFileName(); //utils.helpers.getAppPath() + fileAttachment.Id.ToString() + "_" + fileAttachment.Name // Load the file attachment into memory. This gives you access to the attachment content, which // is a byte array that you can use to attach this file to another item. This results in a GetAttachment operation // call to EWS. fileAttachment.Load(); // Load attachment contents into a file. This results in a GetAttachment operation call to EWS. fileAttachment.Load(fname); addLog("Attachement file saved to: " + fname); // Put attachment contents into a stream. using (System.IO.FileStream theStream = new System.IO.FileStream(utils.helpers.getAppPath() + fileAttachment.Id.ToString() + "_" + fileAttachment.Name, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite)) { //This results in a GetAttachment operation call to EWS. fileAttachment.Load(theStream); } //load into memory stream, seems the only stream supported using (System.IO.MemoryStream ms = new System.IO.MemoryStream(att.Size)) { fileAttachment.Load(ms); using (System.IO.FileStream fs = new System.IO.FileStream(fname, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite)) { ms.CopyTo(fs); fs.Flush(); } } addLog("saved attachement: " + fname); iRet++; } } */ #endregion return iCount; }
public static LicenseMailBodyData get(IMailMessage msg) { LicenseMailBodyData data = new LicenseMailBodyData(); string sBody = msg.Body; sBody = sBody.Replace("\r\n", ";"); var expression = new Regex( @"Order Number:[ ]+(?<order_number>[\S]+);" + @".+Order Date:[ ]+(?<order_date>[\S]+);" + @".+Your PO Number:[ ]+(?<po_number>[\S]+);"+ @".+End Customer:[ ]+(?<end_customer>[\S]+);"+ "" ); string currentMatch = "START"; string gMatch = ""; try { var match = expression.Match(sBody); currentMatch = "order_number"; //utils.helpers.addLog(string.Format("order_number......{0}", match.Groups["order_number"])); data.OrderNumber = match.Groups["order_number"].Value; currentMatch = "order_date"; //utils.helpers.addLog(string.Format("order_date........{0}", match.Groups["order_date"])); gMatch = match.Groups["order_date"].Value.TrimEnd(new char[] { ';' }); data.OrderDate = getDateTimeFromUSdateString(gMatch); currentMatch = "po_number"; //utils.helpers.addLog(string.Format("po_number........ {0}", match.Groups["po_number"])); data.yourPOnumber = match.Groups["po_number"].Value; currentMatch = "end_customer"; //utils.helpers.addLog(string.Format("end_customer..... {0}", match.Groups["end_customer"])); data.EndCustomer = match.Groups["end_customer"].Value; currentMatch = "Product"; expression = new Regex(@".+Product:[ ]+(?<product>.+);.+Quantity"); match = expression.Match(sBody); //utils.helpers.addLog(string.Format("product...........{0}", match.Groups["product"])); data.Product = match.Groups["product"].Value; currentMatch = "Quantity"; expression = new Regex(@".+Quantity:[ ]+(?<quantity>[0-9]+);"); match = expression.Match(sBody); //utils.helpers.addLog(string.Format("quantity..........{0}", match.Groups["quantity"])); data.Quantity = Convert.ToInt16(match.Groups["quantity"].Value.TrimEnd(new char[] { ';' })); } catch (Exception ex) { utils.helpers.addLog("Exception processing mail body regex: " + ex.Message + "\r\ncurrent expression: "+expression+"\r\ncurrentMatch: "+currentMatch); throw new FormatException("RegEx fails for MailBody"); } return data; }
// (Microsoft.Exchange.WebServices.Data.EmailMessage m) /// <summary> /// process an email and call processAttachement for every attachement /// which fires an event for every new attachement /// </summary> /// <param name="m"></param> /// <returns></returns> public int processMail(IMailMessage m) { int iRet = 0; if (m == null) { utils.helpers.addLog("processMail: null msg"); OnStateChanged(new StatusEventArgs(StatusType.error, "NULL msg")); return iRet; } try { OnStateChanged(new StatusEventArgs(StatusType.none, "processing "+m.Attachements.Length.ToString()+" attachements" )); utils.helpers.addLog(m.User +","+ m.Subject + ", # attachements: " + m.Attachements.Length.ToString() + "\r\n"); //get data from email string sReceivededBy = m.User; DateTime dtSendAt = m.timestamp; //get data from body LicenseMailBodyData bodyData = new LicenseMailBodyData(); OnStateChanged(new StatusEventArgs(StatusType.none, "processing mail body")); utils.helpers.addLog("processing mail body"); bodyData = processMailBody(m); utils.helpers.addLog("mail body='" + m.Body + "'"); utils.helpers.addLog( bodyData.dump() ); if (m.Attachements.Length > 0) { //process each attachement foreach (Attachement a in m.Attachements) { try { utils.helpers.addLog("start processAttachement...\r\n"); iRet += processAttachement(a, bodyData, m); OnStateChanged(new StatusEventArgs(StatusType.none, "processed " + iRet.ToString() + " licenses")); utils.helpers.addLog("processAttachement done\r\n"); } catch (Exception ex) { utils.helpers.addExceptionLog(ex.Message); OnStateChanged(new StatusEventArgs(StatusType.error, "Exception1 in processAttachement: " + ex.Message)); } } } } catch (Exception ex) { utils.helpers.addLog("Exception: " + ex.Message); OnStateChanged(new StatusEventArgs(StatusType.error, "Exception2 in processAttachement: " + ex.Message)); } utils.helpers.addLog("processMail did process " + iRet.ToString() + " files"); return iRet; }