r/visualbasic • u/[deleted] • May 21 '21
Help displaying html email using mailkit
I've been working on an email client to connect to IMAP servers and so far have been successful getting a connection and displaying the emails in my inbox. I'd like to display emails using a web view 2 and have been looking at the documentation for 2 days straight now and have been unable to translate the C# code. So far, I've gotten connection, authentication and fetching and displaying the list of emails in my inbox in a list box.
Here is my code so far:
Imports System
Imports MimeKit
Imports MailKit
Imports MailKit.Net.Imap
Imports MailKit.Security
Imports MailKit.Search
Imports System.Threading
Public Class ReceiveEmails
Dim HasAttachment As Boolean = False
Dim aryAttachments() As String
Private Sub btnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click
'Dim connectThread As New Thread(AddressOf Login)
'connectThread.Start()
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
Me.Hide()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'normally a thread or background worker cannot update the UI, but the line below is a cheap way to accomplish this task
'not the ideal way to update the UI apparently, but it works
CheckForIllegalCrossThreadCalls = False
Dim username As String = txtAccount.Text.ToString
Dim password As String = txtPassword.Text
Dim imapServerName As String = cmbServer.SelectedItem
Dim imapServer As New ImapClient(New ProtocolLogger("imap.log"))
Dim i As Integer = 0
imapServer.Connect(imapServerName, 993, SecureSocketOptions.SslOnConnect)
imapServer.Authenticate(username, password)
imapServer.Inbox.Open(FolderAccess.ReadOnly)
Dim emailMessage = imapServer.Inbox.GetMessage(i)
For count As Integer = 0 To imapServer.Inbox.Count - 1
emailMessage = imapServer.Inbox.GetMessage(count)
'email subjects get listed in a list box
listEmails.Items.Add(emailMessage.Subject)
Next
Dim query = SearchQuery.SubjectContains("")
Dim uids = imapServer.Inbox.Search(query)
Dim items = imapServer.Inbox.Fetch(uids, MessageSummaryItems.UniqueId)
End Sub
End ClassI want the background worker to fetch an email based on the list box's selected index, format any html and display it in the web view.
5
Upvotes
1
u/RJPisscat May 22 '21
I had to look up CheckForIllegalCrossThreadCalls. NO NOT USE.
You want your BackgroundWorker to be in an instance of a Class that contains info about the data to put into the ListBox. The BackgroundWorker will store data, call ReportProgress with the userrState object set to the instance of the Class, and the invoking thread (the one with the Listbox) will update the UI.
Look up BackgroundWorker.ReportProgress in Bing for code examples of how to set up all of this. You want the 2-arg method of ReportProgress.