Session' is not declared. It may be inaccessible due to its protection level

graham23s

Member
Joined
Feb 22, 2009
Messages
13
Programming Experience
1-3
Hi GUys,

After a few years away from programming i have dusted of visual studio and have got back in to it :)

What i am doing is logging in to my pop3 account to retrieve the emails, i have added an association with openpop.net: OpenPop.NET | Free Communications software downloads at SourceForge.net

My current code is:

VB.NET:
Imports OpenPop.Pop3
Imports OpenPop.Mime
Imports System.Data

Public Class formMain

    Private Sub mainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Protected Sub Read_Emails(ByVal sender As Object, ByVal e As EventArgs)

        Dim pop3Client As Pop3Client

        If (Session("Pop3Client") Is Nothing) Then

            pop3Client = New Pop3Client

            pop3Client.Connect(txtMailServer.Text, Integer.Parse(txtPort.Text), chkSSL.Checked)

            pop3Client.Authenticate(txtUserName.Text, txtPassword.Text)

            Session("Pop3Client") = pop3Client

        Else

            pop3Client = CType(Session("Pop3Client"), Pop3Client)

        End If

        Dim count As Integer = pop3Client.GetMessageCount

        Dim dtMessages As DataTable = New DataTable

        dtMessages.Columns.Add("MessageNumber")

        dtMessages.Columns.Add("From")

        dtMessages.Columns.Add("Subject")

        dtMessages.Columns.Add("DateSent")

        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

            counter = counter + 1

            i = i - 1

            If counter = 5 Then

                Exit Do

            End If

        Loop

        gvEmails.DataSource = dtMessages

        gvEmails.DataBind()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles getMailButton.Click


    End Sub


End Class

I seem to be getting a few erros which is mainly:

"Session' is not declared. It may be inaccessible due to its protection level."

I have googled about but can't seem to find a solution, any help would be appreciated :)

thanks guys

Graham
 
The example you're trying to follow is for ASP.NET and you're trying to create a WinForms app. Session is for global variables in a web app so whatever data is being stored in session variables there, you will have to store somewhere appropriate for a WinForms app.
 
Hey Guys,

ahh that explains it lol, i'm not going crazy after all lol is there any tutorials or premade classes i could use, i'm using aol as my main email service, i have read gmail is slightly more complicated to do if i'm not mistaken.

thanks guys

Graham
 
Hi,

Have a look at the SmtpClient which can be used in the WinForms environment. See here:-

SmtpClient Class (System.Net.Mail)

Cheers,

Ian
The SMTP protocol is for sending email messages from a client to a server, while the POP3 protocol is for retrieving email messages from a server to a client. As such, the .NET SmtpClient class is for sending email messages, which is a quite common task for applications to perform, but it is not for retrieving emails. There is no Pop3Client class in the .NET Framework because retrieving emails is a very uncommon task for an application to perform. Pretty much everyone has an email client for that and if you want to build an email client then you would want to handle the low-level details yourself. Basically, you need to either use a Socket and implement the POP3 protocol yourself or else find a component that does just that, which apparently includes that OpenPop.Pop3.Pop3Client used in that example.
 
Last edited:
Back
Top