本文整理汇总了C#中MailKit.Net.Imap.ImapCommand类的典型用法代码示例。如果您正苦于以下问题:C# ImapCommand类的具体用法?C# ImapCommand怎么用?C# ImapCommand使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ImapCommand类属于MailKit.Net.Imap命名空间,在下文中一共展示了ImapCommand类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Create
/// <summary>
/// Create a new <see cref="ImapCommandException"/> based on the specified command name and <see cref="ImapCommand"/> state.
/// </summary>
/// <remarks>
/// Create a new <see cref="ImapCommandException"/> based on the specified command name and <see cref="ImapCommand"/> state.
/// </remarks>
/// <returns>A new command exception.</returns>
/// <param name="command">The command name.</param>
/// <param name="ic">The command state.</param>
internal static ImapCommandException Create (string command, ImapCommand ic)
{
var result = ic.Result.ToString ().ToUpperInvariant ();
string message, reason = null;
if (string.IsNullOrEmpty (ic.ResultText)) {
for (int i = 0; i < ic.RespCodes.Count; i++) {
if (ic.RespCodes[i].IsError) {
reason = ic.RespCodes[i].Message;
break;
}
}
} else {
reason = ic.ResultText;
}
if (!string.IsNullOrEmpty (reason))
message = string.Format ("The IMAP server replied to the '{0}' command with a '{1}' response: {2}", command, result, reason);
else
message = string.Format ("The IMAP server replied to the '{0}' command with a '{1}' response.", command, result);
return ic.Exception != null ? new ImapCommandException (message, ic.Exception) : new ImapCommandException (message);
}
示例2: SetMetadata
/// <summary>
/// Sets the specified metadata.
/// </summary>
/// <remarks>
/// Sets the specified metadata.
/// </remarks>
/// <returns>The metadata.</returns>
/// <param name="metadata">The metadata.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="metadata"/> is <c>null</c>.
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// The <see cref="ImapClient"/> has been disposed.
/// </exception>
/// <exception cref="ServiceNotConnectedException">
/// The <see cref="ImapClient"/> is not connected.
/// </exception>
/// <exception cref="ServiceNotAuthenticatedException">
/// The <see cref="ImapClient"/> is not authenticated.
/// </exception>
/// <exception cref="System.NotSupportedException">
/// The IMAP server does not support the METADATA extension.
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The operation was canceled via the cancellation token.
/// </exception>
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
/// <exception cref="ImapProtocolException">
/// The server's response contained unexpected tokens.
/// </exception>
/// <exception cref="ImapCommandException">
/// The server replied with a NO or BAD response.
/// </exception>
public override void SetMetadata (MetadataCollection metadata, CancellationToken cancellationToken = default (CancellationToken))
{
if (metadata == null)
throw new ArgumentNullException ("metadata");
CheckState (false, false);
if ((Engine.Capabilities & ImapCapabilities.Metadata) == 0)
throw new NotSupportedException ("The IMAP server does not support the METADATA extension.");
var command = new StringBuilder ("SETMETADATA %F (");
var args = new object[metadata.Count * 2 + 1];
int argc = 0;
args[argc++] = this;
for (int i = 0; i < metadata.Count; i++) {
if (i > 0)
command.Append (' ');
command.Append ("%S %S");
args[argc++] = metadata[i].Tag.Id;
args[argc++] = metadata[i].Value;
}
command.Append (")\r\n");
var ic = new ImapCommand (Engine, cancellationToken, null, command.ToString (), args);
Engine.QueueCommand (ic);
Engine.Wait (ic);
ProcessResponseCodes (ic, null);
if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create ("SETMETADATA", ic);
}
示例3: Thread
/// <summary>
/// Threads the messages in the folder that match the search query using the specified threading algorithm.
/// </summary>
/// <remarks>
/// The <see cref="MessageThread.UniqueId"/> can be used with methods such as
/// <see cref="IMailFolder.GetMessage(UniqueId,CancellationToken,ITransferProgress)"/>.
/// </remarks>
/// <returns>An array of message threads.</returns>
/// <param name="uids">The subset of UIDs</param>
/// <param name="algorithm">The threading algorithm to use.</param>
/// <param name="query">The search query.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="algorithm"/> is not supported.
/// </exception>
/// <exception cref="System.ArgumentNullException">
/// <para><paramref name="uids"/> is <c>null</c>.</para>
/// <para>-or-</para>
/// <para><paramref name="query"/> is <c>null</c>.</para>
/// </exception>
/// <exception cref="System.ArgumentException">
/// <para><paramref name="uids"/> is empty.</para>
/// <para>-or-</para>
/// <para>One or more of the <paramref name="uids"/> is invalid.</para>
/// </exception>
/// <exception cref="System.NotSupportedException">
/// <para>One or more search terms in the <paramref name="query"/> are not supported by the IMAP server.</para>
/// <para>-or-</para>
/// <para>The server does not support the THREAD extension.</para>
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// The <see cref="ImapClient"/> has been disposed.
/// </exception>
/// <exception cref="ServiceNotConnectedException">
/// The <see cref="ImapClient"/> is not connected.
/// </exception>
/// <exception cref="ServiceNotAuthenticatedException">
/// The <see cref="ImapClient"/> is not authenticated.
/// </exception>
/// <exception cref="FolderNotOpenException">
/// The <see cref="ImapFolder"/> is not currently open.
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The operation was canceled via the cancellation token.
/// </exception>
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
/// <exception cref="ImapProtocolException">
/// The server's response contained unexpected tokens.
/// </exception>
/// <exception cref="ImapCommandException">
/// The server replied with a NO or BAD response.
/// </exception>
public override IList<MessageThread> Thread (IList<UniqueId> uids, ThreadingAlgorithm algorithm, SearchQuery query, CancellationToken cancellationToken = default (CancellationToken))
{
var method = algorithm.ToString ().ToUpperInvariant ();
var set = ImapUtils.FormatUidSet (uids);
var args = new List<string> ();
string charset;
if ((Engine.Capabilities & ImapCapabilities.Thread) == 0)
throw new NotSupportedException ("The IMAP server does not support the THREAD extension.");
if (!Engine.ThreadingAlgorithms.Contains (algorithm))
throw new ArgumentOutOfRangeException ("algorithm", "The specified threading algorithm is not supported.");
if (query == null)
throw new ArgumentNullException ("query");
CheckState (true, false);
var optimized = query.Optimize (new ImapSearchQueryOptimizer ());
var expr = BuildQueryExpression (optimized, args, out charset);
var command = "UID THREAD " + method + " " + charset + " ";
command += "UID " + set + " " + expr + "\r\n";
var ic = new ImapCommand (Engine, cancellationToken, this, command, args.ToArray ());
ic.RegisterUntaggedHandler ("THREAD", ThreadMatches);
Engine.QueueCommand (ic);
Engine.Wait (ic);
ProcessResponseCodes (ic, null);
if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create ("THREAD", ic);
var threads = (IList<MessageThread>) ic.UserData;
if (threads == null)
return new MessageThread[0];
return threads;
}
示例4: Search
//.........这里部分代码省略.........
/// <para><paramref name="uids"/> is <c>null</c>.</para>
/// <para>-or-</para>
/// <para><paramref name="query"/> is <c>null</c>.</para>
/// <para>-or-</para>
/// <para><paramref name="orderBy"/> is <c>null</c>.</para>
/// </exception>
/// <exception cref="System.ArgumentException">
/// <para>One or more of the <paramref name="uids"/> is invalid.</para>
/// <para>-or-</para>
/// <para><paramref name="orderBy"/> is empty.</para>
/// </exception>
/// <exception cref="System.NotSupportedException">
/// <para>One or more search terms in the <paramref name="query"/> are not supported by the IMAP server.</para>
/// <para>-or-</para>
/// <para>The IMAP server does not support the ESORT extension.</para>
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// The <see cref="ImapClient"/> has been disposed.
/// </exception>
/// <exception cref="ServiceNotConnectedException">
/// The <see cref="ImapClient"/> is not connected.
/// </exception>
/// <exception cref="ServiceNotAuthenticatedException">
/// The <see cref="ImapClient"/> is not authenticated.
/// </exception>
/// <exception cref="FolderNotOpenException">
/// The <see cref="ImapFolder"/> is not currently open.
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The operation was canceled via the cancellation token.
/// </exception>
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
/// <exception cref="ImapProtocolException">
/// The server's response contained unexpected tokens.
/// </exception>
/// <exception cref="ImapCommandException">
/// The server replied with a NO or BAD response.
/// </exception>
public override SearchResults Search (SearchOptions options, IList<UniqueId> uids, SearchQuery query, IList<OrderBy> orderBy, CancellationToken cancellationToken = default (CancellationToken))
{
var set = ImapUtils.FormatUidSet (uids);
var args = new List<string> ();
string charset;
if (query == null)
throw new ArgumentNullException ("query");
if (orderBy == null)
throw new ArgumentNullException ("orderBy");
if (orderBy.Count == 0)
throw new ArgumentException ("No sort order provided.", "orderBy");
CheckState (true, false);
if ((Engine.Capabilities & ImapCapabilities.ESort) == 0)
throw new NotSupportedException ("The IMAP server does not support the ESORT extension.");
if ((Engine.Capabilities & ImapCapabilities.SortDisplay) == 0) {
for (int i = 0; i < orderBy.Count; i++) {
if (orderBy[i].Type == OrderByType.DisplayFrom || orderBy[i].Type == OrderByType.DisplayTo)
throw new NotSupportedException ("The IMAP server does not support the SORT=DISPLAY extension.");
}
}
if (uids.Count == 0)
return new SearchResults ();
var optimized = query.Optimize (new ImapSearchQueryOptimizer ());
var expr = BuildQueryExpression (optimized, args, out charset);
var order = BuildSortOrder (orderBy);
var command = "UID SORT RETURN (";
if ((options & SearchOptions.Count) != 0)
command += "COUNT ";
if ((options & SearchOptions.Min) != 0)
command += "MIN ";
if ((options & SearchOptions.Max) != 0)
command += "MAX ";
command = command.TrimEnd ();
command += ") ";
command += order + " " + charset + " UID " + set + " " + expr + "\r\n";
var ic = new ImapCommand (Engine, cancellationToken, this, command, args.ToArray ());
ic.RegisterUntaggedHandler ("ESEARCH", ESearchMatches);
ic.UserData = new SearchResults ();
Engine.QueueCommand (ic);
Engine.Wait (ic);
ProcessResponseCodes (ic, null);
if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create ("SORT", ic);
return (SearchResults) ic.UserData;
}
示例5: SearchMatches
static void SearchMatches (ImapEngine engine, ImapCommand ic, int index)
{
var results = new SearchResults ();
var uids = new List<UniqueId> ();
ImapToken token;
ulong modseq;
uint uid;
do {
token = engine.PeekToken (ic.CancellationToken);
// keep reading UIDs until we get to the end of the line or until we get a "(MODSEQ ####)"
if (token.Type == ImapTokenType.Eoln || token.Type == ImapTokenType.OpenParen)
break;
token = engine.ReadToken (ic.CancellationToken);
if (token.Type != ImapTokenType.Atom || !uint.TryParse ((string) token.Value, out uid) || uid == 0)
throw ImapEngine.UnexpectedToken (ImapEngine.GenericUntaggedResponseSyntaxErrorFormat, "SEARCH", token);
uids.Add (new UniqueId (ic.Folder.UidValidity, uid));
} while (true);
if (token.Type == ImapTokenType.OpenParen) {
engine.ReadToken (ic.CancellationToken);
do {
token = engine.ReadToken (ic.CancellationToken);
if (token.Type == ImapTokenType.CloseParen)
break;
if (token.Type != ImapTokenType.Atom)
throw ImapEngine.UnexpectedToken (ImapEngine.GenericUntaggedResponseSyntaxErrorFormat, "SEARCH", token);
var atom = (string) token.Value;
switch (atom) {
case "MODSEQ":
token = engine.ReadToken (ic.CancellationToken);
if (token.Type != ImapTokenType.Atom || !ulong.TryParse ((string) token.Value, out modseq)) {
Debug.WriteLine ("Expected 64-bit nz-number as the MODSEQ value, but got: {0}", token);
throw ImapEngine.UnexpectedToken (ImapEngine.GenericItemSyntaxErrorFormat, atom, token);
}
break;
}
token = engine.PeekToken (ic.CancellationToken);
} while (token.Type != ImapTokenType.Eoln);
}
results.UniqueIds = uids;
ic.UserData = results;
}
示例6: Create
/// <summary>
/// Creates a new subfolder with the given name.
/// </summary>
/// <remarks>
/// Creates a new subfolder with the given name.
/// </remarks>
/// <returns>The created folder.</returns>
/// <param name="name">The name of the folder to create.</param>
/// <param name="specialUses">A list of special uses for the folder being created.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="name"/> is <c>null</c>.
/// </exception>
/// <exception cref="System.ArgumentException">
/// <paramref name="name"/> is empty.
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// The <see cref="ImapClient"/> has been disposed.
/// </exception>
/// <exception cref="ServiceNotConnectedException">
/// The <see cref="ImapClient"/> is not connected.
/// </exception>
/// <exception cref="ServiceNotAuthenticatedException">
/// The <see cref="ImapClient"/> is not authenticated.
/// </exception>
/// <exception cref="System.InvalidOperationException">
/// The <see cref="MailFolder.DirectorySeparator"/> is nil, and thus child folders cannot be created.
/// </exception>
/// <exception cref="System.NotSupportedException">
/// The IMAP server does not support the CREATE-SPECIAL-USE extension.
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The operation was canceled via the cancellation token.
/// </exception>
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
/// <exception cref="ImapProtocolException">
/// The server's response contained unexpected tokens.
/// </exception>
/// <exception cref="ImapCommandException">
/// The server replied with a NO or BAD response.
/// </exception>
public override IMailFolder Create (string name, IEnumerable<SpecialFolder> specialUses, CancellationToken cancellationToken = default (CancellationToken))
{
if (name == null)
throw new ArgumentNullException ("name");
if (!Engine.IsValidMailboxName (name, DirectorySeparator))
throw new ArgumentException ("The name is not a legal folder name.", "name");
CheckState (false, false);
if (!string.IsNullOrEmpty (FullName) && DirectorySeparator == '\0')
throw new InvalidOperationException ("Cannot create child folders.");
if ((Engine.Capabilities & ImapCapabilities.CreateSpecialUse) == 0)
throw new NotSupportedException ("The IMAP server does not support the CREATE-SPECIAL-USE extension.");
var uses = new StringBuilder ();
foreach (var use in specialUses) {
if (uses.Length > 0)
uses.Append (' ');
switch (use) {
case SpecialFolder.All: uses.Append ("\\All"); break;
case SpecialFolder.Archive: uses.Append ("\\Archive"); break;
case SpecialFolder.Drafts: uses.Append ("\\Drafts"); break;
case SpecialFolder.Flagged: uses.Append ("\\Flagged"); break;
case SpecialFolder.Junk: uses.Append ("\\Junk"); break;
case SpecialFolder.Sent: uses.Append ("\\Sent"); break;
case SpecialFolder.Trash: uses.Append ("\\Trash"); break;
default: if (uses.Length > 0) uses.Length--; break;
}
}
var fullName = !string.IsNullOrEmpty (FullName) ? FullName + DirectorySeparator + name : name;
var command = string.Format ("CREATE %s (USE ({0}))\r\n", uses);
var encodedName = Engine.EncodeMailboxName (fullName);
var list = new List<ImapFolder> ();
var createName = encodedName;
ImapFolder folder;
var ic = Engine.QueueCommand (cancellationToken, null, command, createName);
Engine.Wait (ic);
ProcessResponseCodes (ic, null);
if (ic.Response != ImapCommandResponse.Ok) {
var useAttr = ic.RespCodes.FirstOrDefault (rc => rc.Type == ImapResponseCodeType.UseAttr);
if (useAttr != null)
throw new ImapCommandException (ic.Response, useAttr.Message);
throw ImapCommandException.Create ("CREATE", ic);
}
ic = new ImapCommand (Engine, cancellationToken, null, "LIST \"\" %S\r\n", encodedName);
//.........这里部分代码省略.........
示例7: GetStream
/// <summary>
/// Gets a substream of the specified message.
/// </summary>
/// <remarks>
/// Fetches a substream of the message. If the starting offset is beyond
/// the end of the message, an empty stream is returned. If the number of
/// bytes desired extends beyond the end of the message, a truncated stream
/// will be returned.
/// </remarks>
/// <returns>The stream.</returns>
/// <param name="uid">The UID of the message.</param>
/// <param name="offset">The starting offset of the first desired byte.</param>
/// <param name="count">The number of bytes desired.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="progress">The progress reporting mechanism.</param>
/// <exception cref="System.ArgumentException">
/// <paramref name="uid"/> is invalid.
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <para><paramref name="offset"/> is negative.</para>
/// <para>-or-</para>
/// <para><paramref name="count"/> is negative.</para>
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// The <see cref="ImapClient"/> has been disposed.
/// </exception>
/// <exception cref="ServiceNotConnectedException">
/// The <see cref="ImapClient"/> is not connected.
/// </exception>
/// <exception cref="ServiceNotAuthenticatedException">
/// The <see cref="ImapClient"/> is not authenticated.
/// </exception>
/// <exception cref="FolderNotOpenException">
/// The <see cref="ImapFolder"/> is not currently open.
/// </exception>
/// <exception cref="MessageNotFoundException">
/// The IMAP server did not return the requested message stream.
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The operation was canceled via the cancellation token.
/// </exception>
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
/// <exception cref="ImapProtocolException">
/// The server's response contained unexpected tokens.
/// </exception>
/// <exception cref="ImapCommandException">
/// The server replied with a NO or BAD response.
/// </exception>
public override Stream GetStream (UniqueId uid, int offset, int count, CancellationToken cancellationToken = default (CancellationToken), ITransferProgress progress = null)
{
if (uid.Id == 0)
throw new ArgumentException ("The uid is invalid.", "uid");
if (offset < 0)
throw new ArgumentOutOfRangeException ("offset");
if (count < 0)
throw new ArgumentOutOfRangeException ("count");
CheckState (true, false);
if (count == 0)
return new MemoryStream ();
var ic = new ImapCommand (Engine, cancellationToken, this, "UID FETCH %u (BODY.PEEK[]<%d.%d>)\r\n", uid.Id, offset, count);
var ctx = new FetchStreamContext (progress);
Stream stream;
ic.RegisterUntaggedHandler ("FETCH", FetchStream);
ic.UserData = ctx;
Engine.QueueCommand (ic);
try {
Engine.Wait (ic);
ProcessResponseCodes (ic, null);
if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create ("FETCH", ic);
if (!ctx.Sections.TryGetValue (string.Empty, out stream))
throw new MessageNotFoundException ("The IMAP server did not return the requested stream.");
ctx.Sections.Remove (string.Empty);
} finally {
ctx.Dispose ();
}
return stream;
}
示例8: Open
/// <summary>
/// Opens the folder using the requested folder access.
/// </summary>
/// <remarks>
/// Opens the folder using the requested folder access.
/// </remarks>
/// <returns>The <see cref="FolderAccess"/> state of the folder.</returns>
/// <param name="access">The requested folder access.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="access"/> is not a valid value.
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// The <see cref="ImapClient"/> has been disposed.
/// </exception>
/// <exception cref="ServiceNotConnectedException">
/// The <see cref="ImapClient"/> is not connected.
/// </exception>
/// <exception cref="ServiceNotAuthenticatedException">
/// The <see cref="ImapClient"/> is not authenticated.
/// </exception>
/// <exception cref="FolderNotFoundException">
/// The <see cref="ImapFolder"/> does not exist.
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The operation was canceled via the cancellation token.
/// </exception>
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
/// <exception cref="ImapProtocolException">
/// The server's response contained unexpected tokens.
/// </exception>
/// <exception cref="ImapCommandException">
/// The server replied with a NO or BAD response.
/// </exception>
public override FolderAccess Open (FolderAccess access, CancellationToken cancellationToken = default (CancellationToken))
{
if (access != FolderAccess.ReadOnly && access != FolderAccess.ReadWrite)
throw new ArgumentOutOfRangeException ("access");
CheckState (false, false);
if (IsOpen && Access == access)
return access;
var condstore = (Engine.Capabilities & ImapCapabilities.CondStore) != 0 ? " (CONDSTORE)" : string.Empty;
var command = string.Format ("{0} %F{1}\r\n", SelectOrExamine (access), condstore);
var ic = new ImapCommand (Engine, cancellationToken, this, command, this);
if (access == FolderAccess.ReadWrite) {
// Note: if the server does not respond with a PERMANENTFLAGS response,
// then we need to assume all flags are permanent.
PermanentFlags = SettableFlags | MessageFlags.UserDefined;
} else {
PermanentFlags = MessageFlags.None;
}
try {
Engine.QueueCommand (ic);
Engine.Wait (ic);
ProcessResponseCodes (ic, this);
if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create (access == FolderAccess.ReadOnly ? "EXAMINE" : "SELECT", ic);
} catch {
PermanentFlags = MessageFlags.None;
throw;
}
if (Engine.Selected != null && Engine.Selected != this) {
var folder = Engine.Selected;
folder.PermanentFlags = MessageFlags.None;
folder.AcceptedFlags = MessageFlags.None;
folder.Access = FolderAccess.None;
folder.OnClosed ();
}
Engine.State = ImapEngineState.Selected;
Engine.Selected = this;
OnOpened ();
return Access;
}
示例9: Fetch
/// <summary>
/// Fetches the message summaries for the specified message UIDs.
/// </summary>
/// <remarks>
/// <para>Fetches the message summaries for the specified message UIDs.</para>
/// <para>It should be noted that if another client has modified any message
/// in the folder, the IMAP server may choose to return information that was
/// not explicitly requested. It is therefore important to be prepared to
/// handle both additional fields on a <see cref="IMessageSummary"/> for
/// messages that were requested as well as summaries for messages that were
/// not requested at all.</para>
/// </remarks>
/// <returns>An enumeration of summaries for the requested messages.</returns>
/// <param name="uids">The UIDs.</param>
/// <param name="items">The message summary items to fetch.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="uids"/> is <c>null</c>.
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="items"/> is empty.
/// </exception>
/// <exception cref="System.ArgumentException">
/// One or more of the <paramref name="uids"/> is invalid.
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// The <see cref="ImapClient"/> has been disposed.
/// </exception>
/// <exception cref="FolderNotOpenException">
/// The <see cref="ImapFolder"/> is not currently open.
/// </exception>
/// <exception cref="ServiceNotConnectedException">
/// The <see cref="ImapClient"/> is not connected.
/// </exception>
/// <exception cref="ServiceNotAuthenticatedException">
/// The <see cref="ImapClient"/> is not authenticated.
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The operation was canceled via the cancellation token.
/// </exception>
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
/// <exception cref="ImapProtocolException">
/// The server's response contained unexpected tokens.
/// </exception>
/// <exception cref="ImapCommandException">
/// The server replied with a NO or BAD response.
/// </exception>
public override IList<IMessageSummary> Fetch (IList<UniqueId> uids, MessageSummaryItems items, CancellationToken cancellationToken = default (CancellationToken))
{
var set = ImapUtils.FormatUidSet (uids);
if (items == MessageSummaryItems.None)
throw new ArgumentOutOfRangeException ("items");
CheckState (true, false);
if (uids.Count == 0)
return new IMessageSummary[0];
var query = FormatSummaryItems (ref items, null);
var command = string.Format ("UID FETCH {0} {1}\r\n", set, query);
var ic = new ImapCommand (Engine, cancellationToken, this, command);
var ctx = new FetchSummaryContext (items);
ic.RegisterUntaggedHandler ("FETCH", FetchSummaryItems);
ic.UserData = ctx;
Engine.QueueCommand (ic);
Engine.Wait (ic);
ProcessResponseCodes (ic, null);
if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create ("FETCH", ic);
return AsReadOnly (ctx.Results.Values);
}
示例10: FetchSummaryItems
void FetchSummaryItems (ImapEngine engine, ImapCommand ic, int index)
{
var token = engine.ReadToken (ic.CancellationToken);
if (token.Type != ImapTokenType.OpenParen)
throw ImapEngine.UnexpectedToken (ImapEngine.GenericUntaggedResponseSyntaxErrorFormat, "FETCH", token);
var ctx = (FetchSummaryContext) ic.UserData;
IMessageSummary isummary;
MessageSummary summary;
if (!ctx.Results.TryGetValue (index, out isummary)) {
summary = new MessageSummary (index);
ctx.Results.Add (index, summary);
} else {
summary = (MessageSummary) isummary;
}
do {
token = engine.ReadToken (ic.CancellationToken);
if (token.Type == ImapTokenType.CloseParen || token.Type == ImapTokenType.Eoln)
break;
if (token.Type != ImapTokenType.Atom)
throw ImapEngine.UnexpectedToken (ImapEngine.GenericUntaggedResponseSyntaxErrorFormat, "FETCH", token);
var atom = (string) token.Value;
string format;
ulong value64;
uint value;
int idx;
switch (atom) {
case "INTERNALDATE":
token = engine.ReadToken (ic.CancellationToken);
switch (token.Type) {
case ImapTokenType.QString:
case ImapTokenType.Atom:
summary.InternalDate = ImapUtils.ParseInternalDate ((string) token.Value);
break;
case ImapTokenType.Nil:
summary.InternalDate = null;
break;
default:
throw ImapEngine.UnexpectedToken (ImapEngine.GenericItemSyntaxErrorFormat, atom, token);
}
summary.Fields |= MessageSummaryItems.InternalDate;
break;
case "RFC822.SIZE":
token = engine.ReadToken (ic.CancellationToken);
if (token.Type != ImapTokenType.Atom || !uint.TryParse ((string) token.Value, out value))
throw ImapEngine.UnexpectedToken (ImapEngine.GenericItemSyntaxErrorFormat, atom, token);
summary.Fields |= MessageSummaryItems.MessageSize;
summary.Size = value;
break;
case "BODYSTRUCTURE":
format = string.Format (ImapEngine.GenericItemSyntaxErrorFormat, "BODYSTRUCTURE", "{0}");
summary.Body = ImapUtils.ParseBody (engine, format, string.Empty, ic.CancellationToken);
summary.Fields |= MessageSummaryItems.BodyStructure;
break;
case "BODY":
token = engine.PeekToken (ic.CancellationToken);
if (token.Type == ImapTokenType.OpenBracket) {
// consume the '['
token = engine.ReadToken (ic.CancellationToken);
if (token.Type != ImapTokenType.OpenBracket)
throw ImapEngine.UnexpectedToken (ImapEngine.GenericItemSyntaxErrorFormat, atom, token);
// References and/or other headers were requested...
do {
token = engine.ReadToken (ic.CancellationToken);
if (token.Type == ImapTokenType.CloseBracket)
break;
if (token.Type == ImapTokenType.OpenParen) {
do {
token = engine.ReadToken (ic.CancellationToken);
if (token.Type == ImapTokenType.CloseParen)
break;
// the header field names will generally be atoms or qstrings but may also be literals
switch (token.Type) {
case ImapTokenType.Literal:
engine.ReadLiteral (ic.CancellationToken);
break;
case ImapTokenType.QString:
case ImapTokenType.Atom:
break;
default:
throw ImapEngine.UnexpectedToken (ImapEngine.GenericItemSyntaxErrorFormat, atom, token);
//.........这里部分代码省略.........
示例11: QueueMultiAppend
ImapCommand QueueMultiAppend (FormatOptions options, IList<MimeMessage> messages, IList<MessageFlags> flags, IList<DateTimeOffset> dates, CancellationToken cancellationToken, ITransferProgress progress)
{
var args = new List<object> ();
string format = "APPEND %F";
args.Add (this);
for (int i = 0; i < messages.Count; i++) {
if ((flags[i] & SettableFlags) != 0)
format += " " + ImapUtils.FormatFlagsList (flags[i], 0);
if (dates != null)
format += " \"" + ImapUtils.FormatInternalDate (dates[i]) + "\"";
format += " %L";
args.Add (messages[i]);
}
format += "\r\n";
var ic = new ImapCommand (Engine, cancellationToken, null, options, format, args.ToArray ());
ic.Progress = progress;
Engine.QueueCommand (ic);
return ic;
}
示例12: QueueAppend
ImapCommand QueueAppend (FormatOptions options, MimeMessage message, MessageFlags flags, DateTimeOffset? date, CancellationToken cancellationToken, ITransferProgress progress)
{
string format = "APPEND %F";
if ((flags & SettableFlags) != 0)
format += " " + ImapUtils.FormatFlagsList (flags, 0);
if (date.HasValue)
format += " \"" + ImapUtils.FormatInternalDate (date.Value) + "\"";
format += " %L\r\n";
var ic = new ImapCommand (Engine, cancellationToken, null, options, format, this, message);
ic.Progress = progress;
Engine.QueueCommand (ic);
return ic;
}
示例13: QResyncFetch
static void QResyncFetch (ImapEngine engine, ImapCommand ic, int index)
{
ic.Folder.OnFetch (engine, index, ic.CancellationToken);
}
示例14: GetMessage
/// <summary>
/// Gets the specified message.
/// </summary>
/// <remarks>
/// Gets the specified message.
/// </remarks>
/// <returns>The message.</returns>
/// <param name="index">The index of the message.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="progress">The progress reporting mechanism.</param>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="index"/> is out of range.
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// The <see cref="ImapClient"/> has been disposed.
/// </exception>
/// <exception cref="ServiceNotConnectedException">
/// The <see cref="ImapClient"/> is not connected.
/// </exception>
/// <exception cref="ServiceNotAuthenticatedException">
/// The <see cref="ImapClient"/> is not authenticated.
/// </exception>
/// <exception cref="FolderNotOpenException">
/// The <see cref="ImapFolder"/> is not currently open.
/// </exception>
/// <exception cref="MessageNotFoundException">
/// The IMAP server did not return the requested message.
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The operation was canceled via the cancellation token.
/// </exception>
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
/// <exception cref="ImapProtocolException">
/// The server's response contained unexpected tokens.
/// </exception>
/// <exception cref="ImapCommandException">
/// The server replied with a NO or BAD response.
/// </exception>
public override MimeMessage GetMessage (int index, CancellationToken cancellationToken = default (CancellationToken), ITransferProgress progress = null)
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException ("index");
CheckState (true, false);
var ic = new ImapCommand (Engine, cancellationToken, this, "FETCH %d (BODY.PEEK[])\r\n", index + 1);
var ctx = new FetchStreamContext (progress);
Stream stream;
ic.RegisterUntaggedHandler ("FETCH", FetchStream);
ic.UserData = ctx;
Engine.QueueCommand (ic);
try {
Engine.Wait (ic);
ProcessResponseCodes (ic, null);
if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create ("FETCH", ic);
if (!ctx.Sections.TryGetValue (string.Empty, out stream))
throw new MessageNotFoundException ("The IMAP server did not return the requested message.");
ctx.Sections.Remove (string.Empty);
} finally {
ctx.Dispose ();
}
return ParseMessage (stream, cancellationToken);
}
示例15: GetBodyPart
/// <summary>
/// Gets the specified body part.
/// </summary>
/// <remarks>
/// Gets the specified body part.
/// </remarks>
/// <returns>The body part.</returns>
/// <param name="index">The index of the message.</param>
/// <param name="partSpecifier">The body part specifier.</param>
/// <param name="headersOnly"><c>true</c> if only the headers should be downloaded; otherwise, <c>false</c>></param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="progress">The progress reporting mechanism.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="partSpecifier"/> is <c>null</c>.
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="index"/> is out of range.
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// The <see cref="ImapClient"/> has been disposed.
/// </exception>
/// <exception cref="ServiceNotConnectedException">
/// The <see cref="ImapClient"/> is not connected.
/// </exception>
/// <exception cref="ServiceNotAuthenticatedException">
/// The <see cref="ImapClient"/> is not authenticated.
/// </exception>
/// <exception cref="FolderNotOpenException">
/// The <see cref="ImapFolder"/> is not currently open.
/// </exception>
/// <exception cref="MessageNotFoundException">
/// The IMAP server did not return the requested message.
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The operation was canceled via the cancellation token.
/// </exception>
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
/// <exception cref="ImapProtocolException">
/// The server's response contained unexpected tokens.
/// </exception>
/// <exception cref="ImapCommandException">
/// The server replied with a NO or BAD response.
/// </exception>
public MimeEntity GetBodyPart (int index, string partSpecifier, bool headersOnly, CancellationToken cancellationToken = default (CancellationToken), ITransferProgress progress = null)
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException ("index");
if (partSpecifier == null)
throw new ArgumentNullException ("partSpecifier");
CheckState (true, false);
string[] tags;
var command = string.Format ("FETCH {0} ({1})\r\n", index + 1, GetBodyPartQuery (partSpecifier, headersOnly, out tags));
var ic = new ImapCommand (Engine, cancellationToken, this, command);
var ctx = new FetchStreamContext (progress);
ChainedStream chained;
bool dispose = false;
Stream stream;
ic.RegisterUntaggedHandler ("FETCH", FetchStream);
ic.UserData = ctx;
Engine.QueueCommand (ic);
try {
Engine.Wait (ic);
ProcessResponseCodes (ic, null);
if (ic.Response != ImapCommandResponse.Ok)
throw ImapCommandException.Create ("FETCH", ic);
chained = new ChainedStream ();
foreach (var tag in tags) {
if (!ctx.Sections.TryGetValue (tag, out stream))
throw new MessageNotFoundException ("The IMAP server did not return the requested body part.");
if (!(stream is MemoryStream || stream is MemoryBlockStream))
dispose = true;
chained.Add (stream);
}
foreach (var tag in tags)
ctx.Sections.Remove (tag);
} finally {
ctx.Dispose ();
}
var entity = ParseEntity (chained, dispose, cancellationToken);
if (partSpecifier.Length == 0) {
for (int i = entity.Headers.Count; i > 0; i--) {
var header = entity.Headers[i - 1];
//.........这里部分代码省略.........