示例#1
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);

                    MimeMultiPart attachmentsPart = new MimeMultiPart(MimeMultiPartType.Mixed);
                    foreach (Attachment attachment in Attachments)
                    {
                        if (attachment != null)
                        {
                            //ensure we can read from the stream.
                            attachment.PrepareForSending(allowUnicode);
                            attachmentsPart.Parts.Add(attachment.MimePart);
                        }
                    }
                    part.Parts.Add(attachmentsPart);
                    _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;
            }
        }
 private void SetContent()
 {
     if (this.bodyView != null)
     {
         this.bodyView.Dispose();
         this.bodyView = null;
     }
     if ((this.AlternateViews.Count == 0) && (this.Attachments.Count == 0))
     {
         if ((this.body != null) && (this.body != string.Empty))
         {
             this.bodyView        = AlternateView.CreateAlternateViewFromString(this.body, this.bodyEncoding, this.isBodyHtml ? "text/html" : null);
             this.message.Content = this.bodyView.MimePart;
         }
     }
     else if ((this.AlternateViews.Count == 0) && (this.Attachments.Count > 0))
     {
         MimeMultiPart part = new MimeMultiPart(MimeMultiPartType.Mixed);
         if ((this.body != null) && (this.body != string.Empty))
         {
             this.bodyView = AlternateView.CreateAlternateViewFromString(this.body, this.bodyEncoding, this.isBodyHtml ? "text/html" : null);
         }
         else
         {
             this.bodyView = AlternateView.CreateAlternateViewFromString(string.Empty);
         }
         part.Parts.Add(this.bodyView.MimePart);
         foreach (Attachment attachment in this.Attachments)
         {
             if (attachment != null)
             {
                 attachment.PrepareForSending();
                 part.Parts.Add(attachment.MimePart);
             }
         }
         this.message.Content = part;
     }
     else
     {
         MimeMultiPart part2 = null;
         MimeMultiPart part3 = new MimeMultiPart(MimeMultiPartType.Alternative);
         if ((this.body != null) && (this.body != string.Empty))
         {
             this.bodyView = AlternateView.CreateAlternateViewFromString(this.body, this.bodyEncoding, null);
             part3.Parts.Add(this.bodyView.MimePart);
         }
         foreach (AlternateView view in this.AlternateViews)
         {
             if (view != null)
             {
                 view.PrepareForSending();
                 if (view.LinkedResources.Count > 0)
                 {
                     MimeMultiPart item = new MimeMultiPart(MimeMultiPartType.Related);
                     item.ContentType.Parameters["type"] = view.ContentType.MediaType;
                     item.ContentLocation = view.MimePart.ContentLocation;
                     item.Parts.Add(view.MimePart);
                     foreach (LinkedResource resource in view.LinkedResources)
                     {
                         resource.PrepareForSending();
                         item.Parts.Add(resource.MimePart);
                     }
                     part3.Parts.Add(item);
                 }
                 else
                 {
                     part3.Parts.Add(view.MimePart);
                 }
             }
         }
         if (this.Attachments.Count > 0)
         {
             part2 = new MimeMultiPart(MimeMultiPartType.Mixed)
             {
                 Parts = { part3 }
             };
             MimeMultiPart part5 = new MimeMultiPart(MimeMultiPartType.Mixed);
             foreach (Attachment attachment2 in this.Attachments)
             {
                 if (attachment2 != null)
                 {
                     attachment2.PrepareForSending();
                     part5.Parts.Add(attachment2.MimePart);
                 }
             }
             part2.Parts.Add(part5);
             this.message.Content = part2;
         }
         else if ((part3.Parts.Count == 1) && ((this.body == null) || (this.body == string.Empty)))
         {
             this.message.Content = part3.Parts[0];
         }
         else
         {
             this.message.Content = part3;
         }
     }
 }