Gets a single part of a message.
Parameters
- part
- The message part to retrieve.
- peek
- Use true if the message Seen flag should not be set to true.
Return Value
A Stream containing the part data.
This example demonstrates how to download the attachments of a message, without downloading the entire message.
/// <summary>
/// Downloads all attachments from the first email that contains attachments.
/// </summary>
/// <param name="myImap">A connected and authenticated Imap instance, that has a mailbox selected.</param>
/// <param name="saveDir">Path of the directory to save the attachments in.</param>
public void GetMessageAttachments(Imap myImap, string saveDir)
{
//Discover attachments by retrieving email BodyStructure,
//then download the attachments using GetPart()
foreach (ImapMessage imapMessage in myImap.SelectedMailbox.ToArray())
{
imapMessage.Get(ImapMessageInfo.Structure);
if (imapMessage.Message.Attachments.Count > 0)
{
foreach (Attachment attachment in imapMessage.Message.Attachments)
{
imapMessage.GetPart(attachment, false);
attachment.Content.MoveTo(Path.Combine(saveDir, attachment.FileName));
}
break;
}
}
}
''' <summary>
''' Downloads all attachments from the first email that contains attachments.
''' </summary>
''' <param name="myImap">A connected and authenticated Imap instance, that has a mailbox selected.</param>
''' <param name="saveDir">Path of the directory to save the attachments in.</param>
Public Sub GetMessageAttachments(ByVal myImap As Imap, ByVal saveDir As String)
'Discover attachments by retrieving email BodyStructure,
'then download the attachments using GetPart()
For Each imapMessage As ImapMessage In myImap.SelectedMailbox.ToArray()
imapMessage.Get(ImapMessageInfo.Structure)
If imapMessage.Message.Attachments.Count > 0 Then
For Each attachment As Attachment In imapMessage.Message.Attachments
imapMessage.GetPart(attachment, False)
attachment.Content.MoveTo(Path.Combine(saveDir, attachment.FileName))
Next attachment
Exit For
End If
Next imapMessage
End Sub