Dart.Mail Namespace > ImapMessage Class : GetPart Method |
Dim instance As ImapMessage Dim part As Part Dim peek As Boolean Dim value As Stream value = instance.GetPart(part, peek)
Exception | Description |
---|---|
ProtocolException | Bad IMAP protocol response received from server. |
System.Net.Sockets.SocketException | A communications failure has occurred. |
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.
/// <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