예제 #1
0
        private static string[] EncodeMailAddressCollection(MailAddressCollection collection)
        {
            if (collection.Count == 0)
            {
                return(Array.Empty <string>());
            }

            string[] addresses = new string[collection.Count];

            for (int i = 0; i < collection.Count; i++)
            {
                addresses[i] = SesHelper.EncodeMailAddress(collection[i]);
            }

            return(addresses);
        }
예제 #2
0
        public static SesEmail FromMailMessage(MailMessage message)
        {
            if (message is null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var doc = new SesEmail
            {
                Source  = SesHelper.EncodeMailAddress(message.From),
                To      = message.To.Select(r => SesHelper.EncodeMailAddress(r)).ToArray(),
                Subject = new SesContent(message.Subject, CharsetType.UTF8)
            };

            // "Carbonmade" <*****@*****.**>


            if (message.ReplyToList.Count > 0)
            {
                doc.ReplyTo = EncodeMailAddressCollection(message.ReplyToList);
            }
#pragma warning disable CS0618 // Type or member is obsolete
            else if (message.ReplyTo != null)
            {
                doc.ReplyTo = new[] { SesHelper.EncodeMailAddress(message.ReplyTo) };
            }
#pragma warning restore CS0618


            if (message.IsBodyHtml)
            {
                doc.Html = new SesContent(message.Body, CharsetType.UTF8);
            }
            else
            {
                doc.Text = new SesContent(message.Body, CharsetType.UTF8);
            }


            if (message.CC.Count > 0)
            {
                doc.CC = EncodeMailAddressCollection(message.CC);
            }

            // Alternate view support
            if (message.AlternateViews != null)
            {
                foreach (var view in message.AlternateViews)
                {
                    using var streamReader = new StreamReader(view.ContentStream);

                    string text = streamReader.ReadToEnd();

                    switch (view.ContentType.MediaType)
                    {
                    case "text/plain": doc.Text = new SesContent(text, CharsetType.UTF8); break;

                    case "text/html": doc.Html = new SesContent(text, CharsetType.UTF8); break;
                    }
                }
            }

            return(doc);
        }