Reading and sending e-mail messages from Gmail using SMTP and POP3 protocols

Note:
This solution requires the component to be installed first.
To download the installer,
click here
Introduction
This article will show you how to read and send e-mail messages from Gmail account using SMTP and POP3 protocol over an SSL connection.
Detail
Sending an e-mail
To Send an e-mail message we will use the wodSmtp component.
Sending an e-mail is as easy as "1-2-3". Download the sample source and the component using the link above.
How the code works
First we set up the hostname, port, login, password and security options.
VB6
With wodSmtp1
.Login = "user@gmail.com"
.Password = "password"
.Authentication = AuthAuto
.HostName = "smtp.gmail.com"
.Security = SecurityImplicit
.Port = 465
End With
VC
m_Smtp.SetLogin(m_Login);
m_Smtp.SetPassword(m_Password);
m_Smtp.SetAuthentication(255 /* AuthLogin */);
m_Smtp.SetHostname("smtp.gmail.com");
m_Smtp.SetSecurity(3 /* SecurityImplicit */);
m_Smtp.SetPort(465);
after everything is set to connect to the Gmail SMTP server, you only need to call one method...
VB6
wodSmtp1.SendSimple txtFrom.Text, txtTo.Text, txtSubject.Text, txtText.Text
VC
m_Smtp.SendSimple(m_From, m_To, m_Subject, m_Text);
...and that is it! Your e-mail is on its way.
Reading e-mail from Gmail account
To read e-mails from a Gmail account we will use wodPop3 component (download from http://www.weonlydo.com/Samples/wodPop3.exe).
As in the previous case, we first set the correct settings (hostname, port, security options and account information).
VB6
With wodPop31
.HostName = "pop.gmail.com"
.Security = SecurityImplicit
.Port = 995
.Login = "user@gmail.com"
.Password = "password"
End With
VC
m_Pop3.SetHostname("pop.gmail.com");
m_Pop3.SetSecurity(3 /* SecurityImplicit */);
m_Pop3.SetPort(995);
m_Pop3.SetLogin(m_Login);
m_Pop3.SetPassword(m_Password);
After the information is set, we connect to Gmail's POP3 server.
VB6
wodPop31.Connect
VC
m_Pop3.Connect()
Upon successful connection the component will fire the Connected event in which we can tell the component to retrieve all available messages.
VB6
Private Sub wodPop31_Connected()
wodPop31.Messages.GetAll
End Sub
VC
void CGmailDlg::POP3_OnConnected()
{
CPop3Msgs msgs;
msgs.AttachDispatch(m_Pop3.GetMessages());
msgs.GetAll();
msgs.DetachDispatch();
}
The component will retrieve all messages for you and put them in the Messages collection from where you can easily access them. It will notify you when messages are available by firing the Done event
and that is where you probably want to populate a list of messages on your application window or do some first-time parsing.
VB6
Private Sub wodPop31_Done(ByVal ErrorCode As Long, ByVal ErrorText As String, ByVal HeadersOnly As Boolean)
For i = 0 To wodPop31.Messages.Count - 1
List1.AddItem wodPop31.Messages(i).Subject
Next i
End Sub
VC
void CGmailDlg::POP3_OnDone(long ErrorCode, LPCTSTR ErrorText, BOOL HeadersOnly)
{
CPop3Msgs msgs;
CPop3Msg msg;
msgs.AttachDispatch(m_Pop3.GetMessages());
for (int i = 0; i < msgs.GetCount(); i++)
{
msg.AttachDispatch(msgs.GetItem(i));
m_List.AddString(msg.GetSubject());
msg.DetachDispatch();
}
msgs.DetachDispatch();
}
Summary
In this example we have shown how simple it is to send e-mails and retrieve them from a Gmail account by using WeOnlyDo's components -
wodSmtp and wodPop3.
Visit
WeOnlyDo! Inc.
for more information and more samples.