public void TestSendingMailWithAlternateView() { var address = new MailAddress("*****@*****.**", "Test User", Encoding.UTF8); var msg = new MailMessage(address, address) { Subject = "text subject", Body = "plain text body" }; var htmlView = AlternateView.CreateAlternateViewFromString( "<h1><img src=\"/wp-content/themes/ES/images/logo-ES.png\" /><img src=\"cid:companyLogo\" />html body</h1>"); htmlView.ContentType = new ContentType("text/html"); htmlView.BaseUri = new Uri("https://microsoft.com"); var imgResource = new LinkedResource(new MemoryStream(Convert.FromBase64String(ImageData)), "image/png") { ContentId = "companyLogo", ContentType = new ContentType("image/png"), }; htmlView.LinkedResources.Add(imgResource); msg.AlternateViews.Add(htmlView); byte[] serialized = MailMessageBinarySerializer.Serialize(msg); using (var client = new SmtpClient("10.10.104.138", 25)) using (var msg2 = MailMessageBinarySerializer.Deserialize(serialized)) { client.Send(msg2); } }
public void TestSerialization() { var msg = new MailMessage( new MailAddress("*****@*****.**", "Address1"), new MailAddress("*****@*****.**", "Address2")) { Subject = "subject sucbejct", Body = "Message Body", IsBodyHtml = false, Priority = MailPriority.High, DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.Delay }; msg.CC.Add(new MailAddress("*****@*****.**", "Address3")); msg.Bcc.Add(new MailAddress("*****@*****.**")); msg.ReplyToList.Add("*****@*****.**"); msg.ReplyToList.Add("*****@*****.**"); msg.Headers.Add("foo", "bar"); msg.Headers.Add("foo", "baz"); msg.HeadersEncoding = Encoding.UTF8; byte[] serialized = MailMessageBinarySerializer.Serialize(msg); Debug.WriteLine(serialized.Length); Debug.WriteLine(TestHelper.ToHex(serialized)); Debug.WriteLine(Encoding.UTF8.GetString(serialized)); MailMessage msg2 = MailMessageBinarySerializer.Deserialize(serialized); Assert.AreEqual(msg.Subject, msg2.Subject); Assert.AreEqual(msg.SubjectEncoding, msg2.SubjectEncoding); Assert.AreEqual(msg.Body, msg2.Body); Assert.AreEqual(msg.IsBodyHtml, msg2.IsBodyHtml); Assert.AreEqual(msg.BodyEncoding, msg2.BodyEncoding); Assert.AreEqual(msg.BodyTransferEncoding, msg2.BodyTransferEncoding); Assert.AreEqual(msg.Priority, msg2.Priority); Assert.AreEqual(msg.DeliveryNotificationOptions, msg2.DeliveryNotificationOptions); Assert.IsTrue(msg.Headers.AllKeys.SequenceEqual(msg2.Headers.AllKeys)); Assert.IsTrue(msg.Headers.AllKeys.SelectMany(msg.Headers.GetValues) .SequenceEqual(msg2.Headers.AllKeys.SelectMany(msg2.Headers.GetValues))); Assert.AreEqual(msg.HeadersEncoding, msg2.HeadersEncoding); Assert.AreEqual(msg.From, msg2.From); Assert.AreEqual(msg.Sender, msg2.Sender); Assert.IsTrue(msg.To.SequenceEqual(msg2.To)); Assert.IsTrue(msg.CC.SequenceEqual(msg2.CC)); Assert.IsTrue(msg.Bcc.SequenceEqual(msg2.Bcc)); Assert.IsTrue(msg.ReplyToList.SequenceEqual(msg2.ReplyToList)); }
public void TestConcreteMessageDeserialization() { const string data = "AD534B6FD340107604A74AF90BC8CD193B8F0605A21081102DA84A704250738BF2589434260E7EA48453DA222AD40810EA8D03D07245B242DD3A0FBB7F61F62F70E347A032BB4EA422B53776B5DA9D996F7666BE9DBD169763028E361C8243B7E90EF830A2FB3016E10B1EA77408A73745F80436CC4438A30370E0187C8EB4610453943D71A1A2BB7026C20481280F38644A77C5087CC3790847327C85EF91F3EBE1253842671B4E7039300B025E9DC172A6A667D1E9BFA621CF2FFD88E17618105C38059BBEC5DD070F3C4CC26177DA0B213DF7F88CF69F88F2444549C6A4D59814974AC430B55EBFA5CC310718F203CF80C5A67B2C417003958DB1FC85DDE1C58C318E8B6938DC45A4DBBCA463F47C4FDF313BAA70EEA37A82A05F8383F052A62A3675F2FC6EA4699ADD7434AA6AF5AADAD40C337D6B25753B152D1283984AD530B634BD117DA4BF2C561EE6DAB9D5427F4D7D5A8A6FA89B957CAF55AB6FDE29D69EBC262BA9C72F8A85C45A522A747B6D3545483DB7B55ED69BC57C57274AF981954FD43B25E9FE8644D6CBC9C2B35C3E5E4844B2176AC40AE8F06285C34CB49A0D1899F3F28371CD202E963384192F96BEE1FCCF50C9FC469C7244D9C1D3A0C1C6DD652C22331370E5A0394E82978571D008D8A81E63D0476184D03D348F03523DB0B911DFCD9743A165C330AD4A83F42A9CB47BE45597E8A6A1A996D9D23A86AC5BE17F9A4508E11FB9D1E8563B7DCB68B65B9D4B3C84CBC6398EDF7F04812D765EE8FF02"; MailMessage msg = MailMessageBinarySerializer.Deserialize(TestHelper.FromHex(data)); Debug.Write(msg.Body); Debug.WriteLine(""); string actual = TestHelper.ToHex(MailMessageBinarySerializer.Serialize(msg)); Assert.AreEqual(data, actual); }
public void TestEmptySerialization() { var msg = new MailMessage(); byte[] serialized = MailMessageBinarySerializer.Serialize(msg); Debug.WriteLine(serialized.Length); Debug.WriteLine(TestHelper.ToHex(serialized)); Debug.WriteLine(Encoding.UTF8.GetString(serialized)); using (var msg2 = MailMessageBinarySerializer.Deserialize(serialized)) { Assert.IsNotNull(msg2); } }
public void SendStoredMessage(string filePath) { byte[] serializedMsg = File.ReadAllBytes(filePath); MailMessage msg = MailMessageBinarySerializer.Deserialize(serializedMsg); using (var client = new SmtpClient("mysmtphost") { DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory, PickupDirectoryLocation = Path.GetTempPath(), }) { client.Send(msg); } }
public void TestAttachmentSerialization() { var stream = new DisposableStream(Encoding.UTF8.GetBytes("test test test"), false); var attachment = new Attachment(stream, "file.txt", "text/plain"); attachment.ContentDisposition.FileName = "file.txt"; attachment.Name = null; var msg = new MailMessage(); msg.Attachments.Add(attachment); Debug.WriteLine(TestHelper.GetStreamString(attachment.ContentStream, Encoding.UTF8)); Debug.WriteLine(attachment.ContentType.ToString()); Debug.WriteLine(attachment.ContentDisposition.ToString()); Assert.IsFalse(stream.Disposed); byte[] serialized = MailMessageBinarySerializer.Serialize(msg); Assert.IsTrue(stream.Disposed); using (var msg2 = MailMessageBinarySerializer.Deserialize(serialized)) { Assert.IsNotNull(msg2); Assert.AreEqual(1, msg2.Attachments.Count); var attachment2 = msg2.Attachments[0]; Assert.IsNotNull(attachment2); Assert.AreNotSame(attachment, attachment2); string data = Encoding.UTF8.GetString(((MemoryStream)attachment2.ContentStream).ToArray()); Assert.AreEqual("test test test", data); Assert.AreNotSame(attachment.ContentType, attachment2.ContentType); Assert.AreEqual(attachment.ContentType.MediaType, attachment2.ContentType.MediaType); Assert.IsTrue(TestHelper.DictionariesAreEqual( attachment.ContentType.Parameters, attachment2.ContentType.Parameters)); Assert.AreNotSame(attachment.ContentDisposition, attachment2.ContentDisposition); Assert.AreEqual(attachment.ContentDisposition, attachment2.ContentDisposition); Debug.WriteLine(TestHelper.GetStreamString(attachment2.ContentStream, Encoding.UTF8)); Debug.WriteLine(attachment2.ContentType.ToString()); Debug.WriteLine(attachment2.ContentDisposition.ToString()); } }
public void TestAlternateViewSerialization() { var htmlView = AlternateView.CreateAlternateViewFromString("<p>html body</p>"); htmlView.ContentType = new ContentType("text/html"); htmlView.BaseUri = new Uri("https://microsoft.com"); var imgResource = new LinkedResource(new MemoryStream(Convert.FromBase64String(ImageData)), "image/png") { ContentId = null, ContentType = new ContentType("image/png"), }; htmlView.LinkedResources.Add(imgResource); var msg = new MailMessage("from@from", "to@to", "subject", "plain text body"); msg.AlternateViews.Add(htmlView); msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("<div>html body 2</div>")); Debug.WriteLine(TestHelper.GetStreamString(htmlView.ContentStream, Encoding.UTF8)); Debug.WriteLine(htmlView.BaseUri.ToString()); byte[] serialized = MailMessageBinarySerializer.Serialize(msg); using (var msg2 = MailMessageBinarySerializer.Deserialize(serialized)) { Assert.IsNotNull(msg2); Assert.AreEqual(2, msg2.AlternateViews.Count); var alternateView = msg2.AlternateViews[0]; Assert.IsNotNull(alternateView); Assert.AreNotSame(htmlView, alternateView); string data = Encoding.UTF8.GetString(((MemoryStream)alternateView.ContentStream).ToArray()); Assert.AreEqual("<p>html body</p>", data); Assert.AreNotSame(htmlView.BaseUri, alternateView.BaseUri); Assert.AreEqual(htmlView.BaseUri, alternateView.BaseUri); Debug.WriteLine(TestHelper.GetStreamString(alternateView.ContentStream, Encoding.UTF8)); Debug.WriteLine(alternateView.BaseUri.ToString()); } }
public void StoreMessage(string filePath) { MailMessage msg = new MailMessage( new MailAddress("*****@*****.**", "Address1"), new MailAddress("*****@*****.**", "Address2")) { Subject = "subject sucbejct", Body = "Message Body", IsBodyHtml = false, Priority = MailPriority.High, }; msg.CC.Add(new MailAddress("*****@*****.**", "Address3")); msg.Bcc.Add(new MailAddress("*****@*****.**")); msg.ReplyToList.Add("*****@*****.**"); byte[] serializedMsg = MailMessageBinarySerializer.Serialize(msg); File.WriteAllBytes(filePath, serializedMsg); }
public void TestSendingMailWithAttachment() { var address = new MailAddress("*****@*****.**", "Test User", Encoding.UTF8); var msg = new MailMessage(address, address) { IsBodyHtml = true }; var attachment = Attachment.CreateAttachmentFromString("test test test", "file.txt"); attachment.ContentDisposition.FileName = "file.txt"; msg.Body = "body body body"; msg.Attachments.Add(attachment); byte[] serialized = MailMessageBinarySerializer.Serialize(msg); using (var client = new SmtpClient("10.10.104.138", 25)) using (var msg2 = MailMessageBinarySerializer.Deserialize(serialized)) { client.Send(msg2); } }
public virtual async Task <bool> TrySend(byte[] serializedMessage) { MailMessage message; try { message = MailMessageBinarySerializer.Deserialize(serializedMessage); List <MailValidationError> errors = message.ValidateAddresses().ToList(); if (errors.Any()) { _logger.Error("Email has invalid format " + String.Join(", ", errors)); return(false); } } catch (Exception exception) { _logger.Error(exception, "Email has invalid binary format"); return(false); } return(await TrySend(message)); }