Example #1
0
        public void AddLinkedResourceManually()
        {
            const string myContentId  = "my.content.id";
            var          filesAbsPath = Path.Combine(Helper.GetCodeBaseDirectory(), _pathRelativeToCodebase);

            var dataItem = new
            {
                Name        = "John",
                MailboxAddr = "*****@*****.**",
                Success     = true,
                Date        = DateTime.Now,
                SenderAddr  = "*****@*****.**"
            };

            var mmm = new MailMergeMessage
            {
                HtmlText  = $"<html><body><img src=\"cid:{myContentId}\" width=\"100\"><br/>only an image</body></html>",
                PlainText = "only an image",
                Subject   = "Message subject",
                Config    = { FileBaseDirectory = filesAbsPath }
            };

            mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, "{SenderAddr}"));
            mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "\"{Name}\" <{MailboxAddr}>", mmm.Config.CharacterEncoding));
            mmm.AddExternalInlineAttachment(new FileAttachment(Path.Combine(filesAbsPath, "success.jpg"), myContentId));

            var msg = mmm.GetMimeMessage(dataItem);

            Assert.IsTrue(msg.BodyParts.Any(bp => bp.ContentDisposition?.Disposition == ContentDisposition.Inline && bp.ContentType.IsMimeType("image", "jpeg") && bp.ContentId == myContentId));
        }
Example #2
0
        public void MissingVariableAndAttachmentsExceptions()
        {
            // build message with a total of 8 placeholders which will be missing
            var mmm = new MailMergeMessage("Missing in subject {subject}", "Missing in plain text {plain}",
                                           "<html><head></head><body>{html}</body></html>");

            mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, "{from.address}"));
            mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "{to.address}"));
            mmm.AddExternalInlineAttachment(new FileAttachment("{inlineAtt.filename}.jpg", string.Empty));
            mmm.FileAttachments.Add(new FileAttachment("{fileAtt.filename}.xml", "{fileAtt.displayname}"));

            try
            {
                var mimeMessage = mmm.GetMimeMessage(default(object));
                Assert.Fail("Expected exceptions not thrown.");
            }
            catch (MailMergeMessage.MailMergeMessageException exceptions)
            {
                Console.WriteLine($"Aggregate {nameof(MailMergeMessage.MailMergeMessageException)} thrown. Passed.");
                Console.WriteLine();

                /* Expected exceptions:
                 * 1) 8 missing variables for {placeholders}
                 * 2) No recipients
                 * 3) No FROM address
                 * 4) Missing file attachment {fileAtt.filename}.xml
                 * 5) Missing inline attachment {inlineAtt.filename}.jpg
                 */
                Assert.That(exceptions.InnerExceptions.Count == 5);

                foreach (var ex in exceptions.InnerExceptions.Where(ex => !(ex is MailMergeMessage.AttachmentException)))
                {
                    if (ex is MailMergeMessage.VariableException)
                    {
                        Assert.AreEqual(8, (ex as MailMergeMessage.VariableException).MissingVariable.Count);
                        Console.WriteLine($"{nameof(MailMergeMessage.VariableException)} thrown successfully:");
                        Console.WriteLine("Missing variables: " +
                                          string.Join(", ", (ex as MailMergeMessage.VariableException).MissingVariable));
                    }
                    if (ex is MailMergeMessage.AddressException)
                    {
                        Console.WriteLine($"{nameof(MailMergeMessage.AddressException)} thrown successfully:");
                        Console.WriteLine((ex as MailMergeMessage.AddressException).Message);
                        Assert.That((ex as MailMergeMessage.AddressException).Message == "No recipients." ||
                                    (ex as MailMergeMessage.AddressException).Message == "No from address.");
                    }
                }

                // one exception for a missing file attachment, one for a missing inline attachment
                var attExceptions = exceptions.InnerExceptions.Where(ex => ex is MailMergeMessage.AttachmentException).ToList();
                Assert.AreEqual(2, attExceptions.Count);
                foreach (var ex in attExceptions)
                {
                    Console.WriteLine($"{nameof(MailMergeMessage.AttachmentException)} thrown successfully:");
                    Console.WriteLine("Missing files: " + string.Join(", ", (ex as MailMergeMessage.AttachmentException).BadAttachment));
                    Assert.AreEqual(1, (ex as MailMergeMessage.AttachmentException).BadAttachment.Count);
                }
            }
        }
        public static MailMergeMessage GetHtmlMsgWithManualLinkedResources()
        {
            var mmm = new MailMergeMessage
            {
                HtmlText  = $"<html><body><img src=\"cid:{MyContentId}\" width=\"100\"><br/>only an image</body></html>",
                PlainText = "only an image",
                Subject   = "Message subject",
                Config    = { FileBaseDirectory = TestFileFolders.FilesAbsPath }
            };

            mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, "{SenderAddr}"));
            mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "\"{Name}\" <{MailboxAddr}>", mmm.Config.CharacterEncoding));
            mmm.AddExternalInlineAttachment(new FileAttachment(Path.GetFullPath(Path.Combine(TestFileFolders.FilesAbsPath, ImgSuccess)), MyContentId));

            return(mmm);
        }
