Recieve email with attachment using VB.Net and openpop.net

Grabbing the body of the e-mail message is a little bit trying. You need to actually grab the MessagePart (in the e-mail address we used the MessageHeader). Once you have the MessagePart you can then access the following methods .FindFirstPlainTextVersion() and .FindFirstHtmlVersion(), plain text and HTML respectively.
First you will need to test whether the e-mail has an attachment. If it does we need to set up a situation where we can loop through all the attachments, in case there is more than one. Below is a bit of code that will save the attachments to a folder on the local drive

Imports OpenPop.Pop3
Imports OpenPop.Mime
Imports System.Data
Public Class Form1

    Private Sub btnGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGet.Click
        Dim pop3Client As Pop3Client

        pop3Client = New Pop3Client
        pop3Client.Connect(txtServer.Text, Integer.Parse(txtPort.Text), cbSSL.Checked)
        pop3Client.Authenticate(txtUser.Text, txtPwd.Text)


        Dim count As Integer = pop3Client.GetMessageCount
        Label1.Text = "Total Emails: " & count.ToString
        '  Exit Sub
        Dim dtMessages As DataTable = New DataTable
        dtMessages.Columns.Add("MessageNumber")
        dtMessages.Columns.Add("From")
        dtMessages.Columns.Add("Subject")
        dtMessages.Columns.Add("DateSent")
        dtMessages.Columns.Add("Recvd")
        dtMessages.Columns.Add("Attachcount")
        Dim counter As Integer = 0
        Dim i As Integer = count
        Do While (i >= 1)
            Dim message As Message = pop3Client.GetMessage(i)
            dtMessages.Rows.Add()
            dtMessages.Rows((dtMessages.Rows.Count - 1))("MessageNumber") = i
            dtMessages.Rows((dtMessages.Rows.Count - 1))("From") = message.Headers.From.Address
            dtMessages.Rows((dtMessages.Rows.Count - 1))("Subject") = message.Headers.Subject
            dtMessages.Rows((dtMessages.Rows.Count - 1))("DateSent") = message.Headers.DateSent
            dtMessages.Rows((dtMessages.Rows.Count - 1))("Recvd") = message.Headers.Date
            dtMessages.Rows((dtMessages.Rows.Count - 1))("Attachcount") = message.FindAllAttachments.Count
            ' Get Body of email
            'message.FindFirstPlainTextVersion.GetBodyAsText ()
            'message.FindFirstHtmlVersion.GetBodyAsText()
            'Download attachment
            For Each msgpart As MessagePart In message.FindAllAttachments
                Dim thefile = msgpart.FileName
                Dim filetype = msgpart.ContentType
                Dim contentid = msgpart.ContentId
                System.IO.File.WriteAllBytes(Application.StartupPath() & "\" & thefile, msgpart.Body)
            Next

            counter = counter + 1
            i = i - 1
            'anyways i dont want to process more than 5 mails at a time
            If counter = 5 Then
                Exit Do
            End If
        Loop
        gvMail.DataSource = dtMessages
        ' gvMail.DataBindings 
       
    End Sub
End Class

- Vinod Kotiya

Comments

danielko said…
Thank You. Nice piece of code. It helps a lot.
Unknown said…
Thanks a Loot.
Unknown said…
I have a issue to download .txt and .csv file using this piece of code.

If I download .txt file from MS Outlook for example, it open correctly as below:
180201141539,9BGJC6920JB232117,BRANCO ,5C692J
180201141857,9BGKL48U0JB231849,PRATA ,5L48UJ

If I download and save with this piece of code, the file saved has the following content:
begin 600 FILE_00820180201151002.csv
M,3@P,C`Q,34P-S,W+#DS0T-,.#!#-DI",C,R,#8R+$)204Y#3R`@+#5,.#!#
#2@T*
`end

Anyone can help me?

Popular Posts