Hi,

I have posted about receiving email before and this is a follow up of my progress. I have only one form and it consists of 2 buttons, one for downloading messages from the server, the other, to disconnect the connection. I also have one listview and one textbox to display the message.

I took the codes from a few open source programming website and compiled them. My program works as such that upon clicking the 'Download Messages' button, a message box will pop up and the user/admin will be asked to enter the POP3 account name. My doubts are I am not sure what POP3 account name to put in.

I use IIS/SMTP to send email and intend to use IIS as a server to receive email. How and where can i find out what is my POP3 account name?

Below are my codes:-

Imports System.Net.Sockets
Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents ListView1 As System.Windows.Forms.ListView
    Friend WithEvents CHFrom As System.Windows.Forms.ColumnHeader
    Friend WithEvents CHSubject As System.Windows.Forms.ColumnHeader
    Friend WithEvents CHDate As System.Windows.Forms.ColumnHeader
    Friend WithEvents CHSize As System.Windows.Forms.ColumnHeader
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.ListView1 = New System.Windows.Forms.ListView
        Me.CHFrom = New System.Windows.Forms.ColumnHeader
        Me.CHSubject = New System.Windows.Forms.ColumnHeader
        Me.CHDate = New System.Windows.Forms.ColumnHeader
        Me.CHSize = New System.Windows.Forms.ColumnHeader
        Me.Button2 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Button1.Location = New System.Drawing.Point(8, 5)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(182, 32)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Download Messages"
        '
        'TextBox1
        '
        Me.TextBox1.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.TextBox1.Location = New System.Drawing.Point(4, 244)
        Me.TextBox1.MaxLength = 0
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both
        Me.TextBox1.Size = New System.Drawing.Size(640, 200)
        Me.TextBox1.TabIndex = 1
        '
        'ListView1
        '
        Me.ListView1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.CHFrom, Me.CHSubject, Me.CHDate, Me.CHSize})
        Me.ListView1.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.ListView1.FullRowSelect = True
        Me.ListView1.HideSelection = False
        Me.ListView1.Location = New System.Drawing.Point(4, 44)
        Me.ListView1.Name = "ListView1"
        Me.ListView1.Size = New System.Drawing.Size(640, 192)
        Me.ListView1.TabIndex = 3
        Me.ListView1.UseCompatibleStateImageBehavior = False
        Me.ListView1.View = System.Windows.Forms.View.Details
        '
        'CHFrom
        '
        Me.CHFrom.Text = "From"
        Me.CHFrom.Width = 140
        '
        'CHSubject
        '
        Me.CHSubject.Text = "Subject"
        Me.CHSubject.Width = 300
        '
        'CHDate
        '
        Me.CHDate.Text = "Date"
        Me.CHDate.Width = 100
        '
        'CHSize
        '
        Me.CHSize.Text = "Size"
        Me.CHSize.Width = 80
        '
        'Button2
        '
        Me.Button2.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Button2.Location = New System.Drawing.Point(462, 5)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(182, 32)
        Me.Button2.TabIndex = 4
        Me.Button2.Text = "Close Connection"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(648, 447)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.ListView1)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "POP3 Demo"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

#End Region
    Dim objPOP3 As New POP3Message()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim msg As POP3Message.Message
        Dim msgString As String
        Dim messages As Integer
        Me.Cursor = Cursors.WaitCursor
        messages = objPOP3.Connect()
        If messages = -1 Then
            Me.Cursor = Cursors.Default
            Exit Sub
        End If
        Dim originalCaption As String = Me.Text
        For i = 1 To messages
            Me.Text = "Downloading message " & i.ToString & "/" & messages.ToString
            Dim msgItem As New ListViewItem()
            msgString = objPOP3.GetMessage(i)
            msg = objPOP3.CreateFromText(msgString)
            msgItem.Text = msg._From
            msgItem.SubItems.Add(msg._Subject)
            msgItem.SubItems.Add(msg._Date)
            ListView1.Items.Add(msgItem)
            TextBox1.AppendText(msg._Body & vbCrLf)
        Next
        Me.Text = originalCaption
        Me.Cursor = Cursors.Default
    End Sub


    Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
        'Dim messages As Integer
        'TextBox1.Text = objPOP3.GetMessage(ListView1.SelectedIndices(0) + 1)
    End Sub

    Private Sub ListView1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListView1.KeyUp
        If e.KeyCode = Keys.Delete Then
            If objPOP3.DeleteMessage(ListView1.SelectedIndices(0) + 1) >= 0 Then
                ListView1.Items(ListView1.SelectedIndices(0)).Text = "DELETED"
                ListView1.Items(ListView1.SelectedIndices(0)).SubItems.Clear()
            End If
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        objPOP3.Quit()
        ListView1.Items.Clear()
    End Sub
End Class

