简体   繁体   English

Mimekit / mailkit下载邮件正文?

[英]Mimekit/mailkit download message body?

I have been making my own mail client recently and added a receive option, I used mimekit and mailkit as plugins and was able to download most of my mails with a reader that is supposed to show content(right now it shows subject, to, from, date) 我最近一直在制作自己的邮件客户端,并添加了一个接收选项,我使用mimekit和mailkit作为插件,并且能够使用应该显示内容的阅读器下载我的大部分邮件(现在它显示主题,来自,来自, ,日期)

The way I downloaded the subject, to, ... is msg.envelope.subject, msg.envelope.to But I cannot download the body like this :( when doing either msg.body, msg.textbody, msg.bodyparts, ... they all result in NOTHING, the place where it should be is just empty, I can't get it downloaded :( 我下载主题的方式是,...是msg.envelope.subject,msg.envelope.to但我无法下载这样的身体:(当做msg.body,msg.textbody,msg.bodyparts ,. ..他们都导致了NOTHING,它应该是空的地方,我无法下载:(

Can anyone help me? 谁能帮我?

There are 2 ways to get the message body: 获取邮件正文有两种方法:

1. Download the Whole Message 1.下载整个消息

This method is probably the easiest way. 这种方法可能是最简单的方法。

To do this, all you need to do is call: 要做到这一点,您需要做的就是致电:

var message = folder.GetMessage (uid);

or 要么

var message = folder.GetMessage (index);

I would recommend always using the UniqueId of the message. 我建议始终使用消息的UniqueId Since you are already using the Fetch method, all you need to do to make sure that you have the UniqueId of the message is to include the MessageSummaryItems.UniqueId in your fetch request: 由于您已经在使用Fetch方法,因此要确保您拥有消息的UniqueId ,您需要做的就是在获取请求中包含MessageSummaryItems.UniqueId

var messages = folder.Fetch (0, -1, MessageSummaryItems.UniqueId |
    MessageSummaryItems.Envelope | ...);

Once you have the message, you can do whatever you want with it. 收到消息后,您可以随心所欲地做任何事情。

For rendering the message, I would recommend taking a look at the MessageReader sample included in the MimeKit GitHub repository. 为了呈现消息,我建议您查看MimeKit GitHub存储库中包含的MessageReader示例。

It will show you how to properly render a MimeMessage . 它将向您展示如何正确呈现MimeMessage

2. Download Only What You Need 2.仅下载您需要的内容

This method is a bit harder but can be more efficient as far as network bandwidth usage goes. 这种方法有点难,但网络带宽的使用而言可能更有效。

The first thing you need to do is to make sure to include the MessageSummaryItems.BodyStructure bit flag in the Fetch method. 您需要做的第一件事是确保在Fetch方法中包含MessageSummaryItems.BodyStructure位标志。 For example: 例如:

var messages = folder.Fetch (0, -1, MessageSummaryItems.Envelope | 
    MessageSummaryItems.BodyStructure);

(You'll probably want other fields, but that's just an example to show you how to bitwise-or flags together to request multiple message summary items). (您可能需要其他字段,但这只是一个示例,向您展示如何按位或标记一起请求多个消息摘要项)。

By requesting the BodyStructure of the messages, you'll be able to make use of the msg.Body property. 通过请求消息的BodyStructure ,您将能够使用msg.Body属性。

Each msg.Body will be a BodyPart object which is an abstract class. 每个msg.Body都是一个BodyPart对象,它是一个抽象类。 The 2 main subclasses are BodyPartMultipart and BodyPartBasic . 2个主要的子类是BodyPartMultipartBodyPartBasic You can use the as cast or the is keyword to figure out which one it is: 您可以使用as cast或is关键字来确定它是哪一个:

var multipart = msg.Body as BodyPartMultipart;

if (multipart != null) {
    // the top-level body part is a multi-part
} else {
    // the body is a basic singleton part
}

This is how you would iterate over the subparts of a BodyPartMultipart : 这是你如何迭代BodyPartMultipart的子部分:

foreach (var part in multipart.BodyParts) {
    // each part will either be a BodyPartMultipart
    // or a BodyPartBasic, just like before...
}

There are also 2 subclasses of BodyPartBasic which are: BodyPartText and BodyPartMessage . 还有2个BodyPartBasic子类: BodyPartTextBodyPartMessage A BodyPartText is a textual-based MIME part (meaning it has a MIME-type of text/* ) whereas a BodyPartMessage is an embedded message (and will have a MIME-type of message/rfc822 ). BodyPartText是基于文本的MIME部分(意味着它具有MIME类型text/* ),而BodyPartMessage是嵌入式消息(并且将具有MIME类型的message/rfc822 )。

Since MIME is recursive, you'll need to implement a recursive function to walk the MIME tree structure to find whatever MIME part you are looking for. 由于MIME是递归的,因此您需要实现一个递归函数来遍历MIME树结构以查找您要查找的任何MIME部分。

For your convenience, the TextBody and HtmlBody properties on the IMessageSummary interface will locate and return the text/plain and text/html body parts, respectively. 为方便起见, IMessageSummary界面上的TextBodyHtmlBody属性将分别定位并返回text/plaintext/html正文部分。

It should be noted, however, that these properties only work in cases where the structure of the message follows the standard convention (notice I said convention , there is no formal standard dictating the location of the message text within a MIME hierarchy). 但是应该注意,这些属性仅适用于消息结构遵循标准约定的情况(注意我所说的约定 ,没有正式标准规定消息文本在MIME层次结构中的位置)。

It should also be noted that if your mail client will be rendering the HTML body, the HTML body part may be part of a group of related MIME parts (ie a child of a multipart/related ), but the HtmlBody property will not be able to return that and so implementing your own recursive logic will be a better option. 还应该注意的是,如果您的邮件客户端将呈现HTML正文,则HTML正文部分可能是一组相关MIME部分(即multipart/related的子HtmlBody )的一部分,但HtmlBody属性将无法返回它,因此实现自己的递归逻辑将是一个更好的选择。

For an example of how to do this, check out the ImapClientDemo sample in the MailKit GitHub repository. 有关如何执行此ImapClientDemo示例,请查看MailKit GitHub存储库中的ImapClientDemo示例。 The logic currently resides in the MainWindow.cs code. 逻辑当前位于MainWindow.cs代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM