我一直在尋找一個例子,說明如何將所有郵件保存到一個類似.mbox檔案的單一檔案中,但一直沒有成功。 以下是我嘗試過的一些代碼,但目前還沒有作業。
var exportStream = new MemoryStream(); foreach (var uid in uids) { var message = client.Inbox.GetMessage(uid); message.WriteTo(exportStream); } exportStream.Position = 0; using (var fileStream = File.Create(@"C: emp
esults.mbox")) { exportStream.Seek(0, SeekOrigin.Begin); exportStream.CopyTo(fileStream); }
uj5u.com熱心網友回復:
我在StackOverflow的另一個問題的評論部分給你的快速& dirty回復有點過于簡單,但基本上你需要做的是回圈瀏覽IMAP檔案夾中的郵件,然后將它們寫入一個檔案流,用一個mbox標記(通常是 不幸的是,mbox檔案格式并沒有特別好的標準化,所以對于 因此,為了將你的IMAP收件箱中的所有郵件保存到一個mbox檔案中,我們可能會這樣做: MboxFilter看起來是這樣的(尚未包含在MimeKit中,但可能會在v3.0中添加):
標籤:"From<SPACE><SOMETHING-ELSE><NEW-LINE>"/code>)分開。
"From<SPACE>"之后應該有哪些內容,并沒有嚴格的規則可循。一般來說它是賬戶的用戶名,后面是某個日期格式的時間戳,或其他的時間戳,然后是一個新行序列,但另一個相當常見的標記將只是"From -
"(在UNIX上)或"From -
"(在 Windows 上)。using (var mboxStream = File.Create ("Inbox.mbox") {
//創建我們的標準Mbox標記。
var mboxMarker = Encoding.ASCII.GetBytes ("From -" Environment.NewLine) 。
//獲得檔案夾中郵件UID的完整串列。
var uids = client.Inbox.Search(SearchQuery.All)。
/// Iterate over each UID, saving each message into the Mbox.
foreach (var uid in uids) {
var message = client.Inbox.GetMessage (uid);
//Mbox檔案中每條訊息的開始都有一個 "From"-line標記。
mboxStream.Write(mboxMarker, 0, mboxMarker.Length)。
//由于以 "From "開頭的行對mbox有特殊意義。
//決議器,我們需要以某種方式確保訊息中沒有任何一行。
///以 "來自 "開頭。為了做到這一點,我們創建了一個過濾的流, />
//將為我們修改發件人的行數。
using (var filtered = new FilteredStream (mboxStream)) {
//>添加mbox過濾器。
filtered.Add (new MboxFilter ())。
///將訊息寫到mbox檔案中,通過過濾器。
message.WriteTo (filtered);
///在處理過濾后的資料流之前,對其進行沖洗。
filtered.Flush()。
}
}
//。
//MboxFilter.cs
//
//作者。Jeffrey Stedfast <[email protected]>/span>
//
// Copyright (c) 2013-2021 .NET Foundation and Contributors
//
///特此允許任何人免費獲得副本。
//本軟體及相關檔案檔案("本軟體")的任何人,均可免費處理//本軟體。
//在本軟體中不受任何限制,包括但不限于權利。
///使用、復制、修改、合并、發布、分發、分許可和/或銷售 //使用本軟體
//本軟體的副本,并允許接受本軟體的人//本軟體。
//提供的人這樣做,但須符合以下條件: //提供的人這樣做。
/
///上述著作權宣告和本許可宣告應包括在中
//所有軟體的副本或實質部分。
//
//本軟體 "按原樣 "提供,不作任何形式的保證,無論是明示的還是的。
//隱含的,包括但不限于適銷對路的保證,//隱含的。
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT。在任何情況下,該都不應該
//作者或著作權所有者對任何索賠、損害或其他責任。
// LIABILITY, WHERE IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING BY,
//因本軟體或其使用或其他交易而產生的責任。
//本軟體。
//本軟體。
使用系統。
using System.Collections.Generic.Details
namespace MimeKit.IO.Filter {
// <summary>
// 一個過濾器,通過在行首塞入一個'>'來粉碎以 "From "開頭的行。
// </summary>/span>
// < remarks>
// <para>Munging Mbox-style "From" -lines是一種變通方法,以防止Mbox決議器誤解
//以 "From "開頭的行作為劃分資訊的Mbox標記。這種劃分是不可逆轉的,但
//是必要的,以正確地格式化資訊,保存到Mbox檔案。</para>
// </remarks>/span>
public class MboxFilter : MimeFilterBase
{
const string From = "From"/span>。
bool midline。
// <summary>/span>
// Initialize a new instance of the <see cref="MboxFilter"/> class.
// </summary>/span>
// < remarks>
//創建一個新的<see cref="MboxFilter"/>。
// </remarks>/span>
public MboxFilter()
{
}
static bool StartsWithFrom (byte[] input。int startIndex, int endIndex).
{
for (int i = 0, index = startIndex; i < From.Length && index < endIndex; i , index ) {
if (input[index] != (byte) From[i])
return false;
}
return true;
}
// <summary>
// 過濾指定的輸入。
// </summary>/span>
// < remarks>
// 從給定的索引開始過濾指定的輸入緩沖區,
//跨越指定的位元組數。
// </remarks>
// <returns>過濾后的輸出。</returns>
// <param name="input">輸入緩沖區。</param>
// <param name="startIndex">/span>輸入緩沖區的起始索引。</param>
// <param name="length">/span>輸入緩沖區的長度,從<paramref name="startIndex"/>/span>開始。 </param>。
// <param name="outputIndex">輸出索引。</param>。
// <param name="outputLength">/span>輸出長度。</param>/span>
// <param name="flush">/span>如果設定為< c>true</c>,所有內部緩沖的資料應該被重繪 到輸出緩沖區。 </param>,所有的內部緩沖資料應該被重繪 到輸出緩沖區。
protected override byte[] Filter (byte[] input。int startIndex, int length, out int outputIndex, out int outputLength, bool flush)
{
var fromOffsets = new List<int> ()。
int endIndex = startIndex length;
int index = startIndex;
int left;
while (index < endIndex) {
byte c = 0;
if (midline) {
while (index < endIndex) {
c = input[index ]。
if (c == (byte) '
')
break。
}
}
if (c == (byte) '
'/span> || !midline) {
if ((left = endIndex - index) > 0) {
midline = true;
if (left < 5) {
if (StartsWithFrom (input, index, endIndex)) {
SaveRemainingInput (input, index, left);
endIndex = index;
midline = false。
break。
}
} else {
if (StartsWithFrom (input, index, endIndex)) {
fromOffsets.Add (index);
index = 5;
}
}
} else {
midline = false;
}
}
}
if (fromOffsets.Count > 0) {
int need = (endIndex - startIndex) fromOffsets.Count;
EnsureOutputSize (need, false)。
outputLength = 0;
outputIndex = 0;
index = startIndex。
foreach (var offset in fromOffsets) {
if (index < offset) {
Buffer.BlockCopy (input, index, OutputBuffer, outputLength, offset - index);
outputLength = offset - index。
index = offset。
}
// munge the beginning of the "From" -line.
OutputBuffer[outputLength ] = (byte) '> '。
}
Buffer.BlockCopy (input, index, OutputBuffer, outputLength, endIndex - index);
outputLength = endIndex - index。
return OutputBuffer。
}
outputLength = endIndex - startIndex;
outputIndex = 0;
return input。
}
// <summary>/span>
//重新設定過濾器。
// </summary>/span>
// < remarks>
//重新設定過濾器。
// </remarks>/span>
public override void Reset ()
{
midline = false;
base.Reset ()。
}
}
