我似乎无法从System.Net.Mail.AlternateView检索AlternateView.
我有一个通过POP3拉电子邮件的应用程序.我了解如何创建备用视图以进行发送,但在查看电子邮件时如何选择备用视图.我收到的电子邮件是一个System.Net.MailMessage对象,所以我可以轻松地拉出正文,编码,主题行等.我可以看到AlternateViews,也就是说,我可以看到计数是2但是想要在我请求正文时,提取当前返回的HTML以外的内容.
希望这有一点意义,有人可以对此有所了解.最后,我希望将明文拉出来,而不是HTML,而不是自己解析它.
Mightytighty正引领你走上正确的道路,但你不应该假设编码的类型.这应该做的伎俩:
var dataStream = view.ContentStream; dataStream.Position = 0; byte[] byteBuffer = new byte[dataStream.Length]; var encoding = Encoding.GetEncoding(view.ContentType.CharSet); string body = encoding.GetString(byteBuffer, 0, dataStream.Read(byteBuffer, 0, byteBuffer.Length));
我遇到了同样的问题,但你只需要从流中读取它.这是一个例子:
public string ExtractAlternateView() { var message = new System.Net.Mail.MailMessage(); message.Body = "This is the TEXT version"; //Add textBody as an AlternateView message.AlternateViews.Add( System.Net.Mail.AlternateView.CreateAlternateViewFromString( "This is the HTML version", new System.Net.Mime.ContentType("text/html") ) ); var dataStream = message.AlternateViews[0].ContentStream; byte[] byteBuffer = new byte[dataStream.Length]; return System.Text.Encoding.ASCII.GetString(byteBuffer, 0, dataStream.Read(byteBuffer, 0, byteBuffer.Length)); }