public static string MessageToString(Message message) { using (var stream = new MemoryStream()) { message.Save(stream); stream.Position = 0; var reader = new StreamReader(stream); return reader.ReadToEnd(); } }
//public static void sendEmail(string reciverMail, string senderMail, string subject, string textBody, string mailPassword, string CC) //{ // SmtpClient c = new SmtpClient(@"smtp.live.com", 25); // MailAddress add = new MailAddress(reciverMail); // MailMessage msg = new MailMessage(); // msg.To.Add(add); // msg.From = new MailAddress(senderMail); // msg.IsBodyHtml = true; // msg.Subject = subject; // msg.Body = textBody; // c.Credentials = new System.Net.NetworkCredential(senderMail, mailPassword); // c.EnableSsl = true; // c.Send(msg); //} /// <summary> /// Example showing: /// - how to save a message to a file /// - how to load a message from a file at a later point /// </summary> /// <param name="mail">The message to save and load at a later point</param> /// <returns>The Message, but loaded from the file system</returns> public static Message SaveAndLoadFullMessage(Message mail) { // FileInfo about the location to save/load message FileInfo file = new FileInfo("someFile.eml"); // Save the full message to some file mail.Save(file); // Now load the message again. This could be done at a later point Message loadedMessage = Message.Load(file); // use the message again return loadedMessage; }
public void TestFileOverwriteTruncatesFileCorrectly() { const string longMessage = "Content-Type: text/plain\r\n" + "Content-Disposition: attachment\r\n" + "\r\n" + "Testing very long..."; const string smallMessage = "Content-Type: text/plain\r\n" + "Content-Disposition: attachment\r\n" + "\r\n" + "Testing"; const string filename = "test_message_part_save_truncate.testFile"; FileInfo longTestFile = new FileInfo(filename); MessagePart messageLong = new Message(Encoding.ASCII.GetBytes(longMessage)).MessagePart; messageLong.Save(longTestFile); long longFileSize = longTestFile.Length; FileInfo smallTestFile = new FileInfo(filename); MessagePart messageSmall = new Message(Encoding.ASCII.GetBytes(smallMessage)).MessagePart; messageSmall.Save(smallTestFile); long smallFileSize = smallTestFile.Length; smallTestFile.Delete(); Assert.AreNotEqual(longFileSize, smallFileSize); }
void addMessage(Message message, bool unread, int group, int image) { //Add the messages to the chosen directory messages.Add(message.Headers.MessageId, message); ListViewItem newItem = new ListViewItem(); newItem.Text = message.Headers.From.ToString(); newItem.SubItems.Add(message.Headers.Subject); newItem.SubItems.Add(message.Headers.DateSent.ToString("yyyy-MM-dd HH:mm:ss")); newItem.Tag = message.Headers.MessageId; newItem.Group = mailView.Groups[group]; newItem.ImageIndex = image; if (unread) { newItem.Font = new Font(newItem.Font, FontStyle.Bold); } AddMailToView(newItem); if (!File.Exists(Application.UserAppDataPath + "\\Inbox\\" + message.Headers.DateSent.ToString("yyyyMMddHHmmssfffffff") + ".eml")) { message.Save(new FileInfo(Application.UserAppDataPath + "\\Inbox\\" + message.Headers.DateSent.ToString("yyyyMMddHHmmssfffffff") + ".eml")); } }
public void TestFileOverwriteTruncatesFileCorrectly() { const string longMessage = "Content-Type: text/plain\r\n" + "Content-Disposition: attachment\r\n" + "\r\n" + "Testing very long..."; const string smallMessage = "Content-Type: text/plain\r\n" + "Content-Disposition: attachment\r\n" + "\r\n" + "Testing"; FileInfo testFile = new FileInfo("test_message_save_truncate.testFile"); Message messageLong = new Message(Encoding.ASCII.GetBytes(longMessage)); messageLong.Save(testFile); Message messageSmall = new Message(Encoding.ASCII.GetBytes(smallMessage)); messageSmall.Save(testFile); Message messageLoaded = Message.Load(testFile); testFile.Delete(); // We should now have the small message in the file. // IE We should have overwritten the longMessage file with our shortMessage file. Assert.AreEqual(messageSmall.RawMessage, messageLoaded.RawMessage); }
public void TestSaveAndLoadComplex() { const string rfcExample = "From: Nathaniel Borenstein <*****@*****.**>\r\n" + "To: Ned Freed <*****@*****.**>\r\n" + "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\r\n" + "Subject: Sample message\r\n" + "MIME-Version: 1.0\r\n" + "Content-type: multipart/mixed; boundary=\"simple boundary\"\r\n" + "\r\n" + "--simple boundary\r\n" + "Content-type: multipart/mixed; boundary=\"anotherBoundary\"\r\n" + "\r\n" + "--anotherBoundary\r\n" + "\r\n" + "TEXT\r\n" + "--anotherBoundary\r\n" + "Content-Type: text/html; charset=us-ascii\r\n" + "\r\n" + "HTML\r\n" + "--anotherBoundary--\r\n" + "--simple boundary\r\n" + "Content-type: text/html; charset=ISO-8859-1\r\n" + "\r\n" + "MORE HTML\r\n" + "--simple boundary--"; Message message = new Message(Encoding.ASCII.GetBytes(rfcExample)); { System.Collections.Generic.List<MessagePart> parts = message.FindAllMessagePartsWithMediaType("multipart/mixed"); Assert.NotNull(parts); Assert.IsNotEmpty(parts); Assert.AreEqual(2, parts.Count); MessagePart firstPart = parts[0]; Assert.IsTrue(firstPart.IsMultiPart); Assert.AreEqual("multipart/mixed", firstPart.ContentType.MediaType); Assert.AreEqual("simple boundary", firstPart.ContentType.Boundary); MessagePart secondPart = parts[1]; Assert.IsTrue(secondPart.IsMultiPart); Assert.AreEqual("multipart/mixed", secondPart.ContentType.MediaType); Assert.AreEqual("anotherBoundary", secondPart.ContentType.Boundary); } FileInfo testFile = new FileInfo("test_message_save_.testFile"); message.Save(testFile); Message message2 = Message.Load(testFile); { System.Collections.Generic.List<MessagePart> parts2 = message2.FindAllMessagePartsWithMediaType("multipart/mixed"); Assert.NotNull(parts2); Assert.IsNotEmpty(parts2); Assert.AreEqual(2, parts2.Count); MessagePart firstPart2 = parts2[0]; Assert.IsTrue(firstPart2.IsMultiPart); Assert.AreEqual("multipart/mixed", firstPart2.ContentType.MediaType); Assert.AreEqual("simple boundary", firstPart2.ContentType.Boundary); MessagePart secondPart2 = parts2[1]; Assert.IsTrue(secondPart2.IsMultiPart); Assert.AreEqual("multipart/mixed", secondPart2.ContentType.MediaType); Assert.AreEqual("anotherBoundary", secondPart2.ContentType.Boundary); } testFile.Delete(); }
public void TestSaveAndLoad() { const string messageString = "Content-Type: text/plain\r\n" + "Content-Disposition: attachment\r\n" + "\r\n" + "Testing"; Message message = new Message(Encoding.ASCII.GetBytes(messageString)); FileInfo testFile = new FileInfo("test_message_save_.testFile"); message.Save(testFile); Message message2 = Message.Load(testFile); Assert.AreEqual("Testing", message.MessagePart.GetBodyAsText()); Assert.AreEqual("Testing", message2.MessagePart.GetBodyAsText()); Assert.AreEqual("text/plain", message.Headers.ContentType.MediaType); Assert.AreEqual("text/plain", message2.Headers.ContentType.MediaType); Assert.IsFalse(message.Headers.ContentDisposition.Inline); Assert.IsFalse(message2.Headers.ContentDisposition.Inline); testFile.Delete(); }