Posts Tagged ‘pop3’

You have to care 2 type of content type if you want to get html part of e-mail message.

1 ) text/html
2 ) multipart/alternative

1 ) text/html

In this case, you can get html content very easily.
The mime message only has one body part.

private function handleMessage(event:IMessageEvent):void{
  var msg:MimeMessage = event.getMimeMessage();
  var htmlText:String = msg.bodyText;
}
2 ) multipart/alternative

This case is slightly harder than case 1.
But airxmail support to access to child part using findParts method.

var parts:Array = msg.findParts("text/html");
  if(parts.length > 0){
    var htmlText:String = MimeBodyPart(parts[0]).bodyText;
  }
}

Typically, text/html part is the only one.
So I think you don’t need to check first text/html part.

see source code

MimeMessage.findPart

I made airxmail swc file on google codes.
The file is draft implemetation version for IMAP4.
So if you don’t want to use draft version, please wait…

It doen’t support IMAP4 all functions.
But it is available to get e-mail message from imap server, like gmail imap.

These code are sample to connect and get message from gmail account.

Create client instance
import com.coltware.airxmail.imap.IMAP4Client;
:

private var client:IMAP4Client;

public function init():void{

	client = new IMAP4Client;
	client.host = "imap.gmail.com";
	client.port = 993;
	client.socketObject = new SecureSocket();
}
mail flow to connect and get e-mail
//  Set handler for Folder Result
client.addEventListener(IMAP4Event.IMAP4_FOLDER_RESULT,folderListResult);

//  Set handler for UID list result ( uid is a key to get e-mail from server )
client.addEventListener(IMAP4ListEvent.IMAP4_RESULT_UID_LIST,uidList);

//  Set message handler
client.addEventListener(IMAP4MessageEvent.IMAP4_MESSAGE,handleMessage);

//  Set username and password, and invoked connect method
client.setAuth(username,password);
client.connect();
client.login();

//  get subscribed folder list
client.lsubBlocking();

// logout
client.logout();
Implemantation of folder result handler
private function folderListResult(event:IMAP4Event):void{
  var folder:IMAP4Folder = event.result as IMAP4Folder;
  trace("NAME=>[" + folder.name + " --> " + folder.nameUTF8 + "]");
  if(!folder.noselect){
    client.selectMailbox(folder).search(IMAP4Search.dateSentSince("2010/10/01"));
  }
}

POINT1

if(!folder.noselect){

}

If the folder’s noselect property is true, you can NOT select mailbox,

POINT2

client.selectMailbox(folder).search(IMAP4Search.dateSentSince("2010/10/01"));

If you want to get ALL messages from the folder,

client.selectMailbox(folder).search(IMAP4Search.ALL);

If you know the folder name ( ex “INBOX” ) and want to get ALL messages form the folder,
you don’t need to get folder list, so you can change main flow source as following below..

:
client.login();

// client.lsubBlocking();
client.selectMailbox("INBOX").search(IMAP4Search.ALL);
client.logout();
Implemantation of message get handler
//  result for search method
private function uidList(event:IMAP4ListEvent):void{
  for(var i:int = 0; i<event.length; i++){
    var id:String = event.getValue(i);
    client.message(id);
  }
}

private function handleMessage(event:IMessageEvent):void{
  var msg:MimeMessage = event.getMimeMessage();
  trace("SUBJECT : " + msg.subjectUTF8);
}

IMessageEvent does not depends on IMAP4 API.
So you can handle the object like as POP3 API.

If you want to know how to get e-mail message.
Please refer to HERE.

private	function messageHandler(e:POP3MessageEvent):void{
	var msg:MimeMessage = e.getMimeMessage();
	//  attachmentChildren are only parts has attachments files.
	//  Don't need to check all children to extract files

	var attaches:Array = msg.attachmentChildren;
	if(attaches.length > 0){
		for each(var attach:MimeBodyPart in attaches){
			if(attach is MimeBinaryPart){
				var binPart:MimeBinaryPart = attach as MimeImagePart;
				var bytes:ByteArray = attach.bodyByteArray;

				//  v0.5 and less than r69 version has BUG !!
				//  binPart.getHeader("Content-Disposition"); is both available

				var contentDisposition:MimeHeader = binPart.contentDisposition;

				if(contentDisposition){
					var filename:String = contentDisposition.getParameter("filename");
					filename = MimeUtils.decodeMimeHeader(filename);
					log.debug("Filename => " + filename );

					//  save Desktop directory, plese change following codes as you like

					var file:File =	File.desktopDirectory.resolvePath(filename);
					var fs:FileStream = new	FileStream();
					fs.open(file,FileMode.APPEND);
					fs.writeBytes(bytes);
					fs.close();
				}
			}
		}
	}
}
RSS
Add to Google
2012年5月
« 3月    
 12345
6789101112
13141516171819
20212223242526
2728293031