Welcome to ADO.NET Access 2003—your ultimate hub for VB.NET and ADO.NET programming excellence. Discover in-depth tutorials, practical code samples, and expert troubleshooting guides covering a broad range of topics—from building robust WinForms applications and seamless MS Access integration to working with SQL Server, MySQL, and advanced tools like WebView2 and Crystal Reports. Whether you're a beginner or a seasoned developer, our step-by-step articles are designed to empower you to optimize.

Looking for MS Access Developer❓❓

Application developer

Post Page Advertisement [Top]

Building an Email Client in VB.NET: 
Sending & Receiving Emails with POP3

app designers and developers

Hello, app designers and developers

I always wondered if I could send and receive emails using .NET technology. I was surprised to learn that .NET4 does not include a built‐in POP3 class to receive emails like Microsoft Outlook—even though Outlook uses a combination of POP3, IMAP, and SMTP to manage email communication.

I managed to complete the email sending part (although the attachment and HTML body handling are still under development). For the receiving side, I successfully connected to POP3 servers and retrieved the +OK response from Gmail; however, the complete implementation of the email reception functionality is still a work in progress.

I developed this project on an older laptop running Windows XP SP3 using Visual Studio 2010. Despite extensive research on VB.NET POP3 resources, I mainly found C# examples that helped me capture the first step: establishing a connection and receiving the server response.

Note: Gmail’s 2-Step Verification is essential for added security. With it enabled, you must use an application-specific password (instead of your regular password) to work with external email clients. Also, always remember to use your full email address (e.g., your_username@emailserver.com) when configuring the client.

Email Server Settings

Hotmail/Live

  • Incoming Server: pop3.live.com (Port 995, SSL required)
  • Outgoing Server: smtp.live.com (Port 587, TLS required)

Yahoo

  • Incoming Server: pop.mail.yahoo.com (Port 995; SSL is optional for free accounts)
  • Outgoing Server: smtp.mail.yahoo.com (Port 465, TLS required)

Gmail

  • Incoming Server: pop.gmail.com (Port 995, SSL required)
  • Outgoing Server: smtp.gmail.com (Port 587, TLS required)

I recommend testing with a client like Outlook first to ensure your email server settings are correct before integrating them into your custom application.

The Send E-mail Part

Below is the VB.NET code that sends an email. (Note: Modify the email addresses, credentials, and server details as needed.)


Imports System.Net.Mail

Public Class SendFrm
    Dim SmtpSvr As New SmtpClient()
    Dim E_mail As New MailMessage()
    Dim UsrNm As String, Pwd As String, Srve As String
    Dim Int_port As Integer

    Private Sub SendFrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Configure your SMTP details
        UsrNm = "Your_UserName@gmail.com"   ' Replace with your email address
        Pwd = "Your_Password"               ' Use your 2-Step Verification password or normal password
        Srve = "smtp.gmail.com"             ' SMTP server (for Gmail)
        Int_port = 587
    End Sub

    Private Sub Label11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label11.Click
        SmtpSvr.UseDefaultCredentials = False
        SmtpSvr.Credentials = New Net.NetworkCredential(UsrNm, Pwd)
        SmtpSvr.Port = Int_port
        SmtpSvr.Host = Srve
        SmtpSvr.EnableSsl = True

        E_mail = New MailMessage()
        E_mail.From = New MailAddress(UsrNm, "Your Display Name", System.Text.Encoding.UTF8)
        E_mail.IsBodyHtml = False  ' HTML support pending further development
        E_mail.Body = "Hello, this is my first email sent from my custom client."
        E_mail.To.Add(TxtFrnd.Text)  ' TxtFrnd is a TextBox for the recipient's email address
        E_mail.Subject = TxtSub.Text ' TxtSub is a TextBox for the email subject

        SmtpSvr.Send(E_mail)
        MsgBox("Mail sent successfully!")
    End Sub
End Class
    

The Receive E-mail Part

The following VB.NET project snippet demonstrates how to connect to a POP3 server and receive responses. This is an early stage of the email reception functionality.


Imports System.IO
Imports System.Net.Sockets
Imports System.Text
Imports System.Net.Security

Public Class Form1
    Dim Read_Stream As StreamReader
    Dim POP3 As New TcpClient
    Dim PopHost As String = "pop.gmail.com"
    Dim UserName As String = "MyGmail@gmail.com"
    Dim Password As String = "My2stepVerificationPass"
    Dim Server_Response As String
    Dim response As StreamWriter

    Private Sub CmdDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdDownload.Click
        Cursor = Cursors.WaitCursor
        POP3.Connect(PopHost, 995)
        TextBox1.AppendText(Cons("STAT "))  ' Uses STAT command to fetch server response
        Cursor = Cursors.Default
    End Sub

    Function Cons(ByVal server_Command As String) As String
        Dim m_buffer() As Byte = Encoding.ASCII.GetBytes(server_Command)
        Dim m_sslStream As New SslStream(POP3.GetStream(), False)
        m_sslStream.AuthenticateAsClient(PopHost)
        Dim bytes As Integer = m_sslStream.Read(m_buffer, 0, m_buffer.Length)
        Return Encoding.ASCII.GetString(m_buffer, 0, bytes)
    End Function
End Class
    

POP3 Commands Overview

Command Response Example
USER <name> +OK name is welcome here / -ERR never heard of name USER David
PASS <string> +OK maildrop locked and ready PASS test
QUIT +OK Server closing connection QUIT
STAT +OK nn mm STAT
LIST +OK scan listing follows LIST
RETR <msg> +OK message follows RETR 1
DELE <msg> +OK message deleted DELE 2
NOOP +OK no transaction NOOP
RSET +OK maildrop has nn messages (mmm octets) RSET

I'm still refining the email receiving process and error handling. Your feedback and suggestions are welcome. Happy coding!

 Here are some online Visual Basic lessons and courses:

Bottom Ad [Post Page]