Class POP3Message

    Dim Server As TcpClient
    Dim NetStrm As NetworkStream
    Dim RdStrm As StreamReader

    Public Function Connect() As Integer
        Dim POP3Account As String
        POP3Account = InputBox("Enter your POP3 account name (e.g., mail.server.com)")
        If POP3Account.Trim = "" Then Exit Function
        Try
            Server = New TcpClient(POP3Account.Trim, 110)
            NetStrm = Server.GetStream()
            RdStrm = New StreamReader(Server.GetStream())
        Catch exc As Exception
            MsgBox(exc.Message)
            Exit Function
        End Try
        Dim user As String
        user = InputBox("Enter your user name")
        Dim data As String = "USER " + user.Trim + vbCrLf
        Dim szData() As Byte = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray())
        NetStrm.Write(szData, 0, szData.Length)
        Dim POPResponse As String
        POPResponse = RdStrm.ReadLine()
        If POPResponse.Substring(0, 4) = "-ERR" Then
            MsgBox("Invalid user name")
            Return -1
        End If

        Dim password As String
        password = InputBox("Enter your password")
        data = "PASS " & password & vbCrLf
        szData = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray())
        NetStrm.Write(szData, 0, szData.Length)
        POPResponse = RdStrm.ReadLine()

        If POPResponse.Substring(0, 4) = "-ERR" Then
            MsgBox("Invalid password")
            Return -1
        End If
        data = "STAT" + vbCrLf
        szData = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray())
        NetStrm.Write(szData, 0, szData.Length)
        POPResponse = RdStrm.ReadLine()
        If POPResponse.Substring(0, 4) = "-ERR" Then
            MsgBox("Could not log you in")
            Return -1
        End If
      
        Dim parts() As String
        parts = POPResponse.Split(" ")
        Dim messages, totSize As Integer
        messages = CInt(parts(1))
        Return messages
    End Function

    Public Function DeleteMessage(ByVal msgIndex As Integer)
        Dim data As String = "DELE " & msgIndex.ToString & vbCrLf
        Dim szData() As Byte = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray())
        NetStrm.Write(szData, 0, szData.Length)
        Dim tmpString As String = RdStrm.ReadLine()
        If tmpString.Substring(0, 4) = "-ERR" Then
            MsgBox("Could not delete message")
            Return -1
        Else
            Return 1
        End If
    End Function

    Public Function Quit()
        Dim data As String = "QUIT " & vbCrLf
        Dim szData() As Byte = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray())
        NetStrm.Write(szData, 0, szData.Length)
        Dim tmpString As String = RdStrm.ReadLine()
    End Function

    Public Structure Message
        Dim _From As String
        Dim _To As String
        Dim _Date As String
        Dim _Subject As String
        Dim _Sender As String
        Dim _CC As String
        Dim _BCC As String
        Dim _Received As String
        Dim _Body As String
    End Structure

    Public Function CreateFromText(ByVal strMessage As String) As Message
        Dim Mssg As New Message()
        Dim brkPos As Integer
        Dim Header As String
        Dim Headers() As String
        Dim Body As String
        Dim vField As Object
        Dim strHeader As String
        Dim HeaderName As String
        Dim HeaderValue As String

        brkPos = InStr(1, strMessage, vbCrLf & vbCrLf)
        If brkPos Then
            Header = strMessage.Substring(0, brkPos - 1)
            Body = strMessage.Substring(brkPos + 1, strMessage.Length - Header.Length - 3)
            Mssg._Body = Body
        Else
            Throw New Exception("Invalid message format")
            Exit Function
        End If

        Headers = Split(Header, vbCrLf)
        Dim _header As String
        For Each _header In Headers
            brkPos = _header.IndexOf(":")
            If brkPos >= 0 Then
                HeaderName = _header.Substring(0, brkPos)
            Else
                HeaderName = ""
            End If
            HeaderValue = _header.Substring(brkPos + 1)
            Select Case HeaderName.ToLower
                Case "received"
                    Mssg._Received = HeaderValue
                Case "from"
                    Mssg._From = HeaderValue
                Case "sender"
                    Mssg._Sender = HeaderValue
                Case "to"
                    Mssg._To = HeaderValue
                Case "cc"
                    Mssg._CC = HeaderValue
                Case "bcc"
                    Mssg._BCC = HeaderValue
                Case "subject"
                    Mssg._Subject = HeaderValue
                Case "date"
                    Mssg._Date = HeaderValue
            End Select
        Next
        Return Mssg
    End Function

    Function GetMessage(ByVal msgindex As Integer) As String
        Dim tmpString As String
        Dim Data As String
        Dim szData() As Byte
        Dim msg As String

        Try
            Data = "RETR " & msgindex.ToString & vbCrLf
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray())
            NetStrm.Write(szData, 0, szData.Length)
            tmpString = RdStrm.ReadLine()
            If tmpString.Substring(0, 4) <> "-ERR" Then
                While (tmpString <> ".")
                    msg = msg & tmpString & vbCrLf
                    tmpString = RdStrm.ReadLine()
                End While
            End If
        Catch exc As InvalidOperationException
            MsgBox("Message retrieval failed: " & vbCrLf & Err.ToString())
        End Try
        Return msg
    End Function

End Class

Recommended Answers

All 2 Replies

