Recieve email with attachment using VB.Net and

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
        Dim counter As Integer = 0
        Dim i As Integer = count
        Do While (i >= 1)
            Dim message As Message = pop3Client.GetMessage(i)
            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 ()
            '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)

            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
        gvMail.DataSource = dtMessages
        ' gvMail.DataBindings 
    End Sub
End Class

- Vinod Kotiya


danielko said…
Thank You. Nice piece of code. It helps a lot.
Thanks a Loot.
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

Anyone can help me?

Most Viewed Post

solution for facebook waiting

Connect Sony XPLOD MEX-BT2500 Bluetooth CD Receiver with ur mobile

Crystal Report : Show data horizontally (Left to right) i.e. columns as rows