Click here to Skip to main content
16,016,882 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Can anyone help me with vb code to retrieve email attachments with pop3. I manage to read the mails, but not to download and save any attachments. (i am not looking for dll's that are under purchase license)

> i need vb code (and i do not know how to change C#-code into vb... -> it's sergey who suggested sollutions in C#)
Posted
Updated 17-Sep-13 5:37am
v4
Comments
[no name] 16-Sep-13 14:59pm    
Not sure what help it is that you are looking for or what C code has to do with anything.
http://www.bing.com/search?q=vb.net+pop3+email+attachments
[no name] 17-Sep-13 11:28am    
"its sergei who suggested sollutions in C"... no. First of all it's Sergey and that is not C.
DretenX 17-Sep-13 11:35am    
sorry sergey, well it certainly isn't vb...
[no name] 17-Sep-13 11:42am    
So what? Are you a programmer or aren't you? Translate it, use it as is or write it yourself. Or... *gasp* do your own research.
DretenX 18-Sep-13 15:28pm    
wow, i thought we were here to help each other...

 
Share this answer
 
v2
Well,
what you do yourself is mostly ... and so i could find some bits & peaces...
for those who might be interested: here's the result & it works! ->gvMail is a gridvieuw ;):


<pre lang="vb">
Imports OpenPop.Pop3
Imports OpenPop.Mime
Imports System.io

Public Class Form1
    Dim SSL As Boolean
    Dim PopHost As String
    Dim UserName As String
    Dim WW As String
    Dim PortNm As Integer

    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        PopHost = "pop.gmail.com"
        PortNm = 995
        WW = "password"
        UserName = someone@gmail.com
        TxtHostname.Text = PopHost
        CboAccount.Text = UserName
        TxtPort.Text = PortNm
    End Sub

    Private Sub CmdGetM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdGetM.Click        
        If ChbSSL.Checked = True Then
            SSL = True
        Else
            SSL = False
        End If
        Me.Read_Emails()
    End Sub

    Private Sub Read_Emails()
        Me.Cursor = Cursors.WaitCursor
        Dim pop3Client As Pop3Client
        pop3Client = New Pop3Client()
        pop3Client.Connect(PopHost, Val(PortNm), SSL)
        If pop3Client.Connected = False Then
            TxtCounter.BackColor = Color.Red
        Else
            TxtCounter.BackColor = Color.LightGreen
            pop3Client.Authenticate(UserName, WW, AuthenticationMethod.UsernameAndPassword)
            If ChbReset.Checked = True Then pop3Client.Reset()
            Dim count As Integer = pop3Client.GetMessageCount()
            TxtCounter.Text = count
            TxtCounter.Update()
            Dim Email(7) As String, GvIndex As Integer = -1, message As Message
            For i As Integer = count To 1 Step -1
                TxtCounter.Text = GvIndex + 2 & "/" & count
                TxtCounter.Update()
                message = pop3Client.GetMessage(i)
                Email(1) = i 'MessageNumber
                Email(2) = message.Headers.Subject.ToString
                Email(3) = message.Headers.DateSent.ToString
                Email(4) = message.Headers.From.DisplayName.ToString & "(" & message.Headers.From.Address.ToString & ")"
                Email(5) = ""
                If message.Headers.To.Count > 0 Then
                    Email(5) = Email(5) & "TO:" & vbCrLf
                    For Each tomailaddress As OpenPop.Mime.Header.RfcMailAddress In message.Headers.To
                        Email(5) = Email(5) & tomailaddress.Address & vbCrLf
                    Next
                End If
                If message.Headers.Cc.Count > 0 Then
                    Email(5) = Email(5) & "CC:" & vbCrLf
                    For Each ccmailaddress As OpenPop.Mime.Header.RfcMailAddress In message.Headers.Cc
                        Email(5) = Email(5) & ccmailaddress.Address & vbCrLf
                    Next
                End If
                If message.Headers.Bcc.Count > 0 Then
                    Email(5) = Email(5) & "BCC:" & vbCrLf
                    For Each bccmailaddress As OpenPop.Mime.Header.RfcMailAddress In message.Headers.Bcc
                        Email(5) = Email(5) & bccmailaddress.Address & vbCrLf
                    Next
                End If

                Dim body As MessagePart = message.FindFirstHtmlVersion()
                If body IsNot Nothing Then
                    Email(6) = body.GetBodyAsText()
                Else
                    body = message.FindFirstPlainTextVersion()
                    If body IsNot Nothing Then
                        Email(6) = body.GetBodyAsText()
                    End If
                End If

                Dim attachments As List(Of MessagePart) = message.FindAllAttachments()
                Email(7) = ""
                For Each attachment As MessagePart In attachments
                    If Email(7) = "" Then
                        Email(7) = Email(7) & attachment.FileName
                    Else
                        Email(7) = Email(7) & vbCrLf & attachment.FileName
                    End If
                    'Dim ContentID As String = attachment.ContentId 'If this is set then the attachment is inline.
                    'Dim ContentType As String = attachment.ContentType.MediaType 'type of attachment pdf, jpg, png, etc.
                    File.WriteAllBytes(TxtAttDest.Text & attachment.FileName.ToString, attachment.Body) '//overwrites MessagePart.Body with attachment   
                Next

                gvEmails.Rows.Add()
                GvIndex = GvIndex + 1
                gvEmails.Item(0, GvIndex).Value = Email(1)
                gvEmails.Item(1, GvIndex).Value = Email(2)
                gvEmails.Item(2, GvIndex).Value = Email(3)
                gvEmails.Item(3, GvIndex).Value = Email(4)
                gvEmails.Item(4, GvIndex).Value = Email(5)
                gvEmails.Item(5, GvIndex).Value = Email(6)
                gvEmails.Item(6, GvIndex).Value = Email(7)
            Next
            pop3Client.Disconnect()
        End If
        Me.Cursor = Cursors.Default
    End Sub
End Class
 
Share this answer
 
here's the form i used to test the code:


<global.microsoft.visualbasic.compilerservices.designergenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.
<system.diagnostics.debuggernonusercode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<system.diagnostics.debuggerstepthrough()> _
Private Sub InitializeComponent()
Dim DataGridViewCellStyle1 As System.Windows.Forms.DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle
Dim DataGridViewCellStyle2 As System.Windows.Forms.DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle
Dim DataGridViewCellStyle3 As System.Windows.Forms.DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle
Dim DataGridViewCellStyle4 As System.Windows.Forms.DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle
Dim DataGridViewCellStyle5 As System.Windows.Forms.DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle
Dim DataGridViewCellStyle6 As System.Windows.Forms.DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle
Dim DataGridViewCellStyle7 As System.Windows.Forms.DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
Me.gvEmails = New System.Windows.Forms.DataGridView
Me.MsgNr = New System.Windows.Forms.DataGridViewTextBoxColumn
Me.Subject = New System.Windows.Forms.DataGridViewTextBoxColumn
Me.Datesent = New System.Windows.Forms.DataGridViewTextBoxColumn
Me.from = New System.Windows.Forms.DataGridViewTextBoxColumn
Me.Dest = New System.Windows.Forms.DataGridViewTextBoxColumn
Me.Body = New System.Windows.Forms.DataGridViewTextBoxColumn
Me.Attach = New System.Windows.Forms.DataGridViewTextBoxColumn
Me.CmdGetM = New System.Windows.Forms.Button
Me.TxtCounter = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.TxtHostname = New System.Windows.Forms.TextBox
Me.Label4 = New System.Windows.Forms.Label
Me.TxtPort = New System.Windows.Forms.TextBox
Me.ChbSSL = New System.Windows.Forms.CheckBox
Me.ChbReset = New System.Windows.Forms.CheckBox
Me.TxtAttDest = New System.Windows.Forms.TextBox
Me.Label5 = New System.Windows.Forms.Label
Me.CboAccount = New System.Windows.Forms.ComboBox
Me.CmdVnet = New System.Windows.Forms.Button
CType(Me.gvEmails, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'gvEmails
'
Me.gvEmails.AllowUserToAddRows = False
Me.gvEmails.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.gvEmails.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.gvEmails.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.MsgNr, Me.Subject, Me.Datesent, Me.from, Me.Dest, Me.Body, Me.Attach})
Me.gvEmails.Location = New System.Drawing.Point(30, 102)
Me.gvEmails.Name = "gvEmails"
Me.gvEmails.Size = New System.Drawing.Size(834, 148)
Me.gvEmails.TabIndex = 0
'
'MsgNr
'
DataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.TopCenter
Me.MsgNr.DefaultCellStyle = DataGridViewCellStyle1
Me.MsgNr.HeaderText = "MsgNr"
Me.MsgNr.Name = "MsgNr"
'
'Subject
'
DataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.TopLeft
DataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.[True]
Me.Subject.DefaultCellStyle = DataGridViewCellStyle2
Me.Subject.HeaderText = "Subject"
Me.Subject.Name = "Subject"
'
'Datesent
'
DataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.TopLeft
Me.Datesent.DefaultCellStyle = DataGridViewCellStyle3
Me.Datesent.HeaderText = "Date Sent"
Me.Datesent.Name = "Datesent"
'
'from
'
DataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.TopLeft
Me.from.DefaultCellStyle = DataGridViewCellStyle4
Me.from.HeaderText = "From"
Me.from.Name = "from"
'
'Dest
'
DataGridViewCellStyle5.Alignment = System.Windows.Forms.DataGridViewContentAlignment.TopLeft
DataGridViewCellStyle5.WrapMode = System.Windows.Forms.DataGridViewTriState.[True]
Me.Dest.DefaultCellStyle = DataGridViewCellStyle5
Me.Dest.HeaderText = "Dest."
Me.Dest.Name = "Dest"
Me.Dest.Width = 250
'
'Body
'
DataGridViewCellStyle6.Alignment = System.Windows.Forms.DataGridViewContentAlignment.TopLeft
DataGridViewCellStyle6.WrapMode = System.Windows.Forms.DataGridViewTriState.[True]
Me.Body.DefaultCellStyle = DataGridViewCellStyle6
Me.Body.HeaderText = "Content"
Me.Body.Name = "Body"
Me.Body.Width = 250
'
'Attach
'
DataGridViewCellStyle7.Alignment = System.Windows.Forms.DataGridViewContentAlignment.TopLeft
DataGridViewCellStyle7.WrapMode = System.Windows.Forms.DataGridViewTriState.[True]
Me.Attach.DefaultCellStyle = DataGridViewCellStyle7
Me.Attach.HeaderText = "Attachment(s)"
Me.Attach.Name = "Attach"
'
'CmdGetM
'
Me.CmdGetM.Enabled = False
Me.CmdGetM.Location = New System.Drawing.Point(527, 15)
Me.CmdGetM.Name = "CmdGetM"
Me.CmdGetM.Size = New System.Drawing.Size(113, 20)
Me.CmdGetM.TabIndex = 1
Me.CmdGetM.Text = "Get Mails"
Me.CmdGetM.UseVisualStyleBackColor = True
'
'TxtCounter
'
Me.TxtCounter.Location = New System.Drawing.Point(527, 42)
Me.TxtCounter.Name = "TxtCounter"
Me.TxtCounter.ReadOnly = True
Me.TxtCounter.Size = New System.Drawing.Size(113, 20)
Me.TxtCounter.TabIndex = 2
Me.TxtCounter.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(272, 18)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(47, 13)
Me.Label1.TabIndex = 5
Me.Label1.Text = "Account"
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(31, 18)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(55, 13)
Me.Label3.TabIndex = 8
Me.Label3.Text = "Hostname"
'
'TxtHostname
'
Me.TxtHostname.Location = New System.Drawing.Point(92, 15)
Me.TxtHostname.Name = "TxtHostname"
Me.TxtHostname.ReadOnly = True
Me.TxtHostname.Size = New System.Drawing.Size(151, 20)
Me.TxtHostname.TabIndex = 7
'
'Label4
'
Me.Label4.AutoSize = True
Me.Label4.Location = New System.Drawing.Point(31, 44)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(26, 13)
Me.Label4.TabIndex = 10
Me.Label4.Text = "Port"
'
'TxtPort
'
Me.TxtPort.Location = New System.Drawing.Point(92, 41)
Me.TxtPort.Name = "TxtPort"
Me.TxtPort.ReadOnly = True
Me.TxtPort.Size = New System.Drawing.Size(70, 20)
Me.TxtPort.TabIndex = 9
'
'ChbSSL
'
Me.ChbSSL.AutoSize = True
Me.ChbSSL.Checked = True
Me.ChbSSL.CheckState = System.Windows.Forms.CheckState.Checked
Me.ChbSSL.Location = New System.Drawing.Point(197, 44)
Me.ChbSSL.Name = "ChbSSL"
Me.ChbSSL.Size = New System.Drawing.Size(46, 17)
Me.ChbSSL.TabIndex = 11
Me.ChbSSL.Text = "SSL"
Me.ChbSSL.UseVisualStyleBackColor = True
'
'ChbReset
'
Me.ChbReset.AutoSize = True
Me.ChbReset.Checked = True
Me.ChbReset.CheckState = System.Windows.Forms.CheckState.Checked
Me.ChbReset.Location = New System.Drawing.Point(275, 71)
Me.ChbReset.Name = "ChbReset"
Me.ChbReset.Size = New System.Drawing.Size(128, 17)
Me.ChbReset.TabIndex = 12
Me.ChbReset.Text = "Reset Mails on server"
Me.ChbReset.UseVisualStyleBackColor = True
'
'TxtAttDest
'
Me.TxtAttDest.Location = New System.Drawing.Point(106, 72)
Me.TxtAttDest.Name = "TxtAttDest"
Me.TxtAttDest.Size = New System.Drawing.Size(137, 20)
Me.TxtAttDest.TabIndex = 13
Me.TxtAttDest.Text = "C:\Temp\"
'
'Label5
'
Me.Label5.AutoSize = True
Me.Label5.Location = New System.Drawing.Point(31, 75)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(69, 13)
Me.Label5.TabIndex = 14
Me.Label5.Text = "Attach. Dest."
'
'CboAccount
'
Me.CboAccount.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.CboAccount.FormattingEnabled = True
Me.CboAccount.Location = New System.Drawing.Point(324, 15)
Me.CboAccount.Name = "CboAccount"
Me.CboAccount.Size = New System.Drawing.Size(160, 21)
Me.CboAccount.TabIndex = 15
'
'CmdVnet
'
Me.CmdVnet.Location = New System.Drawing.Point(679, 14)
Me.CmdVnet.Name = "CmdVnet"
Me.CmdVnet.Size = New System.Drawing.Size(113, 20)
Me.CmdVnet.TabIndex = 16
Me.CmdVnet.Text = "Version .net"
Me.CmdVnet.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(884, 262)
Me.Controls.Add(Me.CmdVnet)
Me.Controls.Add(Me.CboAccount)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.TxtAttDest)
Me.Controls.Add(Me.ChbReset)
Me.Controls.Add(Me.ChbSSL)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.TxtPort)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.TxtHostname)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.TxtCounter)
Me.Controls.Add(Me.CmdGetM)
Me.Controls.Add(Me.gvEmails)
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.MinimumSize = New System.Drawing.Size(900, 300)
Me.Name = "Form1"
Me.Text = "POP3mail"
CType(Me.gvEmails, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub
Friend WithEvents gvEmails As System.Windows.Forms.DataGridView
Friend WithEvents CmdGetM As System.Windows.Forms.Button
Friend WithEvents TxtCounter As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents TxtHostname As System.Windows.Forms.TextBox
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents TxtPort As System.Windows.Forms.TextBox
Friend WithEvents ChbSSL As System.Windows.Forms.CheckBox
Friend WithEvents ChbReset As System.Windows.Forms.CheckBox
Friend WithEvents TxtAttDest As System.Windows.Forms.TextBox
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents CboAccount As System.Windows.Forms.ComboBox
Friend WithEvents MsgNr As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents Subject As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents Datesent As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents from As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents Dest As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents Body As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents Attach As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents CmdVnet As System.Windows.Forms.Button

End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900