我正在尝试为使用Ct中的Qt框架创建的自定义应用程序创建一个简单的发送邮件功能。在SMTP尝试进行身份验证的时候,我非常接近,并且终其一生都无法使它正常工作。这是我正在处理的
do {
responseLine = socket->readLine();
response += responseLine;
}
while ( socket->canReadLine() && responseLine[3] != ' ' );
responseLine.truncate( 3 );
if ( state == Init && responseLine[0] == '2' ) {
// banner was okay, let's go on
*t << "HELO there\r\n";
t->flush();
state = Auth;
} else if (state == Auth && responseLine[0] == '2') {
*t << "STARTTLS\r\n";
*t << "AUTH PLAIN\r\n";
t->flush();
state = User;
} else if (state == User && responseLine[0] == '2') {
*t << "username\r\n";
t->flush();
state = Pass;
} else if (state == Pass && responseLine[0] == '2') {
*t << "pass\r\n";
t->flush();
state = Mail;
} else if ( state == Mail && responseLine[0] == '2' ) {
// HELO response was okay (well, it has to be)
*t << "MAIL FROM: " << from << "\r\n";
t->flush();
state = Rcpt;
} else if ( state == Rcpt && responseLine[0] == '2' ) {
*t << "RCPT TO: " << rcpt << "\r\n"; //r
t->flush();
state = Data;
} else if ( state == Data && responseLine[0] == '2' ) {
*t << "DATA\r\n";
t->flush();
state = Body;
} else if ( state == Body && responseLine[0] == '3' ) {
*t << message << "\r\n.\r\n";
t->flush();
state = Quit;
} else if ( state == Quit && responseLine[0] == '2' ) {
*t << "QUIT\r\n";
t->flush();
// here, we just close.
state = Close;
emit status( tr( "Message sent" ) );
} else if ( state == Close ) {
deleteLater();
return;
} else {
// something broke.
最佳答案
您正在使用QTextStream吗?好像错了QTextStream用于编写编码的文本,并且会困惑编码和行尾,而这并不是实现协议(protocol)时想要的。最好使用QByteArray(而不是QString)和QDataStream而不是QTextStream,或者为了更好地进行错误处理,请直接写入套接字。