通过套接字捕获 smtp 电子邮件服务器返回代码的问题
我编写了一个简单的 smtp 邮件发送程序的(精简版)。
一切正常(在这个精简版本和更复杂的版本中)。
但是,电子邮件服务器在发送邮件时会返回许多消息。
ProgressEvent.SOCKET_DATA(handleNewData函数)似乎没有捕获所有电子邮件返回消息
(特别是我想捕捉
250 2.0.0 好的:按 XYZ 排队
消息以确认消息已发送。
有人知道为什么所有消息都没有被handleNewData捕获,我该如何纠正这个问题?
提前致谢。
我的代码是用 Adobe Air 2.5 编写的
我的跟踪输出如下:
Connecting to the mail server, example.com:25
Response is: 220 example.com ESMTP Postfix
Response is: 250 example.com
Response is: 250 2.1.0 Ok
250 2.1.5 Ok
354 End data with <CR><LF>.<CR><LF>
250 2.0.0 Ok: queued as 2C0264F5FF
221 2.0.0 Bye
我的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="Smtp()">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Button label="Send Email" x="200" y="200" click="mail()"> </s:Button>
<fx:Script>
<![CDATA[
import flash.net.Socket;
import flash.profiler.showRedrawRegions;
import flash.utils.ByteArray;
import mx.core.mx_internal;
import mx.utils.ObjectUtil;
private var socket:Socket;
private var serverURL:String;
private var serverPort:int = 25;
private var serverResponse:ByteArray = new ByteArray();
public var sendData:Array = new Array();
public var dataStr:String;
public function Smtp():void
{
this.serverURL = "example.com";
this.serverPort = 25;
socket = new Socket();
socket.addEventListener(ProgressEvent.SOCKET_DATA,handleNewData);
this.connectToServer();
}
public function mail():void
{
dataStr ="HELO example.com" + "\r\n";
sendString(dataStr);
dataStr = "MAIL FROM:<[email protected]>\r\n";
sendString(dataStr);
dataStr = "RCPT TO:<[email protected]>\r\n";
sendString(dataStr);
dataStr = "DATA\r\n";
sendString(dataStr);
dataStr = "To: <[email protected]>\r\n";
sendString(dataStr);
dataStr = "Date: Wed Jan 26 2011 07:49:31 PM\r\n"
sendString(dataStr);
dataStr = "Subject: Test\r\n";
sendString(dataStr);
dataStr = "My Text\r\n";
sendString(dataStr);
dataStr = ".\r\n";
sendString(dataStr);
dataStr = "QUIT\r\n";
sendString(dataStr);
}
private function connectToServer():void
{
trace("Connecting to the mail server, " + serverURL + ":" + serverPort);
socket.connect(serverURL, serverPort);
}
private function sendString(dataStr:String):void
{
var bytes:ByteArray = new ByteArray();
bytes.writeMultiByte(dataStr, "UTF-8");
socket.writeBytes(bytes);
socket.flush();
}
private function handleNewData(event:ProgressEvent):void
{
var numBytes:int = socket.bytesAvailable;
serverResponse = new ByteArray();
while(--numBytes >= 0)
{
var byte:int = socket.readUnsignedByte();
serverResponse.writeByte(byte);
}
var response:String = serverResponse.toString();
trace("\t Response is: " + response);
}
]]>
</fx:Script>
</s:WindowedApplication>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,
套接字确实正在接收所有服务器响应:-)
只是在返回排队消息的部分附近,套接字一次接收了几行。
哦,好吧,希望代码对某人有用。
Sorry chaps/chapettes,
the socket is indeed receiving all the server responses:-)
It's simply that near the part where the queued message is returned, the socket receives several lines at once.
Oh, well, hope the code was of some use to somebody.