4.1. My typical VB.NET desktop application. Parse message body by MailKit/MimeKit and save attachment.
This description is continue to my articles MailKit/MimeKit - best eMail client. and ASP.NET Core Backend with MailKit/IMAP (Async/Await, Monitor, ConcurrentQueue, CancellationTokenSource).. In this page I describe how to save attachment and open it.
In this case I read mailbody from DB:
by this way:
151: Dim RawBody = db1.messagees.Where(Function(X) X.idmessagee = idmessageeTextBox.Text).Select(Function(X) X.rawbody).FirstOrDefault
152: If RawBody IsNot Nothing Then
153: Dim Buf() As Byte = System.Text.Encoding.UTF8.GetBytes(RawBody)
154: Dim ByteStream As IO.MemoryStream = New IO.MemoryStream(Buf)
155: 'Dim Rdr As IO.StreamReader = New IO.StreamReader(ByteStream)
156: 'Dim Txt As String = Rdr.ReadToEnd
157: Dim MultipartBodyMessage As MimeKit.MimeMessage = MimeKit.MimeMessage.Load(ByteStream)
158: If MultipartBodyMessage.HtmlBody IsNot Nothing Then
159: HtmlPanel.Text = MultipartBodyMessage.HtmlBody
160: ElseIf MultipartBodyMessage.TextBody IsNot Nothing Then
161: HtmlPanel.Text = MultipartBodyMessage.TextBody
162: Else
163: HtmlPanel.Text = ""
164: End If
165: MailHeaders = MultipartBodyMessage.Headers
166: MailAttachments = MultipartBodyMessage.Attachments
167: FillAttachmentListListView(MultipartBodyMessage.Attachments)
And save link to attachment to this variable.
199: Dim MailAttachments As IEnumerable(Of MimeKit.MimeEntity)
Line 153-154 is converting string to stream, and 155-156 is the same converting to opposite derection - this is small reminder for me.
FillAttachmentListListView in line 167 call to fill data to ListView
221: Sub FillAttachmentListListView(Attachments As IEnumerable(Of MimeKit.MimeEntity))
222: AttachmentListListView.Items.Clear()
223: If Attachments.Count = 0 Then
224: AttachmentListListView.BackColor = SystemColors.Control
225: Else
226: AttachmentListListView.BackColor = SystemColors.Window
227: For Each One As MimeKit.MimePart In Attachments
228: If One.IsAttachment Then
229: Dim AttItem As New ListViewItem(One.FileName, 0)
230: AttItem.ToolTipText = One.ContentType.ToString
231: AttachmentListListView.Items.Add(AttItem)
232: End If
233: Next
234: 'AttachmentListListView.Columns(AttachmentListListView.Columns.Count - 1).Width = -2
235: AttachmentListListView.Refresh()
236: End If
237: End Sub
And click to attachment handle to copy it to tamp directory and open it.
200: Private Sub AttachmentListListView_Click(sender As Object, e As EventArgs) Handles AttachmentListListView.Click
201: If AttachmentListListView.SelectedItems.Count > 0 Then
202: Dim SelectedAtt As String = AttachmentListListView.SelectedItems(0).Text
203: For Each One In MailAttachments
204: If CType(One, MimeKit.MimePart).FileName = SelectedAtt Then
205: Dim LocalFileName = IO.Path.Combine(MainMDI_Instance.TempAppDirectory & SelectedAtt)
206: Using Stream = IO.File.Create(LocalFileName)
207: If TypeOf One Is MimeKit.MessagePart Then
208: Dim rfc822 = CType(One, MimeKit.MessagePart)
209: rfc822.Message.WriteTo(Stream)
210: Else
211: Dim part = CType(One, MimeKit.MimePart)
212: part.Content.DecodeTo(Stream)
213: End If
214: Process.Start(LocalFileName)
215: End Using
216: End If
217: Next
218: End If
219: End Sub
In line 205 I use extension TempAppDirectory
102: <Extension()>
103: Function GetTempDirectory(F As Form) As String
104: Dim LocalTmpPath As String = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
105: Dim TempAppDirectory As String = IO.Path.Combine(LocalTmpPath, Application.ProductName)
106: If Not My.Computer.FileSystem.DirectoryExists(TempAppDirectory) Then
107: My.Computer.FileSystem.CreateDirectory(TempAppDirectory)
108: End If
109: Return TempAppDirectory & "\"
110: End Function
111:
Comments (
)
Link to this page:
//www.vb-net.com/Samantha/Attachment.htm
|