Receive Emails in VB 2010

Day : Aug,12,2012
Send & Receive E-mails from E-mail server using 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 laptop (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, enables you to protect you Google account and provide you with a password to work with applications like this one (E-mail Client). If you dealt with Outlook before you should already knew that; you use your Application password to login to your Gmail.com account associated with your Google account, here is a link that explains all of the 2-Step Verification method, if you don't have a Gmail.com or not working through 2-Step Verification method then you must 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
Outgoing 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.

The Send E-mail Part

Imports System.Net.Mail 'The Mail Class we use to send e-mails
Public Class SendFrm 'The form name : 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
UsrNm = ("Your_UserName@gmail.com") 'I'm using Gmail.com 
Pwd = ("Your_Password") 'My 2-Step Verification Password (Link) coz i enable this method, if not then your regular 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 : TxtSubCursor = Cursors.WaitCursor
SmtpSvr.Send(E_mail)
MsgBox("mail sent")
Cursor = Cursors.Default
End Sub
So easy, huh ?!
The Receive E-mail Part
This is 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)
TextBox1.AppendText(Cons("STAT ")) '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.
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

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

Update (1) Aug,15,2012 - I was able to successfully get response from Gmail (pop.gmail.com) server and pass my Username and password.
POP Commands
CommandResponsesExamples
USER name+OK name is welcome here
-ERR never heard of name
USER David
+OK Please enter a password
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 mmSTAT
+OK 2 320
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
RETR msg+OK message follows
-ERR no such message
RETR 1
+OK 120 octets
< the POP3 server sends the entire message here >
DELE msg+OK message deleted
-ERR no such message
DELE 2
+OK message deleted
NOOP+OK no transactionNOOP
+OK
LAST+OK nnLAST
+OK 2
RSET+OKRSET
+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


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 E-mails from POP3 Mail Server .
Update (3) Aug,18,2012 - Get list of E-mails and download them and read them to your application.

Here are some online Visual Basic lessons and courses :

Popular posts from this blog

Visual Basic 2010 Working With DataBase Full Project Example

VB NET Check internet connection

VB .NET DropBox Api Source Code Example

VB .NET WebView2 WinForms tips

Visual Basic .Net - Button Sound / Music Play

Visual Basic Online Courses DataGridView Add Edit Delete