Ejemplo n.º 1
0
        /// <summary>
        /// Example for showing friendly names in an email
        /// </summary>
        /// <param name="pConfig"></param>
        /// <param name="identifier"></param>
        /// <param name="pFromAddress"></param>
        /// <param name="pToAddress"></param>
        /// <param name="name">Method name as default with an empty string</param>
        public void ExampleSendMaskNames(string pConfig, int identifier, MailFriendly pFromAddress, MailFriendly pToAddress, [CallerMemberName] string name = "")
        {
            var ops  = new DataOperations();
            var data = ops.Read(identifier);

            var mc   = new MailConfiguration(pConfig);
            var mail = new MailMessage
            {
                From    = CreateFriendltAddress(pFromAddress),
                Subject = $"Sent from test: '{name}'"
            };

            mail.To.Add(CreateFriendltAddress(pToAddress));

            var plainMessage = AlternateView.CreateAlternateViewFromString(
                data.TextMessage,
                null, "text/plain");

            var htmlMessage = AlternateView.CreateAlternateViewFromString(
                data.HtmlMessage,
                null, "text/html");

            mail.IsBodyHtml = true;

            mail.AlternateViews.Add(plainMessage);
            mail.AlternateViews.Add(htmlMessage);

            using (var smtp = new SmtpClient(mc.Host, mc.Port))
            {
                smtp.Credentials = new NetworkCredential(mc.UserName, mc.Password);
                smtp.EnableSsl   = mc.EnableSsl;
                smtp.Send(mail);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Example for showing friendly names in an email
        /// </summary>
        /// <param name="pConfig"></param>
        /// <param name="identifier"></param>
        /// <param name="pFromAddress"></param>
        /// <param name="pToAddress"></param>
        /// <param name="userPickupFolder">Toggle between sending live or sending to file</param>
        /// <param name="name">Method name as default with an empty string</param>
        public void UsePickupFolderExample(string pConfig, int identifier, MailFriendly pFromAddress, MailFriendly pToAddress, bool userPickupFolder = true, [CallerMemberName] string name = "")
        {
            var ops  = new DataOperations();
            var data = ops.Read(identifier);

            var mc   = new MailConfiguration(pConfig);
            var mail = new MailMessage
            {
                From    = CreateFriendltAddress(pFromAddress),
                Subject = $"Sent from test: '{name}'"
            };

            mail.To.Add(CreateFriendltAddress(pToAddress));
            mail.IsBodyHtml = true;

            mail.AlternateViews.PlainTextView(data.TextMessage);
            mail.AlternateViews.HTmlView(data.HtmlMessage);

            using (var smtp = new SmtpClient(mc.Host, mc.Port))
            {
                smtp.Credentials = new NetworkCredential(mc.UserName, mc.Password);

                if (userPickupFolder)
                {
                    smtp.DeliveryMethod          = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                    smtp.PickupDirectoryLocation = mc.PickupFolder;
                }

                smtp.EnableSsl = !userPickupFolder;
                smtp.Send(mail);
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Helper method to create a MailAddress with friendly name for FROM or TO address
 /// </summary>
 /// <param name="sender"></param>
 /// <returns>MailAddress</returns>
 /// <remarks>
 /// Could be a in line function to if only used in one method for C# 7.
 /// </remarks>
 private MailAddress CreateFriendltAddress(MailFriendly sender) => new MailAddress(sender.Address, sender.Display);