コード例 #1
0
ファイル: SesEmail.cs プロジェクト: yasinkultur/Amazon
        public static SesEmail FromMailMessage(MailMessage message)
        {
            #region Preconditions

            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            #endregion

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

            // "Carbonmade" <*****@*****.**>
            if (message.ReplyTo != null)
            {
                doc.ReplyTo = new[] { SesHelper.EncodeEmail(message.ReplyTo) };
            }
            else if (message.ReplyToList.Count > 0)
            {
                doc.ReplyTo = message.ReplyToList.Select(r => SesHelper.EncodeEmail(r)).ToArray();
            }

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

            // Alternate view support
            if (message.AlternateViews != null)
            {
                foreach (var view in message.AlternateViews)
                {
                    using (var streamReader = new StreamReader(view.ContentStream))
                    {
                        var 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);
        }
コード例 #2
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);
        }
コード例 #3
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);
        }