Example #4
0
        public void MissingVariableAndAttachmentsExceptions()
        {
            // build message with a total of 9 placeholders which will be missing
            var mmm = new MailMergeMessage("Missing in subject {subject}", "Missing in plain text {plain}",
                                           "<html><head></head><body>{html}{:template(Missing)}</body></html>");

            mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, "{from.address}"));
            mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "{to.address}"));
            mmm.AddExternalInlineAttachment(new FileAttachment("{inlineAtt.filename}.jpg", string.Empty));
            mmm.FileAttachments.Add(new FileAttachment("{fileAtt.filename}.xml", "{fileAtt.displayname}"));
            mmm.FileAttachments.Add(new FileAttachment("{throwParingError.xml", "DisplayName"));

            // **************** Part 1:
            mmm.Config.IgnoreMissingInlineAttachments = false;
            mmm.Config.IgnoreMissingFileAttachments   = false;
            // ************************

            try
            {
                mmm.GetMimeMessage(default);
Example #5
0
        public void MissingVariableAndAttachmentsExceptions()
        {
            // build message with a total of 9 placeholders which will be missing
            var mmm = new MailMergeMessage("Missing in subject {subject}", "Missing in plain text {plain}",
                                           "<html><head></head><body>{html}{:template(Missing)}</body></html>");

            mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, "{from.address}"));
            mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "{to.address}"));
            mmm.AddExternalInlineAttachment(new FileAttachment("{inlineAtt.filename}.jpg", string.Empty));
            mmm.FileAttachments.Add(new FileAttachment("{fileAtt.filename}.xml", "{fileAtt.displayname}"));
            mmm.FileAttachments.Add(new FileAttachment("{throwParingError.xml", "DisplayName"));

            // **************** Part 1:
            mmm.Config.IgnoreMissingInlineAttachments = false;
            mmm.Config.IgnoreMissingFileAttachments   = false;
            // ************************

            try
            {
                mmm.GetMimeMessage(default(object));
                Assert.Fail("Expected exceptions not thrown.");
            }
            catch (MailMergeMessage.MailMergeMessageException exceptions)
            {
                Console.WriteLine($"Aggregate {nameof(MailMergeMessage.MailMergeMessageException)} thrown.");
                Console.WriteLine();

                /* Expected exceptions:
                 * 1) 9 missing variables for {placeholders} and {:templates(...)}
                 * 2) No recipients
                 * 3) No FROM address
                 * 4) Missing file attachment {fileAtt.filename}.xml
                 * 5) Missing inline attachment {inlineAtt.filename}.jpg
                 * 6) Parsing error in file attachment "{throwParingError.xml"
                 */
                Assert.AreEqual(6, exceptions.InnerExceptions.Count);

                foreach (var ex in exceptions.InnerExceptions.Where(ex => !(ex is MailMergeMessage
                                                                            .AttachmentException)))
                {
                    if (ex is MailMergeMessage.VariableException)
                    {
                        Assert.AreEqual(9, (ex as MailMergeMessage.VariableException).MissingVariable.Count);
                        Console.WriteLine($"{nameof(MailMergeMessage.VariableException)} thrown successfully:");
                        Console.WriteLine("Missing variables: " +
                                          string.Join(", ",
                                                      (ex as MailMergeMessage.VariableException).MissingVariable));
                        Console.WriteLine();
                    }
                    if (ex is MailMergeMessage.AddressException)
                    {
                        Console.WriteLine($"{nameof(MailMergeMessage.AddressException)} thrown successfully:");
                        Console.WriteLine((ex as MailMergeMessage.AddressException).Message);
                        Console.WriteLine();
                        Assert.That((ex as MailMergeMessage.AddressException).Message == "No recipients." ||
                                    (ex as MailMergeMessage.AddressException).Message == "No from address.");
                    }
                }

                // one exception for a missing file attachment, one for a missing inline attachment
                var attExceptions = exceptions.InnerExceptions.Where(ex => ex is MailMergeMessage.AttachmentException)
                                    .ToList();
                Assert.AreEqual(2, attExceptions.Count);

                // Inline file missing
                Console.WriteLine($"{nameof(MailMergeMessage.AttachmentException)} thrown successfully:");
                Console.WriteLine("Missing inline attachment files: " +
                                  string.Join(", ", (attExceptions[0] as MailMergeMessage.AttachmentException).BadAttachment));
                Console.WriteLine();
                Assert.AreEqual(1, (attExceptions[0] as MailMergeMessage.AttachmentException).BadAttachment.Count);

                // 2 file attachments missing
                Console.WriteLine($"{nameof(MailMergeMessage.AttachmentException)} thrown successfully:");
                Console.WriteLine("Missing attachment files: " +
                                  string.Join(", ", (attExceptions[1] as MailMergeMessage.AttachmentException).BadAttachment));
                Console.WriteLine();
                Assert.AreEqual(2, (attExceptions[1] as MailMergeMessage.AttachmentException).BadAttachment.Count);
            }

            // **************** Part 2:
            mmm.Config.IgnoreMissingInlineAttachments = true;
            mmm.Config.IgnoreMissingFileAttachments   = true;
            // ************************

            try
            {
                mmm.GetMimeMessage(default(object));
                Assert.Fail("Expected exceptions not thrown.");
            }
            catch (MailMergeMessage.MailMergeMessageException exceptions)
            {
                /* Expected exceptions:
                 * 1) 9 missing variables for {placeholders} and {:templates(...)}
                 * 2) No recipients
                 * 3) No FROM address
                 * 4) 1 parsing error
                 */
                Assert.AreEqual(4, exceptions.InnerExceptions.Count);
                Assert.IsFalse(exceptions.InnerExceptions.Any(e => e is MailMergeMessage.AttachmentException));

                Console.WriteLine("Exceptions for missing attachment files suppressed.");
            }
        }