コード例 #1
0
        public void linked_resources_included_in_html_body()
        {
            // Arrange
            var mailer    = new RegistrationMailer(smtpConfiguration);
            var addresses = new List <MailboxAddress>();

            addresses.Add(new MailboxAddress("Kinger", "*****@*****.**"));
            var guid = "18c1c20f-81f0-4308-8fd8-f3c65a276882";

            List <LinkedResource> linkedResources = null;

            linkedResources = new List <LinkedResource>();
            linkedResources.Add(new LinkedResource {
                Name = "logoCid", Cid = "techwizLogo", Path = @"C:\Users\king\source\repos\KahanuMailer\ConsoleApp1\Views\RegistrationMailer\techwiz-logo.png"
            });
            linkedResources.Add(new LinkedResource {
                Name = "parakeetCid", Cid = "parakeet", Path = @"C:\Users\king\source\repos\KahanuMailer\ConsoleApp1\Views\RegistrationMailer\parakeet.png"
            });

            var ctx = new RegistrationMailerContext
            {
                Now             = "7/13/2020 4:05:49 PM",
                FirstName       = "Mike",
                LastName        = "Jones",
                CustomerMessage = "This is the customer message.",
                Subject         = "This is the customer subject",
                ToAddresses     = addresses,
                Guid            = guid
            };

            if (linkedResources != null)
            {
                ctx.LinkedResources = linkedResources;
            }

            // Act
            var actual       = mailer.Customer(ctx);
            var expectedBody = @"<!DOCTYPE html>"
                               + Environment.NewLine + "<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">"
                               + Environment.NewLine + "<head>"
                               + Environment.NewLine + "    <meta charset=\"utf-8\" />"
                               + Environment.NewLine + "    <title></title>"
                               + Environment.NewLine + "    <style>"
                               + Environment.NewLine + "        .header {"
                               + Environment.NewLine + "            padding: 10px;"
                               + Environment.NewLine + "            background-color: #f7d736;"
                               + Environment.NewLine + "            margin-bottom: 20px;"
                               + Environment.NewLine + "            width: 100%;"
                               + Environment.NewLine + "        }"
                               + Environment.NewLine + ""
                               + Environment.NewLine + "        .footer {"
                               + Environment.NewLine + "            padding: 10px;"
                               + Environment.NewLine + "            background-color: #f7f7f7;"
                               + Environment.NewLine + "            margin-top: 20px;"
                               + Environment.NewLine + "            width: 100%;"
                               + Environment.NewLine + "        }"
                               + Environment.NewLine + "    </style>"
                               + Environment.NewLine + "</head>"
                               + Environment.NewLine + "<body>"
                               + Environment.NewLine + "    <div class=\"header\">"
                               + Environment.NewLine + "        <img src=\"cid:techwizLogo\" />"
                               + Environment.NewLine + "    </div>"
                               + Environment.NewLine + "<p>7/13/2020 4:05:49 PM</p>"
                               + Environment.NewLine + "<p>Mike Jones,</p>"
                               + Environment.NewLine + "This is the customer message."
                               + Environment.NewLine + "<p>My bird: <img src='cid:parakeet' /></p>"
                               + Environment.NewLine + "<p>Link: <a href=\"somepage/18c1c20f-81f0-4308-8fd8-f3c65a276882\">Link with Guid</a></p>"
                               + Environment.NewLine + "<p>Thanks, TechWiz Support.</p>"
                               + Environment.NewLine + "    <div class=\"footer\">"
                               + Environment.NewLine + "        &copy;2020 Tech - All rights reserved."
                               + Environment.NewLine + "    </div>"
                               + Environment.NewLine + "</body>"
                               + Environment.NewLine + "</html>";

            // assert
            actual.HtmlBody.Should().Be(expectedBody);
        }
コード例 #2
0
        public async Task Run()
        {
            var toAddresses = new List <MailboxAddress>();

            toAddresses.Add(new MailboxAddress("Recipient Name", "*****@*****.**"));

            var from = new List <MailboxAddress>();

            from.Add(new MailboxAddress("From Name", "*****@*****.**"));


            // Begin Attachments code
            var path            = @"C:\Users\king\Pictures\Golf Courses\golfcourse.jpg";
            var mimeType        = MimeTypes.GetMimeType(path);
            var contentType     = ContentType.Parse(mimeType);
            var imageAttachment = new MimePart(contentType)
            {
                Content                 = new MimeContent(File.OpenRead(path), ContentEncoding.Default),
                ContentDisposition      = new ContentDisposition(ContentDisposition.Attachment),
                ContentTransferEncoding = ContentEncoding.Base64,
                FileName                = Path.GetFileName(path)
            };

            var pdfPath = @"C:\Users\king\Documents\some.pdf";

            mimeType    = MimeTypes.GetMimeType(pdfPath);
            contentType = ContentType.Parse(mimeType);
            var pdfAttachment = new MimePart(contentType)
            {
                Content                 = new MimeContent(File.OpenRead(pdfPath), ContentEncoding.Default),
                ContentDisposition      = new ContentDisposition(ContentDisposition.Attachment),
                ContentTransferEncoding = ContentEncoding.Base64,
                FileName                = Path.GetFileName(pdfPath)
            };

            List <MimeEntity> attachments = null;

            attachments = new List <MimeEntity>();
            attachments.Add(imageAttachment);
            attachments.Add(pdfAttachment);

            List <LinkedResource> linkedResources = null;

            linkedResources = new List <LinkedResource>();
            linkedResources.Add(new LinkedResource {
                Name = "logoCid", Cid = "techwizLogo", Path = @"C:\Users\king\source\repos\KahanuMailer\ConsoleApp1\Views\RegistrationMailer\techwiz-logo.png"
            });
            linkedResources.Add(new LinkedResource {
                Name = "parakeetCid", Cid = "parakeet", Path = @"C:\Users\king\source\repos\KahanuMailer\ConsoleApp1\Views\RegistrationMailer\parakeet.png"
            });

            var context = new RegistrationMailerContext
            {
                Now             = DateTime.Now.ToString(),
                FirstName       = "John",
                LastName        = "Doh",
                CustomerMessage = @"<p>Thank you for being a loyal customer.</p>
                    <p>Your next visit will get 20% off your purchase.</p>",
                Subject         = "Test from TechWiz",
                ToAddresses     = toAddresses,
                From            = from
            };

            if (linkedResources != null)
            {
                context.LinkedResources = linkedResources;
            }

            if (attachments != null)
            {
                context.AttachmentCollection = attachments;
            }

            try
            {
                await mailer.Customer(context).SendAsync().ConfigureAwait(false);

                Console.WriteLine("Sent email successfully!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error sending email: {0}", ex.Message);
            }
            Console.ReadLine();
        }