点击下载 MimeEntity.rar

这个类是关于Mime实体的类
看下面代码吧

/// <summary>
/// 类说明:Assistant
/// 编 码 人:苏飞
/// 联系方式:361983679
/// 更新网站:[url=http://www.cckan.net/thread-655-1-1.html]http://www.cckan.net/thread-655-1-1.html[/url]
/// </summary>
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.IO;
using System.Net.Mime;
using System.Net.Mail; namespace DotNet.Utilities
{
/// <summary>
/// This class represents a Mime entity.
/// 这个类表示一个MIME实体
/// </summary>
public class MimeEntity
{
private StringBuilder _encodedMessage;
/// <summary>
/// Gets the encoded message.
/// 获取编码的消息。
/// </summary>
/// <value>
/// 编码的消息
/// The encoded message.
/// </value>
public StringBuilder EncodedMessage
{
get { return _encodedMessage; }
} private List<MimeEntity> _children; /// <summary>
/// Gets the children.
/// </summary>
/// <value>The children.</value>
public List<MimeEntity> Children
{
get
{
return _children;
}
} private ContentType _contentType;
/// <summary>
/// Gets the type of the content.
/// </summary>
/// <value>The type of the content.</value>
public ContentType ContentType
{
get { return _contentType; }
} private string _mediaSubType;
/// <summary>
/// Gets the type of the media sub.
/// </summary>
/// <value>The type of the media sub.</value>
public string MediaSubType
{
get { return _mediaSubType; }
} private string _mediaMainType;
/// <summary>
/// Gets the type of the media main.
/// </summary>
/// <value>The type of the media main.</value>
public string MediaMainType
{
get { return _mediaMainType; }
} private NameValueCollection _headers;
/// <summary>
/// Gets the headers.
/// </summary>
/// <value>The headers.</value>
public NameValueCollection Headers
{
get { return _headers; }
} private string _mimeVersion;
/// <summary>
/// Gets or sets the MIME version.
/// </summary>
/// <value>The MIME version.</value>
public string MimeVersion
{
get
{
return _mimeVersion;
}
set
{
_mimeVersion = value;
}
} private string _contentId;
/// <summary>
/// Gets or sets the content id.
/// </summary>
/// <value>The content id.</value>
public string ContentId
{
get
{
return _contentId;
}
set
{
_contentId = value;
}
} private string _contentDescription;
/// <summary>
/// Gets or sets the content description.
/// </summary>
/// <value>The content description.</value>
public string ContentDescription
{
get
{
return _contentDescription;
}
set
{
_contentDescription = value;
}
} private ContentDisposition _contentDisposition;
/// <summary>
/// Gets or sets the content disposition.
/// </summary>
/// <value>The content disposition.</value>
public ContentDisposition ContentDisposition
{
get
{
return _contentDisposition;
}
set
{
_contentDisposition = value;
}
} private string _transferEncoding;
/// <summary>
/// Gets or sets the transfer encoding.
/// </summary>
/// <value>The transfer encoding.</value>
public string TransferEncoding
{
get
{
return _transferEncoding;
}
set
{
_transferEncoding = value;
}
} private TransferEncoding _contentTransferEncoding;
/// <summary>
/// Gets or sets the content transfer encoding.
/// </summary>
/// <value>The content transfer encoding.</value>
public TransferEncoding ContentTransferEncoding
{
get
{
return _contentTransferEncoding;
}
set
{
_contentTransferEncoding = value;
}
} /// <summary>
/// Gets a value indicating whether this instance has boundary.
/// </summary>
/// <value>
/// <c>true</c> if this instance has boundary; otherwise, <c>false</c>.
/// </value>
internal bool HasBoundary
{
get
{
return (!string.IsNullOrEmpty(_contentType.Boundary))
|| (!string.IsNullOrEmpty(_startBoundary));
}
} private string _startBoundary;
/// <summary>
/// Gets the start boundary.
/// </summary>
/// <value>The start boundary.</value>
public string StartBoundary
{
get
{
if (string.IsNullOrEmpty(_startBoundary) || !string.IsNullOrEmpty(_contentType.Boundary))
{
return string.Concat("--", _contentType.Boundary);
} return _startBoundary;
}
} /// <summary>
/// Gets the end boundary.
/// </summary>
/// <value>The end boundary.</value>
public string EndBoundary
{
get
{
return string.Concat(StartBoundary, "--");
}
} private MimeEntity _parent;
/// <summary>
/// Gets or sets the parent.
/// </summary>
/// <value>The parent.</value>
public MimeEntity Parent
{
get { return _parent; }
set { _parent = value; }
} private MemoryStream _content; /// <summary>
/// Gets or sets the content.
/// </summary>
/// <value>The content.</value>
public MemoryStream Content
{
get { return _content; }
internal set { _content = value; }
} /// <summary>
/// Initializes a new instance of the <see cref="MimeEntity"/> class.
/// </summary>
public MimeEntity()
{
_children = new List<MimeEntity>();
_headers = new NameValueCollection();
_contentType = MimeReader.GetContentType(string.Empty);
_parent = null;
_encodedMessage = new StringBuilder();
} /// <summary>
/// Initializes a new instance of the <see cref="MimeEntity"/> class.
/// </summary>
/// <param name="parent">The parent.</param>
public MimeEntity(MimeEntity parent)
: this()
{
if (parent == null)
{
throw new ArgumentNullException("parent");
} _parent = parent;
_startBoundary = parent.StartBoundary;
} /// <summary>
/// Sets the type of the content.
/// </summary>
/// <param name="contentType">Type of the content.</param>
internal void SetContentType(ContentType contentType)
{
_contentType = contentType;
_contentType.MediaType = MimeReader.GetMediaType(contentType.MediaType);
_mediaMainType = MimeReader.GetMediaMainType(contentType.MediaType);
_mediaSubType = MimeReader.GetMediaSubType(contentType.MediaType);
} /// <summary>
/// Toes the mail message ex.
/// </summary>
/// <returns></returns>
public MailMessageEx ToMailMessageEx()
{
return ToMailMessageEx(this);
} /// <summary>
/// Toes the mail message ex.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns></returns>
private MailMessageEx ToMailMessageEx(MimeEntity entity)
{
if (entity == null)
{
//throw new ArgumentNullException("entity");
return null;
} //parse standard headers and create base email.
MailMessageEx message = MailMessageEx.CreateMailMessageFromEntity(entity); if (!string.IsNullOrEmpty(entity.ContentType.Boundary))
{
message = MailMessageEx.CreateMailMessageFromEntity(entity);
BuildMultiPartMessage(entity, message);
}//parse multipart message into sub parts.
else if (string.Equals(entity.ContentType.MediaType, MediaTypes.MessageRfc822, StringComparison.InvariantCultureIgnoreCase))
{
//use the first child to create the multipart message.
if (entity.Children.Count < )
{
throw new Pop3Exception("Invalid child count on message/rfc822 entity.");
} //create the mail message from the first child because it will
//contain all of the mail headers. The entity in this state
//only contains simple content type headers indicating, disposition, type and description.
//This means we can't create the mail message from this type as there is no
//internet mail headers attached to this entity.
message = MailMessageEx.CreateMailMessageFromEntity(entity.Children[]);
BuildMultiPartMessage(entity, message);
} //parse nested message.
else
{
message = MailMessageEx.CreateMailMessageFromEntity(entity);
BuildSinglePartMessage(entity, message);
} //Create single part message. return message;
} /// <summary>
/// Builds the single part message.
/// </summary>
/// <param name="entity">The entity.</param>
/// <param name="message">The message.</param>
private void BuildSinglePartMessage(MimeEntity entity, MailMessageEx message)
{
SetMessageBody(message, entity);
} /// <summary>
/// Gets the body encoding.
/// </summary>
/// <param name="contentType">Type of the content.</param>
public Encoding GetEncoding()
{
if (string.IsNullOrEmpty(this.ContentType.CharSet))
{
return Encoding.ASCII;
}
else
{
try
{
return Encoding.GetEncoding(this.ContentType.CharSet);
}
catch (ArgumentException)
{
return Encoding.ASCII;
}
}
} /// <summary>
/// Builds the multi part message.
/// </summary>
/// <param name="entity">The entity.</param>
/// <param name="message">The message.</param>
private void BuildMultiPartMessage(MimeEntity entity, MailMessageEx message)
{
foreach (MimeEntity child in entity.Children)
{
if (child == null)
{
continue;
}
if (string.Equals(child.ContentType.MediaType, MediaTypes.MultipartAlternative, StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(child.ContentType.MediaType, MediaTypes.MultipartMixed, StringComparison.InvariantCultureIgnoreCase))
{
BuildMultiPartMessage(child, message);
} //if the message is mulitpart/alternative or multipart/mixed then the entity will have children needing parsed.
else if (!IsAttachment(child) &&
(string.Equals(child.ContentType.MediaType, MediaTypes.TextPlain)
|| string.Equals(child.ContentType.MediaType, MediaTypes.TextHtml)))
{
message.AlternateViews.Add(CreateAlternateView(child));
SetMessageBody(message, child); } //add the alternative views.
else if (string.Equals(child.ContentType.MediaType, MediaTypes.MessageRfc822, StringComparison.InvariantCultureIgnoreCase)
&& string.Equals(child.ContentDisposition.DispositionType, DispositionTypeNames.Attachment, StringComparison.InvariantCultureIgnoreCase))
{
message.Children.Add(ToMailMessageEx(child));
} //create a child message and
else if (IsAttachment(child))
{
message.Attachments.Add(CreateAttachment(child)); }
}
} private static bool IsAttachment(MimeEntity child)
{
return (child.ContentDisposition != null)
&& (string.Equals(child.ContentDisposition.DispositionType, DispositionTypeNames.Attachment, StringComparison.InvariantCultureIgnoreCase));
} /// <summary>
/// Sets the message body.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="child">The child.</param>
private void SetMessageBody(MailMessageEx message, MimeEntity child)
{
Encoding encoding = child.GetEncoding();
message.Body = DecodeBytes(child.Content.ToArray(), encoding);
message.BodyEncoding = encoding;
message.IsBodyHtml = string.Equals(MediaTypes.TextHtml,
child.ContentType.MediaType, StringComparison.InvariantCultureIgnoreCase);
} /// <summary>
/// Decodes the bytes.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="encoding">The encoding.</param>
/// <returns></returns>
private string DecodeBytes(byte[] buffer, Encoding encoding)
{
if (buffer == null)
{
return null;
} if (encoding == null)
{
encoding = Encoding.UTF7;
} //email defaults to 7bit. return encoding.GetString(buffer);
} /// <summary>
/// Creates the alternate view.
/// </summary>
/// <param name="view">The view.</param>
/// <returns></returns>
private AlternateView CreateAlternateView(MimeEntity view)
{
AlternateView alternateView = new AlternateView(view.Content, view.ContentType);
alternateView.TransferEncoding = view.ContentTransferEncoding;
alternateView.ContentId = TrimBrackets(view.ContentId);
return alternateView;
} /// <summary>
/// Trims the brackets.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
public static string TrimBrackets(string value)
{
if (value == null)
{
return value;
} if (value.StartsWith("<") && value.EndsWith(">"))
{
return value.Trim('<', '>');
} return value;
} /// <summary>
/// Creates the attachment.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns></returns>
private Attachment CreateAttachment(MimeEntity entity)
{
Attachment attachment = new Attachment(entity.Content, entity.ContentType); if (entity.ContentDisposition != null)
{
attachment.ContentDisposition.Parameters.Clear();
foreach (string key in entity.ContentDisposition.Parameters.Keys)
{
attachment.ContentDisposition.Parameters.Add(key, entity.ContentDisposition.Parameters[key]);
} attachment.ContentDisposition.CreationDate = entity.ContentDisposition.CreationDate;
attachment.ContentDisposition.DispositionType = entity.ContentDisposition.DispositionType;
attachment.ContentDisposition.FileName = entity.ContentDisposition.FileName;
attachment.ContentDisposition.Inline = entity.ContentDisposition.Inline;
attachment.ContentDisposition.ModificationDate = entity.ContentDisposition.ModificationDate;
attachment.ContentDisposition.ReadDate = entity.ContentDisposition.ReadDate;
attachment.ContentDisposition.Size = entity.ContentDisposition.Size;
} if (!string.IsNullOrEmpty(entity.ContentId))
{
attachment.ContentId = TrimBrackets(entity.ContentId);
} attachment.TransferEncoding = entity.ContentTransferEncoding; return attachment;
}
}
}

[Mime] MimeEntity--MimeEntity Mime实体帮助类 (转载)的更多相关文章

  1. Intellij IDEA集成mybatis-generator插件自动生成数据库实体操作类

    Intellij IDEA集成mybatis-generator插件自动生成数据库实体操作类 转载至:https://blog.csdn.net/fishinhouse/article/details ...

  2. [Mime] MimeReader--读取Mime的帮助类 (转载)

    点击下载 MimeReader.rar 这个类是关于MimeReader的帮助类看下面代码吧 /// <summary> /// 类说明:Assistant /// 编 码 人:苏飞 // ...

  3. MIME协议(四) -- MIME消息的头字段

    MIME消息的头字段 4.1  Content-Type 对于表示某个具体资源的MIME消息,它的消息头中需要指定资源的数据类型:对于MIME组合消息,它的消息头中需要指定组合关系.具体资源的数据类型 ...

  4. golang:mime.Encode、mime.Decode

    最近在做邮件解析的工作,所以记录一下对mime.Encode.mime.Decode的总结.

  5. golang:mime.Decode、mime.DecodeHeader

    最近在做邮件解析的相关工作,在使用mime.Decode/mime.DecodeHeader时有些疑问. 有些搞不懂mime.Encode和mime.EncodeHeader的区别.

  6. 利用Xml架构生成实体访问类

    由xml生成xsd及实体类   xmldataset工具 使用VS2005工具XSD.exe(SDK/v2.0/Bin/xsd.exe)自动生成实体类: xsd /c /namespace:myCom ...

  7. abp 修改abp.zero的实体映射类,使生成的表和字段为大写状态

    在我们项目中,由于涉及到报表配置管理,可以通过一段sql快捷的配置出一个报表页面.部分sql会与abp框架的一些系统表做关联查询,而abp的映射类没有单独设置表和字段的名称,默认用类名和属性名,区分大 ...

  8. 实体类与实体DTO类之间的转换

    实体类与实体DTO类之间的转换 实体类与实体DTO类之间的转换 1.通过使用第三方序列化反序列化工具Newtonsoft.Json 2.通过反射实现 3.通过表达式目录树加字典缓存实现 4. 通过表达 ...

  9. MIME类型释义--MIME类型大全--web.xml中有关<mime-mapping>配置说明

    最早的HTTP协议中,并没有附加的数据类型信息,所有传送的数据都被客户程序解释为超文本标记语言HTML 文档,而为了支持多媒体数据类型,HTTP协议中就使用了附加在文档之前的MIME数据类型信息来标识 ...

随机推荐

  1. Andstudio更新失败的解决办法。

    最近AndroidStudio0.60出来了,就急忙想升级,结果屡试屡败.不管架设国外VPN还是Goagent都不行.之前这个问题遇到过,怎么解决的就忘记了.这次又遇到,所以在这里记下,已备以后查阅使 ...

  2. bzoj1899

    显然如果只有一个窗口,是一道贪心的题目,直接让吃饭慢的排在前面即可 两个窗口的话,我们还是根据这个原则 先对吃饭时间降序排序,然后这是一个dp 假如设当前处理到第i个人,当在窗口1的打饭时间确定了,窗 ...

  3. JSOI2008 火星人prefix

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2918  Solved: 866[Submit][ ...

  4. [PeterDLax著泛函分析习题参考解答]第1章 线性空间

    1. 证明定理 1. 2. 验证上述结论. 3. 证明定理 3. 4. 证明定理 4. 证明: 由 $$\bex x=\sum_{k=1}^{n-1}a_k\cdot \sum_{j=1}^{n-1} ...

  5. Count the string -- HDOJ 3336

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HTML5 移动应用开发环境搭建及原理分析

    开发环境搭建: 一.Android 开发平台搭建 安装java jdk:\\10.194.151.132\Mewfile\tmp\ADT 配置java jdk 1)  新建系统变量,JAVA_HOME ...

  7. android camera(三):camera V4L2 FIMC

    1. V4L2 1)简介 在Linux中,摄像头方面的标准化程度比较高,这个标准就是V4L2驱动程序,这也是业界比较公认的方式. V4L全称是Video for Linux,是Linux内核中标准的关 ...

  8. Axure RP 8.0 中继器初体验

    为了解决增删等复杂交互的问题,中继器是个不错的选择. 拖拽出一个默认的中继器 中继器的数据集感觉就像是数据库一样,在右边检视窗口中可以看到中继器的默认数据集,可以理解成一张二维表.默认有1列,现成的3 ...

  9. Kth Ancestor 第k个祖先问题

    题目出处 这道题目出自hackerrank的8月月赛的第三题. 题目大意: 先给出一棵树 之后有三种操作分别为:加边,查询,和删除一个节点 查询的时候要给出任意节点x的第k个祖先 每组数据有t个cas ...

  10. HDU 2102 A计划(三维BFS)

    这题太欢乐了......虽然wa了几次,但是想到骑士在两幅图的传送门中传来传去就觉得这骑士太坑了 #include <cstdio> #include <iostream> # ...