Posts Tagged ‘gmail’

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

RSS
Add to Google
2017年3月
« 2月    
 1234
567891011
12131415161718
19202122232425
262728293031