We begin by setting the MAPISession control's DownLoadMail property to True so that we will download mail when we establish a MAPI session:
Private Sub email_Click()
—> MAPISession1.DownLoadMail = True
.
.
.
End Sub
The MAPISession control has only two methods: SignOn and SignOff. These are the methods you use to connect to the computer's email system and from there to the Internet. To check the user's email, we need only establish an email session with the MAPISession control. If the user's computer is not currently connected to the Internet, the email system automatically makes the connection. All we have to do is to establish an email session with our MAPISession1 control:
Private Sub email_Click()
MAPISession1.DownLoadMail = True
—> MAPISession1.SignOn
.
.
.
End Sub
At this point, the connection is made to the Internet (if necessary) and the user's email (if any) is downloaded into the Inbox. If you want to fetch the messages from the Inbox and display them, you can use the MAPIMessages control, as we'll see later.
There is always the possibility of error when a user signs on to an ISP, so we check for errors using the Err keyword:
Private Sub email_Click()
MAPISession1.DownLoadMail = True
MAPISession1.SignOn
—> If Err <> 0 Then
—> MsgBox "Logon Failure: " + Error$
—> End If
.
.
.
End Sub
If there has been an error, we report that to the user and send the Visual Basic error message that we get from the Error$ function.
Now that the email has been downloaded into the Inbox, we quit the MAPI session using the MAPISession control's SignOff method:
Private Sub email_Click()
MAPISession1.DownLoadMail = True
MAPISession1.SignOn
If Err <> 0 Then
MsgBox "Logon Failure: " + Error$
End If
—> MAPISession1.SignOff
End Sub
It's as simple as that to check the user's email. It's also worth noting that although the MAPISession control has a UserName property and a Password property, the user usually sets them in the Internet Mail dialog box when setting up the email system, so it's not usually necessary to include them here. If the connection is refused, Microsoft Exchange will ask the user to enter the user name and password again in any case, and it stores that information.
Now let's use the MAPIMessages control to send email.