Skip to content

Instantly share code, notes, and snippets.

@simion314
Created April 28, 2017 10:33
Show Gist options
  • Save simion314/b9ca4c8aefee03002dcd7029068106c5 to your computer and use it in GitHub Desktop.
Save simion314/b9ca4c8aefee03002dcd7029068106c5 to your computer and use it in GitHub Desktop.
package com.xxxxx.bulk_email {
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 flash.events.EventDispatcher;
import flash.net.SecureSocket;
import flash.net.Socket;
public class AirXMailWrapper extends EventDispatcher {
private var _initialized:Boolean = false;
private var sender:SMTPSender;
public function AirXMailWrapper() {
this.sender = new SMTPSender();
}
public function initialize(options:EmailSetupOptions):void {
sender.setParameter(SMTPSender.HOST, options.host);
sender.setParameter(SMTPSender.PORT, options.port); // default port is 25
// If you use SMTP-AUTH
sender.setParameter(SMTPSender.AUTH, true);
sender.setParameter(SMTPSender.USERNAME, options.userName);
sender.setParameter(SMTPSender.PASSWORD, options.password);
sender.setParameter(SMTPSender.CONNECTION_TIMEOUT, 102000);
sender.setParameter(SMTPSender.ENABLE_BUFFER, true);
if (options.encryption === EmailSetupOptions.TLS) {
sender.addEventListener(SMTPEvent.SMTP_START_TLS, startTlsHandler);
}
else if (options.encryption === EmailSetupOptions.SSL) {
sender.setParameter(SMTPSender.SOCKET_OBJECT, new SecureSocket());
}
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, options.host);
}
this._initialized = true;
}
public function testConnection():void {
try {//this will run the connect function, it is not public so we can't call it directly
sender.send(null);
}
catch (e:Error) {
}
}
public function sendEmail(messageInfo:EmailMessageInfo):void {
if (!_initialized) {
throw new Error("AirXMailWrapper not initialized!");
}
var message:MimeMessage = buildMimeMessage(messageInfo);
sender.send(message);
}
public static function buildMimeMessage(messageInfo:EmailMessageInfo):MimeMessage {
// Create email message
var message:MimeMessage = new MimeMessage();
// Set from email address and reciepients
var from:INetAddress = new INetAddress(messageInfo.fromEmail, messageInfo.fromName);
message.setFrom(from);
var toReceipt:INetAddress = new INetAddress(messageInfo.toEmail, null);
message.addRcpt(RecipientType.TO, toReceipt);
message.setSubject(messageInfo.subject);
message.setTextBody(messageInfo.message);
return message;
}
public function close():void {
sender.close();
}
override public function addEventListener(_type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void {
sender.addEventListener(_type, listener, useCapture, priority, useWeakReference);
}
override public function removeEventListener(_type:String, listener:Function, useCapture:Boolean = false):void {
sender.removeEventListener(_type, listener, useCapture);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment