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

Related Posts

11 Responses to “airxmail: How to use STARTTLS extension to SMTP(2)”

  • Anand Meena より:

    Hi,

    You are great.
    Very nice example.

    Thanks for this wonderful library.

    Thanks
    Anand Meena
    Air/Flex Developer
    Systematix Infotech Pvt Ltd.

  • Kenny Yates より:

    Great product, has been a joy to work with.
    I am having one issue however…
    When implementing the ability to send IMAP (gmail), using port 587 and TLS, I am able to compose and send mail without any problems.
    But when I reply to an incoming mail message, specifically with gmail, it either bounces back with failure or silently fails without any return.
    Any ideas on if the smtp port should be different or if perhaps the receiving mail server finds that the outgoing message is missing some header/creds or something?
    Any help would be greatly appreciated.

  • coltware より:

    Hi kenny.

    I can not understand the following descriptions.

    >When implementing the ability to send IMAP (gmail),
    IMAP is incoming protocol.
    How do you send message ?

    >But when I reply to an incoming mail message, specifically with gmail, it either bounces >back with failure or silently fails without any return.

    How do you reply the incoming mail message.
    1 ) using airxmail
    2 ) using another MUA like a thunderbird or outlook or gmail client on browser

    Please tell me more detail and steps you did.

  • Benja より:

    Problem with this method

    When i use an example like:

    mimeMsg.setTextBody(textinput.text);

    it does not send the message body.(no message)

    But when i send :
    mimeMsg.setTextBody(“somethis there”);the it work
    what about this??

  • coltware より:

    Hi benja

    How about check textinput.text data.

    trace(textinput.text);
    mimeMsg.setTextBody(textinput.text);

    or

    var text:String = textinput.text;
    trace(text);
    mimeMsg.setTextBody(text);

  • benja より:

    this is my code but this does not ork
    I can get message but there is nothing in message body

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

    import mx.controls.Alert;

    private var sender:SMTPSender;
    private var message:MimeMessage;
    private var mimeMsg:MimeMessage;
    private var from:INetAddress;
    private var toRecpt:INetAddress;
    private var ccRecpt:INetAddress;

    protected function init_email():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,”myMail@gmail.com”);
    sender.setParameter(SMTPSender.PASSWORD,”myPassword”);

    // 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
    {
    mimeMsg = new MimeMessage();
    var from:INetAddress = new INetAddress();
    from.personal = “MyName”;
    from.address = “myMail@gmail.com”;

    mimeMsg.setFrom(from);
    var toAddr:INetAddress = new INetAddress(“otherMail@gmail.com”,”to address”);
    mimeMsg.addRcpt(RecipientType.TO,toAddr);
    // set mail subject
    mimeMsg.setSubject(“Bonjour”);
    var messageBody:String=txtMessage.text;
    trace(messageBody);
    mimeMsg.setTextBody(messageBody);
    sender.send(mimeMsg);
    sender.close();
    }

    protected function sendMessage(event:MouseEvent):void
    {
    sendPlainText();
    Alert.show(“Thank you”);
    btn.enabled=false;
    }

    ]]>

  • benja より:

    Hey :
    Error->init_email()->this.sendPlainText()->txtMessage.text=null

    protected function init_email():void
    {
    this.initSender();
    }

    and

    protected function sendMessage(event:MouseEvent):void
    {
    sendPlainText();
    Alert.show(“Thank you”);
    btn.enabled=false;
    }

  • coltware より:

    Hi benja.

    Error->init_email()->this.sendPlainText()->txtMessage.text=null

    It means ‘txtMessage’ is null or ‘txtMessage.text” is null. right ?
    Maybe it is not airxmail problem,
    Please check how to get string object what you want from txtMessage instance.

  • Tim John より:

    Hi Coltware,

    I’m getting a null object runtime error after StartTLS in com.hurlant.crypto.cert::X509Certificate.getCommonName()

    It seems (subject.findAttributeValue(OID.COMMON_NAME) as PrintableString) is null.

    Any ideas?

    Thanks

  • Tim John より:

    Hi Coltware,

    This patch helped fix my previous problem: https://code.google.com/p/as3crypto/issues/detail?id=39#c3

    But I’m now getting “Error 503: AUTH command used when not advertised”

    It looks like I need to (re)send EHLO after TLS. Is this possible with airxmail.

    I hope you can help.

    Thanks,

    Tim

  • Tim John より:

    Sorry, I was using the wrong procedure. I needed to do it the way outlined on this page: http://flex.coltware.com/2010/07/24/how-to-use-tls-socket-in-airxmail/

    Working now! Thanks.

Leave a Reply

Spam protection by WP Captcha-Free

RSS
Add to Google
2011年1月
« 12月   2月 »
 1
2345678
9101112131415
16171819202122
23242526272829
3031