Are you using a POP account now ? What is your current email address (don't paste the full email, just @yahoo.com or whatever the domain is), how are you connecting to it -- ie webmail, exchange, outlook, etc? And what connection method are you using?

Are you using a POP account now ? What is your current email address (don't paste the full email, just @yahoo.com or whatever the domain is), how are you connecting to it -- ie webmail, exchange, outlook, etc? And what connection method are you using?

I'm not really sure how to answer your questions. I plan on using IIS to connect to I use SMTP Virtual server to send email. I have already developed an application for sending email. My program is such that the user/admin can input any email address that he/she wants on the 'From:' space. There is no specific email address.

Below are my codes for sending email:-

Please advise.

Imports System
Imports System.Net
Imports System.IO
Imports System.Net.Mail

Public Class Email

    'Declarations
    Private mailMessage As New Net.Mail.MailMessage()
    Dim attachment As Net.Mail.Attachment

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

        'Set default value
        txtFrom.Text = "0611145D@student.tp.edu.sg"

    End Sub

    Public Sub SendMassEmail(ByVal MsgFrom As String, ByVal MsgTo As String, ByVal MsgSubject As String, ByVal MsgBody As String)

        'This procedure takes string array parameters for multiple recipients and files
        Try

            '  Pass in the message information to a new MailMessage
            mailMessage = New Net.Mail.MailMessage(MsgFrom, MsgTo, MsgSubject, MsgBody)

            '   Adding attachments
            Dim attachment As Net.Mail.Attachment = New Net.Mail.Attachment(ofdAttach.FileName)
            mailMessage.Attachments.Add(New Attachment(txtAttach.Text))

            '   Create an SmtpClient to send the e-mail
            Dim mailClient As New SmtpClient("152.226.152.152")  '  = local machine IP Address

            '  Use the Windows credentials of the current User
            mailClient.UseDefaultCredentials = True

            ' Pass the message to the mail server
            mailClient.Send(mailMessage)

            '  Optional user reassurance:
            MessageBox.Show(String.Format("Message Successfully Sent!" & vbCrLf & vbCrLf & "Message Subject:   {0}" & vbCrLf & "FROM:    {1}" & vbCrLf & "TO:   {2}", MsgSubject, MsgFrom, MsgTo), "EMail", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Information)

            '  Housekeeping
            'mailMessage.Dispose()

            'Error Checking
        Catch ex As FormatException

            MessageBox.Show(ex.Message & " :Format Exception")

        Catch ex As SmtpException

            MessageBox.Show(ex.Message & " :SMTP Exception")

        End Try

    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

        'Error Messages
        If rtbTo.Text = "" Then
            MsgBox("Please enter the recipient(s)", MsgBoxStyle.Information, "Send Email")
            Exit Sub
        End If

        If txtFrom.Text = "" Then
            MsgBox("Please enter the sender", MsgBoxStyle.Information, "Send Email")
            Exit Sub
        End If

        If txtMsg.Text = "" Then
            MsgBox("Please enter the email message(s)", MsgBoxStyle.Information, "Send Email")
            Exit Sub
        End If

        'Calling the SendMassEmail() from a button
        SendMassEmail(txtFrom.Text, rtbTo.Text, txtSubj.Text, txtMsg.Text)

    End Sub

    Private Sub ofdComma_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ofdComma.FileOk

        'Adding some characters in the file
        Dim sr As New StreamReader(ofdComma.FileName)

        If Not sr.EndOfStream Then
            rtbTo.AppendText(sr.ReadLine)

            While Not sr.EndOfStream

                rtbTo.AppendText("," & sr.ReadLine)
                rtbTo.ScrollToCaret()

            End While
        End If
        sr.Close()

    End Sub

    Private Sub btnBrowseTo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseTo.Click

        'Call the OpenFileDialog()
        ofdComma.ShowDialog()

    End Sub

    Private Sub btnTo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTo.Click

        'Link to another window form
        Dim DGV As New AddContact
        AddContact.Show()

    End Sub

    Private Sub btnBrowseToDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseToDel.Click

        'Clear textbox
        rtbTo.Text = ""

    End Sub

    Private Sub btnBrowseAttch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseAttch.Click

        'Add attachments
        If txtAttach.Text <> "" Then
            mailMessage.Attachments.Add(New Attachment(txtAttach.Text))
        End If
        ofdAttach.ShowDialog()
        txtAttach.Text = ofdAttach.FileName

    End Sub

    Private Sub btnBrowseAttchDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseAttchDel.Click

        'Remove attachments
        txtAttach.Text = ""

        If txtAttach.Text <> "" Then
            mailMessage.Attachments.Remove(New Attachment(txtAttach.Text))
        End If

    End Sub

    Private Sub btnClrMsg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClrMsg.Click

        'Clear textbox
        txtMsg.Text = ""

    End Sub

    Private Sub btnClrAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClrAll.Click

        'Clear textboxes in the form
        txtFrom.Text = ""
        rtbTo.Text = ""
        txtSubj.Text = ""
        txtMsg.Text = ""
        txtAttach.Text = ""
        If txtAttach.Text <> "" Then
            mailMessage.Attachments.Remove(New Attachment(txtAttach.Text))
        End If

    End Sub

End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.