Hi, I am using airxmail and it works perfectly for me when using gmail. I can
use SSL/TLS and attachments, everything OK.
When I try to send using my client's bluehost account, which has SMTP config
settings similar to gmail, it fails with:
"Received event from SMTPSender: [Event type="smtpConnectionFailed"
bubbles=false cancelable=false eventPhase=2]"
I did some experimeting to try to isolate the problem. Here are some facts:
- I can send from my email program (Thunderbird) running on the same PC as my
AIR app, to the gmail SMTP server AND the bluehost SMTP server
- I can send from a _grails_ web app running locally on the same PC, to the
gmail SMTP server AND the bluehost SMTP server
- in the last case there is one difference between gmail and bluehost: for
bluehost I had to specify the "from" parameter when making the call to
MailService.sendMail even though that was already set in the mail config,
whereas when sending to gmail this was not required. Maybe that's a clue.
public class InvoiceMailer
{
import mx.controls.Alert;
import com.coltware.airxmail.INetAddress;
import com.coltware.airxmail.MailSender.SMTPSender;
import com.coltware.airxmail.MimeMessage;
import com.coltware.airxmail.MimeBinaryPart;
import com.coltware.airxmail.MimeTextPart;
import com.coltware.airxmail.ContentType;
import com.coltware.airxmail.RecipientType;
import com.hurlant.crypto.tls.TLSSocket;
protected var fromEmail:String = "";
protected var fromName:String = "";
protected var alwaysCcEmail:String = "";
protected var alwaysCcName:String = "";
protected var host:String = null;
protected var port:String = null;
protected var auth:Boolean = false;
protected var username:String = null;
protected var password:String = null;
protected var senderHost:String = null;
protected var tls:Boolean = false;
protected var debugMode:Boolean = false;
protected var sender:SMTPSender = null;
public function InvoiceMailer(config:XMLList) // constructor
{
fromEmail = config.from.email;
fromName = config.from.name;
alwaysCcEmail = config.cc.email;
alwaysCcName = config.cc.name;
host = config.smtp.host;
port = config.smtp.port;
senderHost = config.smtp.senderHost;
auth = config.smtp.auth == "true";
if (auth) {
username = config.smtp.username;
password = config.smtp.password;
}
tls = (config.smtp.tls == "true");
debugMode = config.debugMode == "true";
if (debugMode) {
Alert.show(config);
Alert.show(host + "\n" + port + "\n" + auth + "\n" + username + "\n" + password + "\n" + tls + "\n" + debugMode);
}
}
protected function eventHandler(e:Event):void {
if (debugMode) {
Alert.show("Received event from SMTPSender: " + e.toString());
}
}
public function sendEmail(
toEmail:String,
ccEmail:String,
subject:String,
msgBody:String,
attachedFile:File
):void{
// How to send plain text email
// var sender:SMTPSender = new SMTPSender();
sender = new SMTPSender();
sender.addEventListener(IOErrorEvent.IO_ERROR, eventHandler);
sender.addEventListener(SecurityErrorEvent.SECURITY_ERROR, eventHandler);
sender.addEventListener(SMTPEvent.SMTP_AUTH_NG, eventHandler);
sender.addEventListener(SMTPEvent.SMTP_AUTH_OK, eventHandler);
sender.addEventListener(SMTPEvent.SMTP_COMMAND_ERROR, eventHandler);
sender.addEventListener(SMTPEvent.SMTP_CONNECTION_FAILED, eventHandler);
sender.addEventListener(SMTPEvent.SMTP_SENT_OK, eventHandler);
sender.addEventListener(SMTPEvent.SMTP_START_TLS, eventHandler);
sender.setParameter(SMTPSender.HOST,host);
sender.setParameter(SMTPSender.PORT,port); // default port is 25
sender.setParameter(SMTPSender.MYHOSTNAME, senderHost); // not sure
// sender.setParameter("from", fromEmail); // not sure
sender.setParameter(SMTPSender.CONNECTION_TIMEOUT,5000);
sender.setParameter(SMTPSender.ENABLE_BUFFER,true);
if (auth) {
sender.setParameter(SMTPSender.AUTH,true);
sender.setParameter(SMTPSender.USERNAME,username);
sender.setParameter(SMTPSender.PASSWORD,password);
}
// If you use TLS/SSL
// you can get as3crypto at http://code.google.com/p/as3crypto/
if (tls) {
Alert.show("using TLS");
// important
// sender.addEventListener(SMTPEvent.SMTP_START_TLS, startTlsHandler);
var tlssocket:TLSSocket = new TLSSocket();
sender.setParameter(SMTPSender.SOCKET_OBJECT,tlssocket);
}
// Create email message
var contentType:ContentType = ContentType.MULTIPART_MIXED;
var message:MimeMessage = new MimeMessage(contentType);
// Set from email address and reciepients
// var from:INetAddress = new INetAddress(fromEmail, fromName);
// var from:INetAddress = new INetAddress(username);
var from:INetAddress = new INetAddress();
from.personal = fromName;
from.address = fromEmail;
message.setFrom(from);
var toRecpt:INetAddress = new INetAddress(toEmail, "");
message.addRcpt(RecipientType.TO,toRecpt);
var ccRecpt:INetAddress = new INetAddress(ccEmail, "");
message.addRcpt(RecipientType.CC,ccRecpt);
var ccRecpt2:INetAddress = new INetAddress(alwaysCcEmail, alwaysCcName);
message.addRcpt(RecipientType.CC,ccRecpt2);
//
message.setSubject(subject);
message.setTextBody("This is a multi-part message");
//
// Plain Text Part
//
var textPart:MimeTextPart = new MimeTextPart();
textPart.contentType.setParameter("charset","UTF-8");
textPart.transferEncoding = "8bit";
textPart.setText(msgBody);
message.addChildPart(textPart);
// file attachment
var filePart:MimeBinaryPart = new MimeBinaryPart();
filePart.setAttachementFile(attachedFile, attachedFile.name);
filePart.contentType.setMainType("application");
filePart.contentType.setSubType("pdf");
message.addChildPart(filePart);
sender.send(message);
sender.close();
}
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,host);
}
} // end class
Original issue reported on code.google.com by xavier.h...@gmail.com on 11 May 2011 at 4:05
Original issue reported on code.google.com by
xavier.h...@gmail.com
on 11 May 2011 at 4:05