Receive E-mails from mail server using POP3 and VB .net

Hello,

I was always thinking if i could send and receive e-mails using .Net technology, i was shocked to know that .Net 4 has no POP3 class so one could receive e-mails just like in microsoft Outlook, regardless that Outlook uses POP3, IMAP and SMTP to both send and receive e-mails from e-mail servers.

I was able to finish the send E-mail part successfully, though i've not finished the attachment and the Html parts, but the receiving e-mails part still out there somewhere i did not do it, though i managed to connect to pop3 servers and receive the +OK response successfully.

I'm using an old labtop (WinXpSp3 Home Edition) with VS2010 along with all the .Net kits installed. I've search the internet for pop3-vb.net resources and i could not land on something that works 100% with the receive E-mails part although i ran into some C# codes that helped me to take the first step which is the Response from the server (Gmail.com).

Another thing to tell you, is Gmail.com have 2-Step Verification method that if enabled, it enables you to protect your Google account and provide you with a password to work with applications like this one (The E-mail Client). If you've dealt before with Outlook then you should already know that, you use your Application passworl to login to your Gmail.com account associated with your Google account, here is a link to explain all the 2-Step Verification method, if you don't have a Gmail.com or not working through 2-Step Verification method then, ignore this.

Also you have to know all about incoming and outgoing Ports, Server and of course User Name and PassWord, and remember to always use the full e-mail address (your_username@emailserver.com) not just your username, here is some info about E-mail server settings:

Hotmail Settings

- Incoming Server(pop3.live.com) port(995) Secure Connection (SSL) is a must
- Otgoing Server(smtp.live.com) port(587) Secure Connection (TLS) is a must

Note that Hotmail.com have both free and paid accounts, but both supports E-mail clients. So you should go inside your E-mail settings to enable POP3 and forwarding option to be able to work with E-mail clients.

Yahoo Settings

Incoming Server(pop.mail.yahoo.com) port(995) Secure Connection (SSL) is not a must if you're using the free mail.
Outgoing Server(smtp.mail.yahoo.com) port(465) Secure Connection (TLS) is a must

Note that Yahoo.com have both free and paid accounts, but both supports E-mail clients. So you should go inside your E-mail settings to enable POP3 and forwarding option to be able to work with E-mail clients.

Gmail Settings

Incoming Server(pop.gmail.com) port(995) Secure Connection (SSL) is a must
Outgoing Server(smtp.gmail.com) port(587) Secure Connection (TLS) is a must

Finally, i would really like to suggest using Outlook as a practise first, try to establish a connection with your mail server and send some e-mail and receive some, just to make sure your mail server is sat up correctly.

VB.Net Send E-mail

'The Mail Class we use to send e-mails
Imports System.Net.Mail
'The form name : SendFrm
Public Class SendFrm
Dim SmtpSvr As New Net.Mail.SmtpClient()
Dim E_mail As New Net.Mail.MailMessage()
Dim UsrNm, Pwd, 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
'I'm using Gmail.com
UsrNm = ("Your_UserName@gmail.com")
'My 2-Step Verfication Password (Link) coz
'I enable this method, if not then your regular Password.
Pwd = ("Your_Password")
Srve = ("pop.gmail.com")
Intport = 587
End Sub
Private Sub Label11_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Label11.Click
'I'm using Label11 to send : Name (Send)
SmtpSvr.UseDefaultCredentials = False
SmtpSvr.Credentials = New Net.NetworkCredential(UsrNmt.ToString, Pwd.ToString)
SmtpSvr.Port = Intport
SmtpSvr.Host = Srve.ToString
E_mail = New MailMessage()
E_mail.From = New MailAddress(UsrNm.ToString, "Whatever_You_Desire", System.Text.Encoding.UTF8)
E_mail.IsBodyHtml = False 'Still Working on it.
E_mail.Body = ("Hello, This is my first E-mail though My Client")
SmtpSvr.EnableSsl = True
E_mail.To.Add(TxtFrnd.Text.ToString) 'TextBox Name : TxtFrnd
E_mail.Subject = (TxtSub.Text.ToString) 'TextBox Name : TxtSub
Cursor = Cursors.WaitCursor
SmtpSvr.Send(E_mail)
MsgBox("mail sent")
Cursor = Cursors.Default
End Sub
view raw gistfile1.vb hosted with ❤ by GitHub

So easy, huh ?!

VB.Net Receive E-mail

  • Another VB Project.

Imports System.IO
Imports System.Net.Sockets
Imports System.Text
Imports System.Net.Security
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 = "My2stepVerficationPass"
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)
'TextBox Name : TextBox1 to receive response from server
'using (STAT) command,
'Note : In the bottom there is a table with all POP commands
'and responses.I'm using Cons Function to send commands to POP server.
TextBox1.AppendText(Cons("STAT "))
Cursor = Cursors.Default
End Sub
Function Cons(ByVal server_Command As String)
Dim m_buffer() As Byte = System.Text.Encoding.ASCII.GetBytes(server_Command)
Dim m_sslStream = New SslStream(POP3.GetStream(), False)
m_sslStream.AuthenticateAsClient(PopHost)
Dim bytes As Int64 = m_sslStream.Read(m_buffer, 0, m_buffer.Length)
Return (Encoding.ASCII.GetString(m_buffer, 0, bytes))
End Function
view raw gistfile1.vb hosted with ❤ by GitHub

Of course this is not it, I'm still working on errors i receive .... keep up!


List of Updates

Update (1) Aug,15,2012 - I was able to successfully get response from Gmail (pop.gmail.com) server and pass my Username and password.
Update (2) Aug,17,2012 - Retrieving the number of messages from Inbox into a TextBox control.
Update (3) Aug,17,2012 - Get list of E-mails and download them and read them .
Update (4) Jan,07,2013 - Fixing issues and the final source code download .

Code Project - evry1falls
Code Project article

POP Commands

Command Responses Examples
USER name +OK name is welcome here 
-ERR never heard of name
USER David 
+OK Please enter a password
- Note that this should be sent first ....
PASS string +OK maildrop locked and ready 
-ERR invalid password 
-ERR unable to lock maildrop
PASS test 
+OK valid logon
QUIT +OK +OK Server closing connection
STAT +OK nn mm STAT 
+OK 2 320
- Returns how many E-mails are in Inbox .....
LIST [msg] +OK scan listing follows 
-ERR no such message
LIST 
+OK 2 messages (320 octets) 
1 120 
2 200 
... 

LIST 2 
+OK 2 200
- Get a list of all E-mails you've waiting for .....
RETR msg +OK message follows 
-ERR no such message
RETR 1 
+OK 120 octets 
< the POP3 server sends the entire message here >
- Retrieves E-mail messages from the server .....
DELE msg +OK message deleted 
-ERR no such message
DELE 2 
+OK message deleted
NOOP +OK no transaction NOOP 
+OK
- Stands for (No Operation) : checks the server status ....
LAST +OK nn LAST 
+OK 2
RSET +OK RSET 
+OK maildrop has 2 messages (320 octets)
Additional Commands
TOP msg nn +OK top of msg 
-ERR
TOP 1 10 
+OK 
< first 10 lines of the header >
RPOP user +OK 
-ERR
RPOP david 
+OK enter password

Back


All Rights Reserved to the Developer evry1falls

Free Web Hosting