(MVC) MVC (2018)

MailKit/MimeKit - best eMail client.

In the past there was many good eMailClient - MailSystem.NET

, imapx, S22, aenetmail, but in my now opinion there are only one best eMail client - MailKit/MimeKit, https://www.nuget.org/packages/MailKit/. This is common documentation of this library http://www.mimekit.net/docs/html/Introduction.htm.

And below you can see mycode template to work with this library.


IMAP client, to receive eMail by IMAP.



This is a special client to receive two type of eMail: first type - with attachment (line 34-47), second type - simple mail (line 50-51).


   1:  Imports MailKit
   2:  Imports MailKit.Net.Imap
   3:  Imports MailKit.Net.Smtp
   4:  Imports MimeKit
   5:   
   6:  Public Delegate Sub ShowProcess(ProgressTXT As String)
   7:   
   8:  Public Class MyMailClient
   9:      Property Server As String
  10:      Property Port As Integer
  11:      Property Login As String
  12:      Property Password As String
  13:   
  14:      Public Sub New(Server1 As String, Port1 As Integer, Login1 As String, Password1 As String)
  15:          Server = Server1
  16:          Port = Port1
  17:          Login = Login1
  18:          Password = Password1
  19:      End Sub
  20:   
  21:      Public Sub SaveMessageFromInbox(Directory As String, FromDate As Date, ShowProcess As ShowProcess, DeleteAfterReaded As Boolean)
  22:          ShowProcess.Invoke("Start: " & Server)
  23:          Try
  24:              Using client = New ImapClient()
  25:                  client.ServerCertificateValidationCallback = Function(s, c, h, e) True
  26:                  client.Connect(Server, Port, True)
  27:                  client.Authenticate(Login, Password)
  28:                  Dim Inbox = client.Inbox
  29:                  Inbox.Open(FolderAccess.ReadWrite)
  30:                  For i As Integer = 0 To Inbox.Count - 1
  31:                      Dim Message = Inbox.GetMessage(i)
  32:                      If Message.Date >= FromDate Then
  33:                          If Message.Subject.Contains("Docket Alert:") Then
  34:                              ShowProcess.Invoke(Left(Message.Subject, 50))
  35:                              If Message.Attachments.Count > 0 Then
  36:                                  For Each Attachment As MimeEntity In Message.Attachments
  37:                                      Dim fileName = System.IO.Path.Combine(Directory, "Westlaw." & Attachment.ContentId & ".csv")
  38:                                      Using Stream = System.IO.File.Create(fileName)
  39:                                          If TypeOf Attachment Is MessagePart Then
  40:                                              Dim rfc822 = CType(Attachment, MessagePart)
  41:                                              rfc822.Message.WriteTo(Stream)
  42:                                          Else
  43:                                              Dim part = CType(Attachment, MimePart)
  44:                                              part.Content.DecodeTo(Stream)
  45:                                          End If
  46:                                      End Using
  47:                                  Next
  48:                              End If
  49:                          ElseIf Message.Subject.Contains("UniCourt Data Delivery") Then
  50:                              ShowProcess.Invoke(Left(Message.Subject, 50))
  51:                              My.Computer.FileSystem.WriteAllText(System.IO.Path.Combine(Directory, "UniCourt." & Message.MessageId & ".txt"), Message.TextBody, False)
  52:                          End If
  53:                      End If
  54:                      If DeleteAfterReaded Then
  55:                          Inbox.AddFlags(i, MessageFlags.Deleted, True)
  56:                      End If
  57:                  Next
  58:                  If DeleteAfterReaded Then
  59:                      client.Inbox.Expunge()
  60:                  End If
  61:                  client.Disconnect(True)
  62:                  ShowProcess.Invoke("Done: " & Server)
  63:              End Using
  64:          Catch ex As Exception
  65:              MsgBox(Server & vbCrLf & ex.Message)
  66:          End Try
  67:      End Sub

