public void TestSetup() { m_EndOfMessageStrategy = new BasicEndOfMessageStrategy(); m_Reader = new MailReader(); char[] ar = MIMERTests.Strings.MailMessage1.ToCharArray(); byte[] bar = Array.ConvertAll <char, byte>(ar, new Converter <char, byte>( delegate(char input) { return((byte)input); })); m_Stream = new MemoryStream(bar); StringBuilder builder = new StringBuilder(); for (int i = 0; i < 100; i++) { builder.Append(MIMERTests.Strings.MailMessage1); } var multiMessageBytes = from char c in builder.ToString() select Convert.ToByte(c); m_MultiMessageStream = new MemoryStream(multiMessageBytes.ToArray()); m_EmptyStream = new MemoryStream(); IEnumerable <byte> mailMessageWithAttachmentBytes = from char c in Strings.MailMessageWithAttachment select Convert.ToByte(c); m_AttachmentStream = new MemoryStream(mailMessageWithAttachmentBytes.ToArray()); m_EmbeddedMessagaesStream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes( MIMERTests.Strings.MailMessageWithRecursivlyEmbeddedMessage)); m_EmbeddedWithAttachmentsStream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes( MIMERTests.Strings.MailMessageWithEmbeddedWithAttachments)); }
private void ReadMultipartBody(MailReader reader, MailProperties properties) { string separator = GetSeparator(properties); string subtype = properties.GetMimeMediaSubtype(); reader.SetSeparator(null); string reply = reader.ReadLine(); ResetTimer(); if (reply != null) { while (reply != null && !reply.StartsWith(separator)) { reply = reader.ReadLine(); ResetTimer(); } } reader.SetSeparator(separator); while (true) { MailProperties props = ReadHeader(reader); if (props == null) { break; } ReadMessage(reader, props, separator); } }
private void UpdateMails() { LastUpdate = DateTime.Now; var lastDate = _proc.LastMailDate(_fsMails); FSharpList <Common.EMail> newMails; try { newMails = MailReader.downloadMailsAfterDate(lastDate); } catch (Exception ex) { Debug.WriteLine("{0} - Exception: {1}", DateTime.Now, ex.Message); return; } var updatedCollection = MailReader.updateAndWriteAfterLastDate(_fsMails, lastDate); LastUpdate = DateTime.Now; Debug.WriteLine("{0} - New items: {1}", LastUpdate, updatedCollection.Length - _fsMails.Length); _fsMails = updatedCollection; _mails = _fsMails.ToList(); }
public void GetUnreadMails_IfNotUnreadMailExists_ReturnsEmptyEnumeration() { MailClientStub PopClient = new MailClientStub(); var DateTime = GetDateTimeStrub("01/01/2013"); MailReader Reader = new MailReader(PopClient, new MailFilterFake(), DateTime.Object); IEnumerable<IMessage> Emails = Reader.GetUnreadMails(null).Mails; Assert.AreEqual(0, Emails.Count()); }
public void TestThatCorrectNumberOfAttachmentsGetsParsed() { Assert.That(m_Stream, Iz.Not.Null); MIMER.IEndCriteriaStrategy endofmessage = new BasicEndOfMessageStrategy(); var message = new MailReader().ReadMimeMessage(ref m_Stream, endofmessage); Assert.That(message.Attachments.Count(), Iz.EqualTo(117)); }
public void TestThatCorrectNumberOfHtmlDocumentsGetsParsed() { Assert.That(m_Stream, Iz.Not.Null); MIMER.IEndCriteriaStrategy endofmessage = new BasicEndOfMessageStrategy(); var message = new MailReader().ReadMimeMessage(ref m_Stream, endofmessage); Assert.That(message.Attachments.Count(a => a.Type.Equals("text") && a.SubType.Equals("html")), Iz.EqualTo(10)); }
public MailMessage(MailReader reader, string attachmentsPath, int timeout) { this.reader = reader; this.attachments = ""; this.attachmentsPath = attachmentsPath; this.timeout = timeout; this.mainThread = Thread.CurrentThread; }
private MailProperties ReadHeader(MailReader reader) { string temp; string reply = reader.ReadLine(); ResetTimer(); if ((reply == null) || reply.Equals(".")) { return(null); } MailProperties properties = null; while (!string.IsNullOrEmpty(reply)) { if (properties == null) { properties = new MailProperties(); } temp = reader.ReadLine(); ResetTimer(); if (temp == null) { return(properties); } if (temp.Equals(".")) { return(properties); } bool returnProps = false; // MultiLine headers have a space\tab at begining of each extra line while ((temp.StartsWith(" ")) || (temp.StartsWith("\t"))) { reply = reply + CRLF + temp; temp = reader.ReadLine(); ResetTimer(); if ((temp == null) || temp.Equals(".")) { returnProps = true; break; } } properties.PutKey(reply); reply = temp; if (returnProps) { return(properties); } } return(properties); }
public void TestThatCssFileGetsParsed() { Assert.IsNotNull(m_Stream); var reader = new MailReader(); MIMER.IEndCriteriaStrategy endofmessage = new BasicEndOfMessageStrategy(); var message = reader.ReadMimeMessage(ref m_Stream, endofmessage); Assert.That(message.Attachments.Count(a => a.Type.Equals("text") && a.SubType.Equals("css")), Iz.EqualTo(1)); }
public void Decode(MailReader input, Stream output, Encoding enc, bool header, GeneXus.Mail.Util.AsyncRunner runner) { string result = DecodeImp(input, enc, header, runner); if (result.StartsWith("\r\n")) { result = result.Substring(2); } byte[] buffer = enc.GetBytes(result); output.Write(enc.GetBytes(result), 0, buffer.Length); }
public void GetUnreadMail_ThereAreOneMail_ReturnsTheMail() { string ValidEmail = "*****@*****.**"; var MessageFake = new MessageFake(ValidEmail); MailClientStub PopClient = new MailClientStub(MessageFake); var DateTime = GetDateTimeStrub("01/01/2013"); MailReader Reader = new MailReader(PopClient, new MailFilterFake(ValidEmail), DateTime.Object); IEnumerable<IMessage> Emails = Reader.GetUnreadMails(null).Mails.ToList(); Assert.AreEqual(1, Emails.Count()); Assert.AreSame(MessageFake, Emails.First()); }
static void Main(string[] args) { string path = "."; if (args.Count() > 0) { path = args[0]; } var files = Directory.GetFiles(path, "*.doc"); FileStream csvFile = new FileStream("output.csv", FileMode.Create); StreamWriter sw = new StreamWriter(csvFile, Encoding.Unicode); foreach (var file in files) { string content = File.ReadAllText(file); content = content.Replace("\n", "\r\n"); Stream fs = new MemoryStream(Encoding.ASCII.GetBytes(content)); var reader = new MailReader(); MIMER.IEndCriteriaStrategy endofmessage = new BasicEndOfMessageStrategy(); //MIMER.IEndCriteriaStrategy endofmessage = new EndOfLineStrategy(); var message = reader.ReadMimeMessage(ref fs, endofmessage); //reader.Read(ref fs, endofmessage); var html = message.Body.FirstOrDefault <KeyValuePair <string, string> >().Value; var htmldoc = new HtmlAgilityPack.HtmlDocument(); htmldoc.LoadHtml(html); //var nodes = htmldoc.DocumentNode.SelectNodes("//td"); //foreach (var node in nodes) //{ // if (node.InnerText == "15021991585" || node.InnerText.Contains("丁曹凯")) // { // Console.WriteLine(node.InnerText + " " + node.XPath); // } //} //var node = htmldoc.DocumentNode.SelectNodes("//a"); var email = htmldoc.DocumentNode.SelectSingleNode("/html[1]/body[1]/table[2]/tr[1]/td[1]/html[1]/body[1]/table[1]/tr[1]/td[1]/table[1]/tr[1]/td[2]/table[1]/tr[2]/td[3]/table[1]/tr[1]/td[2]/a[1]"); var name = htmldoc.DocumentNode.SelectSingleNode("/html[1]/body[1]/table[2]/tr[1]/td[1]/html[1]/body[1]/table[1]/tr[1]/td[1]/table[1]/tr[1]/td[2]"); var phone = htmldoc.DocumentNode.SelectSingleNode("/html[1]/body[1]/table[2]/tr[1]/td[1]/html[1]/body[1]/table[1]/tr[1]/td[1]/table[1]/tr[1]/td[2]/table[1]/tr[2]/td[2]/table[1]/tr[1]/td[2]"); //Console.WriteLine(email.InnerText + " " + name.InnerText + " " + phone.InnerText); int start = name.InnerText.IndexOf(" ") + 6; int end = name.InnerText.IndexOf("流程"); string line = name.InnerText.Substring(start, end - start).Replace(" ", "").Trim() + "," + phone.InnerText + "," + email.InnerText; sw.WriteLine(line); } sw.Close(); csvFile.Close(); }
private void ReadMessage(MailReader reader, MailProperties partProps, string separator) { reader.SetEncoding(partProps.GetCharset()); string mimeMediaType = partProps.GetMimeMediaType(); if (string.Compare(mimeMediaType, "multipart", true) == 0) { ReadMultipartBody(reader, partProps); } else { ReadBody(reader, partProps, separator); } }
public void DecodeBase64() { var reader = new MailReader(); MIMER.IEndCriteriaStrategy endofmessage = new BasicEndOfMessageStrategy(); var stream = (Stream)File.Open("purchase-order.mhtml", FileMode.Open); var message = reader.ReadMimeMessage(ref stream, endofmessage); stream.Close(); Assert.Equal(2, message.Attachments.Count); Assert.Equal("html", message.Attachments[0].SubType); }
public override void DecodeText(MailReader input, TextWriter output, GeneXus.Mail.Util.AsyncRunner runner) { string line; while (true) { line = input.ReadLine(); ResetTimer(runner); if ((line == null) || line.Equals(".")) { break; } output.WriteLine(line); } }
public void GetUnreadMail_ThereAreTargetAndNotTargetMails_ReturnsTargetFilter() { // arrange string ValidFrom = "*****@*****.**"; var ValidMessageFake = new MessageFake(ValidFrom); MailClientStub PopClient = new MailClientStub(ValidMessageFake, new MessageFake("*****@*****.**")); var DateTime = GetDateTimeStrub("01/01/2013"); MailReader Reader = new MailReader(PopClient, new MailFilterFake(ValidFrom), DateTime.Object); // action IEnumerable<IMessage> Emails = Reader.GetUnreadMails(null).Mails.ToList(); // assert Assert.AreEqual(1, Emails.Count()); Assert.AreSame(ValidMessageFake, Emails.First()); }
public override void DecodeFile(MailReader input, Stream output, GeneXus.Mail.Util.AsyncRunner runner) { string line; while (true) { line = input.ReadLine(); ResetTimer(runner); if ((line == null) || line.Equals(".")) { break; } byte[] bytes = input.GetEncoding().GetBytes(line + CRLF); output.Write(bytes, 0, bytes.Length); } }
private byte[] GetFromBase64String(MailReader input, GeneXus.Mail.Util.AsyncRunner runner) { StringBuilder strBuilder = new StringBuilder(); string str = null; while (true) { str = input.ReadLine(); ResetTimer(runner); if (string.IsNullOrEmpty(str) || str.Equals(".")) { break; } strBuilder.Append(str); } return(Convert.FromBase64String(strBuilder.ToString())); }
public void TestParse() { Assert.IsNotNull(m_Stream); MIMER.RFC2045.MailReader m = new MailReader(); MIMER.IEndCriteriaStrategy endofmessage = new BasicEndOfMessageStrategy(); MIMER.RFC2045.IMimeMailMessage message = m.ReadMimeMessage(ref m_Stream, endofmessage); Assert.AreEqual("*****@*****.**", message.To[0].Address); Assert.AreEqual("*****@*****.**", message.CarbonCopy[0].Address); Assert.AreEqual("Testing the .eml format", message.Subject); Assert.AreEqual(3, message.Attachments.Count); Assert.That(message.Attachments.Count(x => x.Name != null && x.Name.Equals("cp_bg_black800.gif")), Iz.EqualTo(1)); System.Net.Mail.MailMessage mailmessage = message.ToMailMessage(); Assert.IsNull(mailmessage.From); Assert.AreEqual("*****@*****.**", mailmessage.CC[0].Address); Assert.AreEqual(message.TextMessage, mailmessage.Body); Assert.AreEqual(message.Attachments.Count, mailmessage.Attachments.Count); }
string ExtractHtml(byte[] input) { var reader = new MailReader(); MIMER.IEndCriteriaStrategy endofmessage = new BasicEndOfMessageStrategy(); var stream = (Stream) new MemoryStream(input); var message = reader.ReadMimeMessage(ref stream, endofmessage); stream.Close(); foreach (var attachment in message.Attachments) { if (attachment.Type == "text" && attachment.SubType.StartsWith("htm")) { return(Encoding.UTF8.GetString(attachment.Data)); } } return(null); }
private string DecodeImp(MailReader input, Encoding enc, bool header, GeneXus.Mail.Util.AsyncRunner runner) { int charRead = 0; StringBuilder sb = new StringBuilder(); while ((charRead = input.Read()) != -1) { if (header) { if (charRead == 13 || charRead == 10) // CR LF must be removed from header. { continue; } else if (charRead == 95) // Underscore in Header must be replaced by space. { charRead = ' '; } } sb.Append((char)charRead); ResetTimer(runner); } return(DecodeQuotedPrintable(sb.ToString(), enc)); }
public override void DecodeFile(MailReader input, Stream output, GeneXus.Mail.Util.AsyncRunner runner) { byte[] converted = GetFromBase64String(input, runner); output.Write(converted, 0, converted.Length); }
private void ReadBody(MailReader reader, MailProperties partProps, String separator) { string contentType = partProps.GetKeyPrincipal("Content-Type"); bool isTextPlain = (string.Compare(contentType, "text/plain", true) == 0); bool isTextHtml = (string.Compare(contentType, "text/html", true) == 0); bool isAttachment = (!isTextPlain && !isTextHtml) || AttachmentContentDisposition(partProps); string oldSeparator = reader.GetSeparator(); reader.SetSeparator(separator); reader.SetTextPlain(isTextPlain); GXLogging.Debug(log, "isAttachment:" + isAttachment + " isTextHTML:" + isTextHtml + " istextplain:" + isTextPlain); MimeDecoder decoder = GetDecoder(partProps.GetField("Content-Transfer-Encoding")); if (isAttachment) { string outname = string.Empty; try { Stream output; if (this.DownloadAttachments) { string cid = GetAttachmentContentId(partProps); string fileName = qpDecoder.DecodeHeader(RemoveQuotes(partProps.GetKeyProperty("Content-Disposition", "filename"))); if (string.IsNullOrEmpty(fileName)) { fileName = RemoveQuotes(partProps.GetKeyProperty("Content-Type", "name")); } bool inlineAttachment = partProps.GetKeyPrincipal("Content-Disposition").StartsWith("inline"); if (inlineAttachment && !string.IsNullOrEmpty(cid)) { fileName = String.Format("{0}_{1}", cid, fileName); outname = GetFileName(attachmentsPath, fileName, contentType); this.messageHtml = this.messageHtml.Replace("cid:" + cid, outname); } else { outname = GetFileName(attachmentsPath, fileName, contentType); } attachments += outname + ";"; if (!Directory.Exists(attachmentsPath)) { Directory.CreateDirectory(attachmentsPath); } output = new FileStream(attachmentsPath + outname, FileMode.Create); } else { output = new DummyStream(); } try { decoder.DecodeFile(reader, output, runner); } catch (Exception e) { GXLogging.Error(log, "ReadBody error", e); throw e; } finally { output.Close(); } } catch (DirectoryNotFoundException dex) { GXLogging.Error(log, "Error with attachments, attachment path:" + attachmentsPath + outname, dex); throw new CouldNotSaveAttachmentException(dex); } catch (Exception ex) { if (ex.InnerException != null) { ex = ex.InnerException; } GXLogging.Error(log, "Error with attachments, attachment path:" + attachmentsPath + outname, ex); throw new InvalidAttachmentException(ex); } } else { try { TextWriter output = new StringWriter(); try { decoder.DecodeText(reader, output, runner); if (isTextPlain) { messageText.Append(((StringWriter)output).GetStringBuilder().ToString()); } else if (isTextHtml) { messageHtml.Append(((StringWriter)output).GetStringBuilder().ToString()); } } catch (Exception e) { GXLogging.Error(log, "ReadBody error", e); throw e; } finally { output.Close(); } } catch (Exception ex1) { if (ex1.InnerException != null) { ex1 = ex1.InnerException; } GXLogging.Error(log, "ReadBody error", ex1); throw new InvalidMessageException(ex1); } } reader.SetSeparator(oldSeparator); }
public void Decode(MailReader input, TextWriter output, Encoding enc, bool header, GeneXus.Mail.Util.AsyncRunner runner) { string result = DecodeImp(input, enc, header, runner); output.Write(result); }
private void GMailData_BodyLoaded(object sender, EventArgs e) { IMailData MailData = (IMailData)sender; MailData.ReadBody(); MailData.ReadHeader(); // 本文無し( 本文が必要な場合は、false で、reader.MainText ) MailReader reader = new MailReader(MailData.DataStream, false); // UI スレッドへの処理( この場合、post_state は null ) sc.Post((object post_state) => { RowItem item; // ヘッダの一覧より、目的のヘッダを探す foreach (TKMP.Reader.Header.HeaderString headerdata in reader.HeaderCollection) { string from = reader.HeaderCollection.HeaderItem("From").Data; string subject = reader.HeaderCollection.HeaderItem("Subject").Data; string mdate = reader.HeaderCollection.HeaderItem("Date").Data; item = new RowItem { From = from, Subject = subject, MDate = mdate, MText = reader.MainText }; rowItems.Remove(item); rowItems.Insert(0, item); } // 行追加 }, null); // イベント削除 MailData.BodyLoaded -= new EventHandler(GMailData_BodyLoaded); }
private void Receive(GXMailMessage msg) { if (lastReadMessage == count) { throw new NoMessagesException(); } msg.Clear(); char pSep = System.IO.Path.DirectorySeparatorChar; if (!attachDir.EndsWith(pSep.ToString())) { attachDir += pSep.ToString(); } SendNL("RETR " + (++lastReadMessage)); GXLogging.Debug(log, "Receive " + lastReadMessage); MailMessage message = null; try { mailReader = new RFC822Reader(new RFC822EndReader(new SecureNetworkStream(connection))); message = new MailMessage(mailReader, attachDir, readerTimeout); message.DownloadAttachments = this.DownloadAttachments; message.ReadAllMessage(); } catch (InvalidMessageException ime) { #if DEBUG GXLogging.Error(log, "Receive error", ime); #endif throw new GXMailException(ime.Message, GXInternetConstants.MAIL_InvalidValue); } catch (CouldNotSaveAttachmentException dae) { #if DEBUG GXLogging.Error(log, "Receive error", dae); #endif throw new GXMailException(dae.Message, GXInternetConstants.MAIL_CantSaveAttachment); } catch (InvalidAttachmentException iae) { #if DEBUG GXLogging.Error(log, "Receive error", iae); #endif throw new GXMailException(iae.Message, GXInternetConstants.MAIL_InvalidAttachment); } catch (TimeoutExceededException toe) { #if DEBUG GXLogging.Error(log, "Receive error", toe); #endif throw new GXMailException(toe.Message, GXInternetConstants.MAIL_TimeoutExceeded); } catch (Exception exc) { #if DEBUG GXLogging.Error(log, "Receive error", exc); #endif throw new GXMailException(exc.Message, MailConstants.MAIL_ConnectionLost); } try { if (message != null) { GXMailRecipientCollection msgFrom = new GXMailRecipientCollection(); message.SetMessageRecipients(msgFrom, GXInternetConstants.FROM); if (msgFrom.Count > 0) { msg.From = msgFrom.Item(1); } message.SetMessageRecipients(msg.ReplyTo, GXInternetConstants.REPLY_TO); message.SetMessageRecipients(msg.To, GXInternetConstants.TO); message.SetMessageRecipients(msg.CC, GXInternetConstants.CC); msg.Headers = message.Keys; msg.DateSent = message.GetDateSent(); msg.DateReceived = message.GetDateReceived(); msg.Subject = message.GetMessageSubject(); msg.Text = message.GetMessageText(); msg.HTMLText = message.GetMessageHtmlText(); message.SetMessageAttachments(msg.Attachments); } } catch (Exception exc) { GXLogging.Error(log, "Error Receiving", exc); throw new GXMailException(exc.Message, GXInternetConstants.MAIL_InvalidValue); } }
public override void DecodeText(MailReader input, TextWriter output, GeneXus.Mail.Util.AsyncRunner runner) { Decode(input, output, input.GetEncoding(), false, runner); }
public override void DecodeFile(MailReader input, Stream output, GeneXus.Mail.Util.AsyncRunner runner) { Decode(input, output, input.GetEncoding(), false, runner); }
public override void DecodeText(MailReader input, TextWriter output, GeneXus.Mail.Util.AsyncRunner runner) { output.Write(input.GetEncoding().GetString(GetFromBase64String(input, runner))); }