Connect Method  
 

Establish a connection with the specified mail server.

Syntax

object.Connect( [ServerName], [ServerPort], [Username], [Password], [Timeout], [Options] )

Parameters

ServerName
A string which specifies the host name or IP address of the mail server.
ServerPort
A number which specifies the port number used to connect to the server. If this argument is not specified, the value of the ServerType property will determine the default port number.
UserName
A string which specifies the name of the user used to authenticate access to the server. If this argument is not specified, it defaults to the value of the UserName property.
Password
A string which specifies the password used to authenticate the user. If this argument is not specified, it defaults to the value of the Password property. If the BearerToken property has been assigned a value, this parameter will be ignored and OAuth 2.0 authentication will be used instead of standard password authentication.
Timeout
The number of seconds that the client will wait for a response before failing the operation. If this argument is not specified, the value of the Timeout property will be used as the default.
Options
A numeric value which specifies one or more options. If this argument is omitted or a value of zero is specified, a default, standard connection will be established. This argument is constructed by using a bitwise operator with any of the following values:

The settings for Options are:

Value Constant Description
1 mailOptionImplicitSSL This option specifies that an implicit SSL session should be established with the mail server and prevents the use of a command which is used to negotiate an explicit SSL connection. This option should only be used if it is required.
2 mailOptionAPOP Causes the APOP authentication method to be used when connecting to a POP3 mail server. The default is to use standard password authentication.

Return Value

A value of zero is returned if the method succeeds. Otherwise, a non-zero error code is returned which indicates the cause of the failure.

Remarks

The Connect method is used to establish a connection with the specified mail server. This is the first method that must be called prior to the application retrieving mail messages using the GetMessage method. If this method is called when a connection already exists, the current connection will be closed. This has the side-effect of causing any messages which have been marked for deletion to be removed by the mail server.

If the ServerType property has not been explicitly set by the application, and a standard port number is specified, then the server type is automatically determined based on the value of the ServerPort parameter. If a non-standard port number is specified, then you must set the ServerType property to identify whether you are attempting to connect to a POP3 or IMAP4 server.

You should not call the Connect method when sending messages using SMTP. This method is only used to establish a connection with a POP3 or IMAP4 server. For more information about sending messages, see the SendMessage method and the RelayServer and RelayPort properties.

Example

The following example connects to a mail server and retrieves each of the mail messages, storing them in a file on the local system:

    Dim strFileName As String
    Dim nMessage As Long, nError As Long

    nError = InternetMail1.Connect(strServerName, , strUserName, strPassword)
    
    If nError > 0 Then
        MsgBox "Unable to connect to " & strServerName & vbCrLf & _
               InternetMail1.LastErrorString, vbExclamation
        Exit Sub
    End If
    
    If InternetMail1.LastMessage = 0 Then
        MsgBox "The mailbox is currently empty", vbInformation
        InternetMail1.Disconnect
        Exit Sub
    End If
    
    For nMessage = 1 To InternetMail1.LastMessage
        strFileName = "c:\temp\msg" & Format(nMessage, "00000") & ".txt"
        nError = InternetMail1.StoreMessage(nMessage, strFileName)
        If nError > 0 Then
            MsgBox "Unable to store message " & nMessage & vbCrLf & _
                   InternetMail1.LastErrorString, vbExclamation
            Exit For
        End If
    Next
    
    If nError = 0 Then
        MsgBox "Stored " & InternetMail1.LastMessage & " messages", vbInformation
    End If
    
    InternetMail1.Disconnect

See Also

BearerToken Property, Password Property, Secure Property, ServerName Property, ServerPort Property, ServerType Property, UserName Property, Disconnect Method, GetMessage Method, SendMessage Method