Advertisement.

TransHimalaya.in

Everest Helicopter Tour for a day

Book exciting packages at www.transhimalaya.in

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.
Thanks a Loot.

Most Viewed Post

solution for facebook static.ak.fbcdn.net 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

Google+ Followers