This is a way to execute this code. Another way to process email in many mailbox, please, see in page - BackgroundWorkerQueue. And pay attention. this is simple way without subscribing to mailbox changing.


 139:      Dim WithEvents BGW1 As BackgroundWorker
 140:      Dim BGW1_Prm As New Object
 141:      Dim WithEvents BGW2 As BackgroundWorker
 142:      Dim BGW2_Prm As New Object
 143:   
 144:      Private Sub ReadEmailButton_Click(sender As Object, e As EventArgs) Handles ReadEmailButton.Click
 145:          BGW1 = New BackgroundWorker
 146:          BGW1_Prm = New With {.Server = ServerTextBox1.Text, .Port = PortNumericUpDown1.Value, .Login = LoginTextBox1.Text, .Pass = PasswordTextBox1.Text, .DeleteAfterReaded = DeleteMailCheckBox.Checked}
 147:          BGW1.RunWorkerAsync(BGW1_Prm)
 148:      End Sub
 149:   
 150:      Private Sub BGW1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BGW1.DoWork
 151:          Dim Box1 As New MyMailClient(e.Argument.Server, e.Argument.Port, e.Argument.Login, e.Argument.Pass)
 152:          Box1.SaveMessageFromInbox(SelectedTempPath, DateTimePickerBox1.Value, AddressOf UpdateLabel, e.Argument.DeleteAfterReaded)
 153:      End Sub
 154:   
 155:      Private Sub BGW1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BGW1.RunWorkerCompleted
 156:          BGW2 = New BackgroundWorker
 157:          BGW2_Prm = New With {.Server = ServerTextBox2.Text, .Port = PortNumericUpDown2.Value, .Login = LoginTextBox2.Text, .Pass = PasswordTextBox2.Text, .DeleteAfterReaded = DeleteMailCheckBox.Checked}
 158:          BGW2.RunWorkerAsync(BGW2_Prm)
 159:      End Sub
 160:   
 161:      Private Sub BGW2_DoWork(sender As Object, e As DoWorkEventArgs) Handles BGW2.DoWork
 162:          Dim Box1 As New MyMailClient(e.Argument.Server, e.Argument.Port, e.Argument.Login, e.Argument.Pass)
 163:          Box1.SaveMessageFromInbox(SelectedTempPath, DateTimePickerBox2.Value, AddressOf UpdateLabel, e.Argument.DeleteAfterReaded)
 164:      End Sub
 165:   
 166:      Private Sub BGW2_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BGW2.RunWorkerCompleted
 ...:   

And this is a result of working this code.



Code template to send eMail by SMPT.



Commented lines is a alternative way to include attachment to mail body.


  70:      Public Sub SendMail(SendTo As String, Subject As String, Body As String, Optional AttachmentFileName As String = "")
  71:          Try
  72:              Using client = New SmtpClient()
  73:                  client.ServerCertificateValidationCallback = Function(s, c, h, e) True
  74:                  client.Connect(Server, Port, True)
  75:                  client.Authenticate(Login, Password)
  76:                  '
  77:                  Dim Message = New MimeMessage()
  78:                  Message.From.Add(New MailboxAddress(Login))
  79:                  Message.To.Add(New MailboxAddress(SendTo))
  80:                  Message.Subject = Subject
  81:                  Dim builder = New BodyBuilder()
  82:                  builder.TextBody = Body
  83:                  builder.Attachments.Add(AttachmentFileName)
  84:                  Message.Body = builder.ToMessageBody()
  85:                  '
  86:                  'Dim TxtBody = New TextPart(Body)
  87:                  'Dim Attachment = New MimePart("application/vnd.ms-excel")
  88:                  'Attachment.Content = New MimeContent(System.IO.File.OpenRead(AttachmentFileName))
  89:                  'Attachment.ContentDisposition = New ContentDisposition(ContentDisposition.Attachment)
  90:                  'Attachment.ContentTransferEncoding = ContentEncoding.Base64
  91:                  'Attachment.FileName = System.IO.Path.GetFileName(AttachmentFileName)
  92:                  'Dim Multipart = New Multipart("mixed")
  93:                  'Multipart.Add(TxtBody)
  94:                  'Multipart.Add(Attachment)
  95:                  'Message.Body = Multipart
  96:   
  97:                  client.Send(Message)
  98:                  client.Disconnect(True)
  99:              End Using
 100:          Catch ex As Exception
 101:              MsgBox(Server & vbCrLf & ex.Message)
 102:          End Try
 103:   
 104:      End Sub
 105:   
 106:  End Class

Execution is doing in the same way.


 554:      Dim WithEvents BGW4 As BackgroundWorker
 555:      Dim BGW4_Prm As New Object
 556:      Dim UnicourtAttachmentFileName As String
 557:      Private Sub SendResultButton_Click(sender As Object, e As EventArgs) Handles SendResultButton.Click
 558:          BGW4 = New BackgroundWorker
 559:          BGW4_Prm = New With {.Server = ServerTextBox3.Text, .Port = PortNumericUpDown3.Value, .Login = LoginTextBox3.Text, .Pass = PasswordTextBox3.Text, .SendTo = SendResultToTextBox.Text, .AttachmentFileName = UnicourtAttachmentFileName, .Subject = ResultSubjectTextBox.Text}
 560:          BGW4.RunWorkerAsync(BGW4_Prm)
 561:      End Sub
 562:   
 563:      Private Sub BGW4_DoWork(sender As Object, e As DoWorkEventArgs) Handles BGW4.DoWork
 564:          Dim Box1 As New MyMailClient(e.Argument.Server, e.Argument.Port, e.Argument.Login, e.Argument.Pass)
 565:          Box1.SendMail(e.Argument.SendTo, e.Argument.Subject, "", e.Argument.AttachmentFileName)
 566:      End Sub
 567:   
 568:      Private Sub BGW4_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BGW4.RunWorkerCompleted



Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19
Link to this page: http://www.vb-net.com/eMailClient/index.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <MAIL ME>  <ABOUT ME>  < THANKS ME>