Example #1
0
        public static SystemMailMessage ToMailMessage(this MailMessage mail)
        {
            var message = new SystemMailMessage
            {
                DeliveryNotificationOptions = mail.DeliveryNotificationOptions,
                IsBodyHtml      = mail.IsBodyHtml,
                Priority        = mail.Priority,
                Body            = mail.Body,
                Subject         = mail.Subject,
                BodyEncoding    = mail.BodyEncoding,
                SubjectEncoding = mail.SubjectEncoding,
                HeadersEncoding = mail.HeadersEncoding,
            };

            if (mail.From != null)
            {
                message.From = new MailAddress(mail.From);
            }

            if (mail.Sender != null)
            {
                message.Sender = new MailAddress(mail.Sender);
            }

            foreach (var alternateView in mail.AlternateViews)
            {
                var mimeType            = new ContentType(alternateView.ContentType);
                var systemAlternateView = SystemAlternateView.CreateAlternateViewFromString(alternateView.Content, mimeType);
                message.AlternateViews.Add(systemAlternateView);
            }

            mail.To.ForEach(a => message.To.Add(new MailAddress(a)));
            mail.ReplyTo.ForEach(a => message.ReplyToList.Add(new MailAddress(a)));
            mail.Bcc.ForEach(a => message.Bcc.Add(new MailAddress(a)));
            mail.Cc.ForEach(a => message.CC.Add(new MailAddress(a)));

            foreach (var header in mail.Headers)
            {
                message.Headers[header.Key] = header.Value;
            }

            return(message);
        }
Example #2
0
        private void SetContent(bool allowUnicode)
        {
            //the attachments may have changed, so we need to reset the message
            if (_bodyView != null)
            {
                _bodyView.Dispose();
                _bodyView = null;
            }

            if (AlternateViews.Count == 0 && Attachments.Count == 0)
            {
                if (!string.IsNullOrEmpty(_body))
                {
                    _bodyView        = AlternateView.CreateAlternateViewFromString(_body, _bodyEncoding, (_isBodyHtml ? MediaTypeNames.Text.Html : null));
                    _message.Content = _bodyView.MimePart;
                }
            }
            else if (AlternateViews.Count == 0 && Attachments.Count > 0)
            {
                MimeMultiPart part = new MimeMultiPart(MimeMultiPartType.Mixed);

                if (!string.IsNullOrEmpty(_body))
                {
                    _bodyView = AlternateView.CreateAlternateViewFromString(_body, _bodyEncoding, (_isBodyHtml ? MediaTypeNames.Text.Html : null));
                }
                else
                {
                    _bodyView = AlternateView.CreateAlternateViewFromString(string.Empty);
                }

                part.Parts.Add(_bodyView.MimePart);

                foreach (Attachment attachment in Attachments)
                {
                    if (attachment != null)
                    {
                        //ensure we can read from the stream.
                        attachment.PrepareForSending(allowUnicode);
                        part.Parts.Add(attachment.MimePart);
                    }
                }
                _message.Content = part;
            }
            else
            {
                // we should not unnecessarily use Multipart/Mixed
                // When there is no attachement and all the alternative views are of "Alternative" types.
                MimeMultiPart?part      = null;
                MimeMultiPart viewsPart = new MimeMultiPart(MimeMultiPartType.Alternative);

                if (!string.IsNullOrEmpty(_body))
                {
                    _bodyView = AlternateView.CreateAlternateViewFromString(_body, _bodyEncoding, null);
                    viewsPart.Parts.Add(_bodyView.MimePart);
                }

                foreach (AlternateView view in AlternateViews)
                {
                    //ensure we can read from the stream.
                    if (view != null)
                    {
                        view.PrepareForSending(allowUnicode);
                        if (view.LinkedResources.Count > 0)
                        {
                            MimeMultiPart wholeView = new MimeMultiPart(MimeMultiPartType.Related);
                            wholeView.ContentType.Parameters["type"] = view.ContentType.MediaType;
                            wholeView.ContentLocation = view.MimePart.ContentLocation;
                            wholeView.Parts.Add(view.MimePart);

                            foreach (LinkedResource resource in view.LinkedResources)
                            {
                                //ensure we can read from the stream.
                                resource.PrepareForSending(allowUnicode);

                                wholeView.Parts.Add(resource.MimePart);
                            }
                            viewsPart.Parts.Add(wholeView);
                        }
                        else
                        {
                            viewsPart.Parts.Add(view.MimePart);
                        }
                    }
                }

                if (Attachments.Count > 0)
                {
                    part = new MimeMultiPart(MimeMultiPartType.Mixed);
                    part.Parts.Add(viewsPart);

                    foreach (Attachment attachment in Attachments)
                    {
                        if (attachment != null)
                        {
                            //ensure we can read from the stream.
                            attachment.PrepareForSending(allowUnicode);
                            part.Parts.Add(attachment.MimePart);
                        }
                    }
                    _message.Content = part;
                }
                // If there is no Attachement, AND only "1" Alternate View AND !!no body!!
                // then in fact, this is NOT a multipart region.
                else if (viewsPart.Parts.Count == 1 && string.IsNullOrEmpty(_body))
                {
                    _message.Content = viewsPart.Parts[0];
                }
                else
                {
                    _message.Content = viewsPart;
                }
            }

            if (_bodyView != null && _bodyTransferEncoding != TransferEncoding.Unknown)
            {
                _bodyView.TransferEncoding = _bodyTransferEncoding;
            }
        }