Library code snippets
Send & Receive Emails using Winsock and POP3
This code demonstrates how to send and receive email from a POP3 server using the WinSock control.
Receiving Email
You will need: cmdCheckMail (a button), Winsock1 and List1.
In Command1_Click() you need t change the username and password and the smtp server. Although some servers allow you to send messages thru them even if the from email account doesnt really exist.
The messages will be downloaded to the C:\Windows\Temp folder. It will download attachments.
It saves them as *.eml, which is the default extension for email. When you open the files, they'll open in the default email client, Outlook Express, or something.
This example will leave a copy of the message still on the server.. If you want to delete the message simply add sendMsg "DELE " & a
after the RETR command in cmdCheckMail_Click(). ' In general Declarations
Dim received As Boolean
Dim Message$
Dim sckError
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData Message$
Select Case Winsock1.Tag
Case "RETR"
Put #1, , Message$
If InStr(Message$, vbLf + "." + vbCrLf) Then
Close 1
received = True
End If
Case Else
sckError = (Left$(Message$, 3) = "-ER")
received = True
End Select
End Sub
Private Sub Winsock1_Close()
Winsock1.Close
End Sub
Private Sub cmdCheckMail_Click()
' LogIn to the server ~ get settings from outlook express
Winsock1.Connect "pop.freeserve.net", 110
Do Until received: DoEvents: Loop
if sckError then msgbox "An error occured trying to connect to server" : Exit sub
sendMsg "USER username" ' Send UserName
if sckError then Msgbox "Error with username" : Exit sub
sendMsg "PASS password" ' Send Password
if sckError then Msgbox "Error with password" : Exit sub
' Get Number of Messages and total size in bytes
sendMsg "STAT"
x = InStr(Message$, " "): b = InStrRev(Message$, " ")
Messages = Val(Mid$(Message$, x + 1, b - x))
Size = Val(Mid$(Message$, b + 1))
MsgBox "Number of messages to download " & Messages
' Download all messages
For a = 1 To Messages
' Winsock1_DataArrival will save message as "Email-1.eml", "Email-2.eml" etc
Winsock1.Tag = "RETR"
Open "C:\Windows\Temp\eMail-" & a & ".eml" For Binary Access Write As #1
sendMsg "RETR " & a
List1.AddItem "eMail " & a & ": Downloaded"
Next
Winsock1.Tag = ""
End Sub
Sub sendMsg(m$)
Winsock1.SendData m$ + vbCrLf
received = False
Do Until received
DoEvents
Loop
End Sub
Sending mail
This is the code I use to send emails using Winsock.
You need these two controls: Winsock1 and Command1.' In general Declarations
Dim received As Boolean
Private Sub Command1_Click()
sFrom$ = "Jack@hammer.net"
sTo$ = "zombie@freakys.fsnet.co.uk"
sSubject$ = "Hello Mary"
sMessage$ = "This is a simple Message"
' need SMTP server to route message thru, 25 (SMTP)
Winsock1.Connect "smtp.freeserve.net", 25
Do While Winsock1.State <> sckConnected: DoEvents: Loop
sendMsg "HELO " & "Peaches"
sendMsg "MAIL FROM: <" & sFrom & ">"
sendMsg "RCPT TO: <" & sTo & ">"
sendMsg "DATA"
m$ = m$ + "From: <" + sFrom + ">" + vbCrLf
m$ = m$ + "To: <" + sTo + ">" + vbCrLf
m$ = m$ + "Subject: " + sSubject$ + vbCrLf
m$ = m$ + "Date: " + Format$(Now, "h:mm:ss") + vbCrLf
m$ = m$ + "MIME-Version: 1.0" + vbCrLf
m$ = m$ + "Content-Type: text/plain; charset=us-ascii" + vbCrLf + vbCrLf
m$ = m$ + sMessage$ + vbCrLf + vbCrLf + "." + vbCrLf
sendMsg m$ + "QUIT"
Winsock1.Close
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
received = True
End Sub
Sub sendMsg(m$)
Winsock1.SendData m$ + vbCrLf
received = False
Do Until received
DoEvents
Loop
End Sub
Related articles
Related discussion
-
How to POP3 in C#
by bishnu.tewary (7 replies)
-
VB6, SQL 2005 & DMO
by elajaunie3 (1 replies)
-
sending sms from pc
by sriraj20074 (0 replies)
-
Automating Excel from VB6.0
by epurdy (0 replies)
-
VB6 system conversion using VBA to Word 2007
by b.macgregor@vodamail.co.za (0 replies)
Related podcasts
-
Moving your Email into the Cloud - Google for Apps and Live Custom Domains
Scott and Carl talk about Scott's Family's recent move to Google Apps and Carl considers moving to Live Custom Domains. What are the benefits of moving your life into the cloud?
Events coming up
-
Jul
20
Hereford Media Central
Hereford, United Kingdom
John Price, NxtGenUG Co-Counder, JIT Pizza eater and the owner of the world's only email generated black hole makes his way over to Hereford to deliver his Windows Media Centre presentation and show how to score brownie points with the wife, do development and have fun all at the same time.
Thanks alot for this code i was searching for this code since 1 month.Thanks once again. Can u please help me with this code I m having issue with the sMessage$ variable in place of the string i want to get the text entered in the textbox of my form and mail it to specific email id. Please help me out with this.Thanks in advance
How would I solve the fact that my e-mail provider requires a SMTP server authentication? Anyone who's got an addition to the code for outgoing e-mail?
Code Here
This code d'ont work on receiving, give me always invalid username.
I need d/l my email to *.eml.
they are another way to d/l.
FMalheiro
Need code on how to attach a file(s) to this email sending program
using WINSOCK is also needed.
Tks.
Rgds
This thread is for discussions of Send & Receive Emails using Winsock and POP3.