Accessing Gmail on SSL

SMTP

This article shows how to perform connect to a Gmail server and send an email using the SMTP protocol on SSL.

Connecting to Gmail SMTP server

The following code snippet shows you how to connect to an SSL enabled SMTP server.

Sending an Email Message

The code above set up the SMTPClient object to connect to the Gmail server. To send a message using the same client object, create a MailMessage class object and send the message using the SMTP client object. The following code snippet shows you how to set the message properties, for example the subject, to and body:

IMAP

This article shows how to perform a number of activities on an SSL enabled mail server using the IMAP protocol:

  • Connect to a mail server.
  • Get the total number of messages in an inbox.
  • Save messages locally.
  • Create a message an add it to a folder.

Connecting to the Mail Server

Use Aspos.Email’s ImapClient class object to connect to the mail server. The server’s address, port, username and password are required to establish a connection. Gmail uses port 993 for the IMAP protocol, the following code snippet shows you how connects to Gmail using that port.

Selecting a Folder and Getting the Total Number of Messages

Checking the Inbox folder is the most frequent task when checking email. Using Aspose.Email, this can be done using just two simple lines of code. The following code snippet shows you how to access the Inbox folder and get the total number of messages in the folder.

Saving Messages to a Local Hard Drive

Once a folder has been selected with the SelectFolder method, use the ListMessages function to get a list of all the messages in the folder in an ImapMessagesInfoCollection object. Iterate through this collection and save email messages to the local drive of computer as follows:

Creating a New Folder

The IMAP protocol also allows you to create a new folder on the email server. This can be done using a simple function call.

Creating a New Message in a Folder

Add a new message to the folder using the MailMessage and ImapClient classes. The examples below creates a MailMessage object first by providing the subject, to and from values. It then subscribes to a folder and adds the message to it. The following code snippet shows you create a new message in a folder.

POP3

This article shows some examples that use the POP3 protocol on SSL. To connect to an SSL-protected server, we need to define the SSL port and two extra properties. The rest of the code is the same as for connecting to a normal POP3 server.

The code samples below show how to:

  • Connect to an SSL server.
  • Check the mailbox status
  • Get information about the message
  • Retrieve emails.

Connecting to Mail Server

Connect to the SSL enabled mail server using the Pop3client class as described below.

Checking the Mailbox Status

The following code snippet shows you how checks the number of messages stored in the mailbox and the size of the mailbox. Use Pop3MailboxInfo class for this purpose.

Checking Message Information

This example checks all the messages in the mailbox using the Pop3MessageInfoCollection class. Use the Pop3Client.ListMessages() function to get the Pop3MessageInfoCollection collection. Then iterate through the collection to read the message information: message ID, index, subject and size

Retrieving Messages

To get the messages from the mailbox, use the Pop3Client class' FetchMessage() method to get the message into a MailMessage type object. The following code snippet shows you how to counts the number of emails in the mailbox and then iterates through them to retrieve each one.