我正在尝试使用来自Gmail收件箱的mailbee阅读电子邮件。 我们的想法是获取任何winmail.dat并提取其附件并将其添加到附件池中,这样我们就不必手动完成。 MailBee无法识别我使用我的testmail作为Tnef文件发送的winmail.dat(文件的类型是Tnef附件类型)。 IsTnef是假的。
我无法找到很多关于mailbee tnef附件的信息,有什么想法吗?
这就是我得到的:
var attachments = message.Attachments.Cast<MailBeeAttachment>().ToList();
var encapsulated = attachments.Where(a => a.IsTnef).SelectMany(a => a.GetAttachmentsFromTnef().Cast<MailBeeAttachment>());
// Add encapsulated attachments
attachments.AddRange(encapsulated);
答案 0 :(得分:1)
您可能希望继续使用MailBee ...但是如果您对替代方案持开放态度,请让我回答这个问题,就像您使用我的开源MimeKit / MailKit库一样:< / p>
当MimeKit的MIME解析器遇到带有与Content-Type
或application/vnd.ms-tnef
匹配的application/ms-tnef
标头的附件时,它会自动使用专用TnefPart类来代表那种依恋。
要从中提取封装的附件,您只需使用
ExtractAttachments()方法可能与MailBee的GetAttachmentsFromTnef()
方法非常相似。
将代码从MailBee转换为MimeKit将如下所示:
var attachments = message.Attachments.ToList();
var encapsulated = attachments.OfType<TnefPart>().SelectMany(a => a.ExtractAttachments ());
// Add encapsulated attachments
attachments.AddRange(encapsulated);
很可能是MailBee的IsTnef
属性为您返回false的原因可能是因为Content-Type
标题与我提到的tnef mime类型不匹配早。如果情况确实如此,那么上面发布的翻译代码也将在MimeKit下失败。
...然而
如果您决定使用替代方法确定包含TNEF内容的MIME部分,MimeKit还提供较低级别的TNEF支持类。
static void ExtractAttachments (MimePart attachment, IList<MimeEntity> attachments)
{
using (var reader = new TnefReader (attachment.ContentObject.Open (), 0, TnefComplianceMode.Loose)) {
// skip past the non-attachment tnef content...
while (reader.ReadNextAttribute ()) {
if (reader.AttributeLevel == TnefAttributeLevel.Attachment)
break;
}
if (reader.AttributeLevel == TnefAttributeLevel.Attachment)
ExtractAttachments (reader, attachments);
}
}
static void ExtractAttachments (TnefReader reader, IList<MimeEntity> attachments)
{
var attachMethod = TnefAttachMethod.ByValue;
var filter = new BestEncodingFilter ();
var prop = reader.TnefPropertyReader;
MimePart attachment = null;
int outIndex, outLength;
TnefAttachFlags flags;
string[] mimeType;
byte[] attachData;
string text;
do {
if (reader.AttributeLevel != TnefAttributeLevel.Attachment)
break;
switch (reader.AttributeTag) {
case TnefAttributeTag.AttachRenderData:
attachMethod = TnefAttachMethod.ByValue;
attachment = new MimePart ();
break;
case TnefAttributeTag.Attachment:
if (attachment == null)
break;
while (prop.ReadNextProperty ()) {
switch (prop.PropertyTag.Id) {
case TnefPropertyId.AttachLongFilename:
attachment.FileName = prop.ReadValueAsString ();
break;
case TnefPropertyId.AttachFilename:
if (attachment.FileName == null)
attachment.FileName = prop.ReadValueAsString ();
break;
case TnefPropertyId.AttachContentLocation:
text = prop.ReadValueAsString ();
if (Uri.IsWellFormedUriString (text, UriKind.Absolute))
attachment.ContentLocation = new Uri (text, UriKind.Absolute);
else if (Uri.IsWellFormedUriString (text, UriKind.Relative))
attachment.ContentLocation = new Uri (text, UriKind.Relative);
break;
case TnefPropertyId.AttachContentBase:
text = prop.ReadValueAsString ();
attachment.ContentBase = new Uri (text, UriKind.Absolute);
break;
case TnefPropertyId.AttachContentId:
attachment.ContentId = prop.ReadValueAsString ();
break;
case TnefPropertyId.AttachDisposition:
text = prop.ReadValueAsString ();
if (attachment.ContentDisposition == null)
attachment.ContentDisposition = new ContentDisposition (text);
else
attachment.ContentDisposition.Disposition = text;
break;
case TnefPropertyId.AttachData:
var stream = prop.GetRawValueReadStream ();
var content = new MemoryStream ();
var guid = new byte[16];
if (attachMethod == TnefAttachMethod.EmbeddedMessage) {
var tnef = new TnefPart ();
foreach (var param in attachment.ContentType.Parameters)
tnef.ContentType.Parameters[param.Name] = param.Value;
if (attachment.ContentDisposition != null)
tnef.ContentDisposition = attachment.ContentDisposition;
attachment = tnef;
}
// read the GUID
stream.Read (guid, 0, 16);
// the rest is content
using (var filtered = new FilteredStream (content)) {
filtered.Add (filter);
stream.CopyTo (filtered, 4096);
filtered.Flush ();
}
content.Position = 0;
attachment.ContentTransferEncoding = filter.GetBestEncoding (EncodingConstraint.SevenBit);
attachment.ContentObject = new ContentObject (content);
filter.Reset ();
attachments.Add (attachment);
break;
case TnefPropertyId.AttachMethod:
attachMethod = (TnefAttachMethod) prop.ReadValueAsInt32 ();
break;
case TnefPropertyId.AttachMimeTag:
mimeType = prop.ReadValueAsString ().Split ('/');
if (mimeType.Length == 2) {
attachment.ContentType.MediaType = mimeType[0].Trim ();
attachment.ContentType.MediaSubtype = mimeType[1].Trim ();
}
break;
case TnefPropertyId.AttachFlags:
flags = (TnefAttachFlags) prop.ReadValueAsInt32 ();
if ((flags & TnefAttachFlags.RenderedInBody) != 0) {
if (attachment.ContentDisposition == null)
attachment.ContentDisposition = new ContentDisposition (ContentDisposition.Inline);
else
attachment.ContentDisposition.Disposition = ContentDisposition.Inline;
}
break;
case TnefPropertyId.AttachSize:
if (attachment.ContentDisposition == null)
attachment.ContentDisposition = new ContentDisposition ();
attachment.ContentDisposition.Size = prop.ReadValueAsInt64 ();
break;
case TnefPropertyId.DisplayName:
attachment.ContentType.Name = prop.ReadValueAsString ();
break;
}
}
break;
case TnefAttributeTag.AttachCreateDate:
if (attachment != null) {
if (attachment.ContentDisposition == null)
attachment.ContentDisposition = new ContentDisposition ();
attachment.ContentDisposition.CreationDate = prop.ReadValueAsDateTime ();
}
break;
case TnefAttributeTag.AttachModifyDate:
if (attachment != null) {
if (attachment.ContentDisposition == null)
attachment.ContentDisposition = new ContentDisposition ();
attachment.ContentDisposition.ModificationDate = prop.ReadValueAsDateTime ();
}
break;
case TnefAttributeTag.AttachTitle:
if (attachment != null && string.IsNullOrEmpty (attachment.FileName))
attachment.FileName = prop.ReadValueAsString ();
break;
case TnefAttributeTag.AttachMetaFile:
if (attachment == null)
break;
// TODO: what to do with the meta data?
break;
case TnefAttributeTag.AttachData:
if (attachment == null || attachMethod != TnefAttachMethod.ByValue)
break;
attachData = prop.ReadValueAsBytes ();
filter.Flush (attachData, 0, attachData.Length, out outIndex, out outLength);
attachment.ContentTransferEncoding = filter.GetBestEncoding (EncodingConstraint.EightBit);
attachment.ContentObject = new ContentObject (new MemoryStream (attachData, false));
filter.Reset ();
attachments.Add (attachment);
break;
}
} while (reader.ReadNextAttribute ());
}
当然......你也可以通过这样做来作弊:
var tnef = new TnefPart { ContentObject = attachment.ContentObject };
attachments.AddRange (tnef.ExtractAttachments ());
因此最终结果可能如下所示(如果您决定与FileName匹配):
var attachments = message.Attachments.ToList();
var encapsulated = attachments.OfType<MimePart>()
.Where(x => (x is TnefPart) || x.FileName == "winmail.dat")
.SelectMany(x => {
var tnef = (x as TnefPart) ?? new TnefPart { ContentObject = x.ContentObject };
return tnef.ExtractAttachments ();
});
// Add encapsulated attachments
attachments.AddRange(encapsulated);
答案 1 :(得分:1)
测试时不应手动添加winmail.dat。它始终以特殊方式由MS Exchange或Outlook生成(和附加)。 IsTnef将从真实(Outlook或Exchange制作的)电子邮件中返回真正的附件。