PowerTCP Mail for .NET
GetPart Method
Example 




The message part to retrieve.
Use true if the message Seen flag should not be set to true.
Gets a single part of a message.
Syntax
Public Function GetPart( _
   ByVal part As Part, _
   ByVal peek As Boolean _
) As Stream
Dim instance As ImapMessage
Dim part As Part
Dim peek As Boolean
Dim value As Stream
 
value = instance.GetPart(part, peek)
public Stream GetPart( 
   Part part,
   bool peek
)
public: Stream* GetPart( 
   Part* part,
   bool peek
) 
public:
Stream^ GetPart( 
   Part^ part,
   bool peek
) 

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.
Exceptions
ExceptionDescription
ProtocolExceptionBad IMAP protocol response received from server.
System.Net.Sockets.SocketExceptionA communications failure has occurred.
Remarks

This method gets a single part of a message. part must be from the MailMessage.Content collection. This method can be used to download a message attachment without retrieving the entire message.

Typically, ImapMessage.Get(ImapMessageInfo.Structure) is called prior to this method to create a MailMessage with a Content collection containing empty Part objects. One of these parts is passed to this method.

peek indicates whether this operation should mark the message as seen; if peek is true, the Seen flag is not changed. If peek is false, the Seen flag is set to true.

Example
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
See Also

Reference

ImapMessage Class
ImapMessage Members


PowerTCP Mail for .NET Documentation Version 4.3
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic