Chapter 6 extracting e-mail from POP3 server

Posted by christine75 on Tue, 08 Feb 2022 09:34:21 +0100

Chapter 6 extracting e-mail from POP3 server

Extract email from POP3 server

Communication with POP3 server

If you have the required permissions and the mail server is running, you can use the POP3 protocol to download and process e-mail from the server. Typically, to communicate with a pop3 server, log in, perform a series of actions that affect the mailbox, and then commit or roll back any changes. To do this in inter system IRIS:

  1. Create% net An instance of POP3. This object describes the POP3 server that will be used.
  2. You can optionally specify% net The following properties of POP3 instances:
  • Port - specifies the port to use; The default value is 110.
  • Timeout - specifies the read timeout (in seconds); The default value is 30 seconds.
  • StoreAttachToFile - specifies whether each attachment is saved to a file when the message is read (when the message contains a content disposition; attachment header). The default value is False. Note that this setting has no effect unless AttachDir is also set.
  • StoreInlineToFile - specifies whether to save each inline attachment to a file when reading a message (when the message contains a content disposition; inline header). The default value is False. Note that this setting has no effect unless AttachDir is also set.
  • AttachDir - specifies the directory to which attachments are saved. No breach of contract. Depending on the operating system, be sure to end the directory name with a slash (/) or backslash (\). Also make sure that the directory already exists and that the user has write access to it.
  • IgnoreInvalidBase64Chars - specifies whether invalid characters found during base-64 decoding are ignored. The default value is false (invalid characters can cause errors). Note that RFC 2045 is ambiguous about whether unexpected characters should be ignored or whether errors should be caused during base-64 decoding.
  1. To connect to a POP3 server using SSL/TLS:

a. Set the SSLConfiguration property to the name of the activated SSL/TLS configuration to use.

b. Set the usestattls property to 0 or 1.

In most cases, the value 0 is used. If the server interaction starts on a normal TCP socket and then switches to TLS on the same port as the normal socket, a value of 1 is used.

c. Alternatively, set the sslccheckserveridentity property to 1. Do this if you want to verify the host server name in the certificate.

  1. Call the Connect() method of the instance. This method accepts three parameters in order:

a. Name of POP3 server

b. User name

c. Code

  1. Use the instance method to check the mailbox, retrieve messages, and delete messages. The following sections provide details.
  2. Alternatively, to prevent connection timeouts, call% net Ping() method of POP3 instance.
  3. Alternatively, if you have marked messages for deletion and now choose not to delete them, call% net RollbackDeletes() method of POP3 instance.
  4. When you finish making changes to your mailbox, call one of the following methods:
  • QuitAndCommit() - commit changes and log out of the mail server.
  • QuitAndRollback() - roll back changes and log off from the mail server.

Each of these methods returns a state that should be checked before continuing. See also% net POP3 class reference to get the complete method signature.

The examples in the following sections use two different free POP3 services available at the time of writing this manual. Choosing these services does not mean special recognition. Also note that these examples do not show the actual password.

Example 1: HotPOPAsPOP3()

Log in to the HotPOP POP3 server using the account you previously set up for this:

ClassMethod HotPOPAsPOP3() As %Net.POP3
{
	Set server=##class(%Net.POP3).%New()

	//The HotPOP POP3 server uses the default port,
	Set server.port=110

	//In case we plan to get any email with attachments
	Set server.StoreAttachToFile=1
	Set server.StoreInlineToFile=1
	Set server.AttachDir="c:\DOWNLOADS\"

	Set servername="pop.hotpop.com"
	Set user="isctest@hotpop.com"
	Set pass="123pass"

	Set status=server.Connect(servername,user,pass)
	If $$$ISERR(status) {
		Do $System.Status.DisplayError(status) 
		Quit $$$NULLOREF
	}
	Quit server
}

This method returns% net Pop3 server instance. Many examples later in this topic accept% net POP3 instance as a parameter.

Example 2: ypossaspop3 ()

The following method also returns% net Pop3 server instance. In this example, we use YPOPS, a client software that provides SMTP and POP3 access to Yahoo e-mail accounts. It uses a test account that has been set up for this purpose:

ClassMethod YPOPsAsPOP3() As %Net.POP3
{
	Set server=##class(%Net.POP3).%New()

	//YPOPs uses the default port 
	//but let's set it anyway
	Set server.port=110

	//just in case we plan to fetch any messages
	//that have attachments
	Set server.StoreAttachToFile=1
	Set server.StoreInlineToFile=1
	Set server.AttachDir="c:\DOWNLOADS\"

	//local host acts as the server
	Set servername="127.0.0.1"
	//YPOPs works with a Yahoo email account
	Set user="isc.test@yahoo.com"
	Set pass="123pass"

	Set status=server.Connect(servername,user,pass)
	If $$$ISERR(status) {
		Do $System.Status.DisplayError(status) 
		Quit $$$NULLOREF
	}
	Quit server
}

Get information about mailboxes

When the user has access to a POP3 account, he or she will be connected to the server. Use the following methods to find the contents of the mailbox:

GetMailBoxStatus()

Returns by reference the number of messages in the mailbox and the number of bytes used by the mailbox.

GetMessageUIDArray()

Given an empty string as the first argument, this method returns by reference an array of information about the messages in the mailbox (excluding any messages currently marked for deletion). Each element in this array contains the following information about a message:

Array KeyArray Item
The message number of the current status in the mailbox. The first message is the number 1, and so on. The message number of a given message cannot be guaranteed to be the same in all sessions.Unique message identifier (UID), which is the permanent identifier available for this message in all sessions. The UID is unique for each mailbox.

GetSizeOfMessages()

Given an empty string as the first argument, this method returns by reference an array of information about the messages in the mailbox (excluding any messages currently marked for deletion). Each element in this array contains the following information about a message:

Array KeyArray Item
The message number of the current status in the mailbox.The size of this message in bytes.

Each of these methods returns a state that should be checked before continuing.

Example: ShowMailbox()

For example, the following method writes information about the mailbox we are currently accessing:

ClassMethod ShowMailbox(server as %Net.POP3) 
{
    Set status=server.GetMailBoxStatus(.count,.size)
    If $$$ISERR(status) {
       Do $System.Status.DisplayError(status) 
       Quit 
    }
    Write "Mailbox information *****",!
    Write "Number of messages in mailbox: ",count,!
    Write "Size of messages: ",size,!

    Set status=server.GetMessageUIDArray(,.uids)
    Set status=server.GetSizeOfMessages(,.sizes)
    
    //iterate through messages, get info, and write it
    For i=1:1:count {
        Set uid=uids.GetAt(i)
        Set size=sizes.GetAt(i)
        Write "Msg number:", i,"   UID:",uid, "   size:",size,!
    }

}

This method produces output similar to the following:

Mailbox information *****
Number of messages in mailbox: 4
Size of messages: 18634
Msg number:1   UID:6ef78df6fd660391   size:7245
Msg number:2   UID:7410041a6faf4a87   size:5409
Msg number:3   UID:5555af7fa489e406   size:5121
Msg number:4   UID:299ad2b54c01a6be   size:859

Extract mail from mailbox

To simply get messages, use% net One of the following methods of POP3 class:

Fetch()

Given the message number as the first parameter, this method returns (by reference, as the second parameter) the% net. Net containing the message Mailmessage instance.

FetchMessage()

Given the message number as the first parameter, this method returns (by reference) information such as From, To, and other public headers, an array containing all headers (including public headers), and the message content itself

Each of these methods returns a state that you should check before continuing. Note that these methods return an error status if the message is currently marked for deletion.

Example: FetchMailbox()

The following example is a variation of the ShowMailbox example described in "getting information about mailboxes". This method uses the fetch() method to check each message and write the subject line of each message:

ClassMethod FetchMailbox(server As %Net.POP3)
{
	Set status=server.GetMailBoxStatus(.count,.size)
	If $$$ISERR(status) {
		Do $System.Status.DisplayError(status) 
		Quit $$$NULLOREF
	}
	Write "Mailbox information *****",!
	Write "Number of messages in mailbox: ",count,!
	Write "Size of messages: ",size,!

	Set status=server.GetMessageUIDArray(,.uids)
	Set status=server.GetSizeOfMessages(,.sizes)

	//iterate through messages, get info, and write it
	For i=1:1:count {
		Set uid=uids.GetAt(i)
		Set size=sizes.GetAt(i)
		Set status=server.Fetch(i,.msg)
		If $$$ISERR(status) {
			Set subj="***error***"
		} else{
			Set subj=msg.Subject
		}
		Write "Msg number:", i,"  UID:",uid, "  Size:",size
		Write "  Subject: ",subj,!
	}
}