Posts Tagged ‘starttls’

Complete STARTTLS sample code

import com.coltware.airxmail.INetAddress;
import com.coltware.airxmail.MailSender.SMTPSender;
import com.coltware.airxmail.MimeMessage;
import com.coltware.airxmail.RecipientType;
import com.hurlant.crypto.tls.TLSSocket;

var sender:SMTPSender;

public function main():void{
	this.initSender();
	this.sendPlainText();
}

public function initSender():void{
	sender = new SMTPSender();
	
	sender.setParameter(SMTPSender.HOST,"smtp.gmail.com");
	sender.setParameter(SMTPSender.PORT,587);
	sender.setParameter(SMTPSender.CONNECTION_TIMEOUT,5000);
	sender.setParameter(SMTPSender.ENABLE_BUFFER,true);
	
	sender.setParameter(SMTPSender.AUTH,true);
	sender.setParameter(SMTPSender.USERNAME,"your_email.address@gmail.com");
	sender.setParameter(SMTPSender.PASSWORD,"password");
	
	//  STARTTLS Handler
	sender.addEventListener(SMTPEvent.SMTP_START_TLS,startTlsHandler);
}

public function startTlsHandler(event:SMTPEvent):void{
	var sock:Socket = event.socket as Socket;
	var tls:TLSSocket = new TLSSocket();
	sender.setParameter(SMTPSender.SOCKET_OBJECT,tls);
	tls.startTLS(sock,"smtp.gmail.com");
}

public function sendPlainText():void{
	var mimeMsg:MimeMessage = new MimeMessage();
	
	var from:INetAddress = new INetAddress();
	from.personal = "Sample User";
	from.address = "your_email.address@gmail.com";
	
	mimeMsg.setFrom(from);
	var toAddr:INetAddress = new INetAddress("toemail@xxxxxxx.xxxx","to address");
	mimeMsg.addRcpt(RecipientType.TO,toAddr);
	// set mail subject
	mimeMsg.setSubject("Hello Airxmail");
	mimeMsg.setTextBody("this is body\r\n2n line");
	
	sender.send(mimeMsg);
	sender.close();
}

starttls event fired if SMTP EHLO command result has STARTTLS.
ex )
00:04:07.694 [DEBUG] com.coltware.airxmail.smtp.SMTPClient [EHLO]:250-mx.google.com at your service, [222.151.155.105]
00:04:07.694 [DEBUG] com.coltware.airxmail.smtp.SMTPClient [EHLO]:250-SIZE 35651584
00:04:07.694 [DEBUG] com.coltware.airxmail.smtp.SMTPClient [EHLO]:250-8BITMIME

00:04:07.694 [DEBUG] com.coltware.airxmail.smtp.SMTPClient [EHLO]:250-STARTTLS

00:04:07.694 [DEBUG] com.coltware.airxmail.smtp.SMTPClient [EHLO]:250 ENHANCEDSTATUSCODES

The STARTTLS extension is complex for a moment.
A smtp client have to connect the server with plain socket.
After the client gives STARTTLS command , the client have to change to tls socket from plain socket.

The following code is a sample to use STARTTLS command


// comment out because of using plain socket
//sender.setParameter(SMTPSender.SOCKET_OBJECT,new TLSSocket());

sender.addEventListener(SMTPEvent.SMTP_START_TLS,startTlsHandler);

// chage to TLSSocket
public function startTlsHandler(event:SMTPEvent):void{
var sock:Object = event.socket as Socket;
var tls:TLSSocket = new TLSSocket();
sender.setParameter(SMTPSender.SOCKET_OBJECT,tls);

tls.startTLS(sock,”your.host.name”); // <– please set “your.host.name” as you like } [/as3]

RSS
Add to Google
2017年5月
« 2月    
 123456
78910111213
14151617181920
21222324252627
28293031