gpt4 book ai didi

c++ - SMTP编译错误

转载 作者:行者123 更新时间:2023-12-02 11:04:53 27 4
gpt4 key购买 nike

我有用于登录身份验证的SMTP代码,我正在Ubuntu 13.04下对其进行编译

但是尝试使用g++ -o命令行编译程序时出现编译器错误

mailogin.cpp: In function ‘bool smtp_send(std::string, int, std::string, std::string, std::vector<std::basic_string<char> >, std::string, std::string, std::string)’:
mailogin.cpp:148:17: error: ‘login_name’ was not declared in this scope
mailogin.cpp:148:37: error: ‘login_pass’ was not declared in this scope
mailogin.cpp:150:34: error: ‘memset’ was not declared in this scope
mailogin.cpp:152:61: error: ‘memcpy’ was not declared in this scope
mailogin.cpp:190:1: error: expected ‘}’ at end of input
mailogin.cpp:190:1: error: expected ‘}’ at end of input
mailogin.cpp:190:1: error: expected ‘}’ at end of input
mailogin.cpp:190:1: error: expected ‘}’ at end of input
mailogin.cpp:190:1: error: expected ‘}’ at end of input
mailogin.cpp:190:1: error: expected ‘}’ at end of input

有人能帮我吗?

这是我的代码
#include <string>
#include <strings.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <resolv.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <vector>

using std::string;
using std::vector;

#define safe_send( sd, b ) if( sd_write(sd, b) <= 0 ){ close(sd); return false; }
#define safe_read( sd, b ) if( (buffer = sd_read(sd)) == "" ){ \
close(sd); return false; \
} \
else if( buffer.find("220") == string::npos && \
buffer.find("235") == string::npos && \
buffer.find("250") == string::npos && \
buffer.find("334") == string::npos && \
buffer.find("354") == string::npos ){ \
printf( "WARNING: %s\n", buffer.c_str() ); \

int sd_create( char *host, int port ){
int sd = socket( AF_INET, SOCK_STREAM, 0 );

if( sd <= 0 ){
return -1;
}

struct sockaddr_in server;
hostent * resol = gethostbyname( host );
if( resol ){
bzero( &server, sizeof(server) );
server.sin_family = AF_INET;
server.sin_port = htons(port);
bcopy( resol->h_addr, &(server.sin_addr.s_addr), resol->h_length );
}

if( connect( sd, (struct sockaddr*)&server, sizeof(server) ) != 0 ){
return -1;
}

return sd;
}

string sd_read( int sd ){
string buffer("");
unsigned char c;
bool isEOL(false), isEOF(false);

while( !isEOL && !isEOF ){
if( recv( sd, &c, sizeof(unsigned char), 0 ) < 1 ){
isEOF = true;
}
else if( c == '\n' ){
isEOL = true;
}
else if( c != '\r' ){
buffer += c;
}
}

return buffer;
}

int sd_write( int sd, string buffer ){
return write( sd, buffer.c_str(), buffer.size() );
}

string base64( unsigned char *data, unsigned int size ){
static const char b64_charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

string ret;
unsigned char block_3[3];
unsigned char block_4[4];
unsigned char * str = data;
int i = 0,
j = 0;

while( size-- ){
block_3[i++] = *(str++);
if( i == 3 ){
block_4[0] = (block_3[0] & 0xfc) >> 2;
block_4[1] = ((block_3[0] & 0x03) << 4) + ((block_3[1] & 0xf0) >> 4);
block_4[2] = ((block_3[1] & 0x0f) << 2) + ((block_3[2] & 0xc0) >> 6);
block_4[3] = block_3[2] & 0x3f;

for(i = 0; (i <4) ; i++){
ret += b64_charset[block_4[i]];
}
i = 0;
}
}

if(i){
for(j = i; j < 3; j++){
block_3[j] = '\0';
}
block_4[0] = (block_3[0] & 0xfc) >> 2;
block_4[1] = ((block_3[0] & 0x03) << 4) + ((block_3[1] & 0xf0) >> 4);
block_4[2] = ((block_3[1] & 0x0f) << 2) + ((block_3[2] & 0xc0) >> 6);
block_4[3] = block_3[2] & 0x3f;

for(j = 0; (j < i + 1); j++){
ret += b64_charset[block_4[j]];
}
while((i++ < 3)){
ret += '=';
}
}

return ret;
}

bool smtp_send( string server_name, int port, string from, string subject, vector<string> receivers, string message, string login, string password )
{
int i, j, sd;
string buffer("");

sd = sd_create( (char *)server_name.c_str(), port );
if ( sd == -1 ){
return false;
}

safe_send( sd, "EHLO emoticode_mailer\r\n" );

// read until last '250 .*' reached
while( (buffer = sd_read(sd)) != "" ){
if( buffer.find("220") == string::npos &&
buffer.find("235") == string::npos &&
buffer.find("250") == string::npos &&
buffer.find("334") == string::npos &&
buffer.find("354") == string::npos ){

printf( "WARNING: %s\n", buffer.c_str() );
}
else if( buffer.find( "250 " ) != string::npos ){
break;
}
}

int auth_len = login_name.size() + login_pass.size() + 2;
unsigned char * b64auth = new unsigned char[auth_len];
memset( b64auth, 0x00, auth_len );

memcpy( &b64auth[1], login_name.c_str(), login_name.size() );
memcpy( &b64auth[2 + login_name.size()], login_pass.c_str(), login_pass.size() );

string hash = base64( b64auth, auth_len );

delete [] b64auth;

safe_send( sd, "AUTH PLAIN " + hash + "\r\n" );
safe_read( sd, buffer );

safe_send( sd, "MAIL FROM: <" + from + ">\r\n" );
safe_read( sd, buffer );
for( i = 0; i < receivers.size(); ++i ){
safe_send( sd, "RCPT TO: <" + receivers[i] + ">\r\n" );
safe_read( sd, buffer );
}
safe_send( sd, "DATA\r\n" );
safe_read( sd, buffer );

safe_send( sd, "Subject: " + subject + "\r\n" );
safe_send( sd, "From: <" + from + ">\r\n" );
safe_send( sd, "To: <" + receivers[0] + ">\r\n" );
safe_send( sd, "Content-Type: text/plain\r\n" );
safe_send( sd, "Mime-Version: 1.0\r\n" );
safe_send( sd, "X-Mailer: Emoticode smtp_send\r\n" );
safe_send( sd, "Content-Transfer-Encoding: 7bit\r\n\r\n" );

safe_send( sd, message );

safe_send( sd, "\r\n.\r\n" );
safe_read( sd, buffer );
safe_send( sd, "RSET\r\n" );
safe_read( sd, buffer );
safe_send( sd, "QUIT\r\n" );

close(sd);

return true;
}

最佳答案

您没有声明login_namelogin_pass。不应该将loginpassword传递给函数smtp_send作为参数吗?

关于c++ - SMTP编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19464027/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com